Skip to content

Commit

Permalink
feat: upgrade pyo3 to 0.23 (#5368)
Browse files Browse the repository at this point in the history
  • Loading branch information
XmchxUp authored Dec 3, 2024
1 parent 0b077ec commit a686ba3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bindings/python/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ After `venv` has been prepared, you can activate it by `source venv/bin/activate
To simplify our work, we will utilize the tool [`maturin`](https://github.com/PyO3/maturin). Kindly install it beforehand.

```shell
pip install maturin[patchelf]
pip install 'maturin[patchelf]'
```

## Build
Expand Down
6 changes: 2 additions & 4 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ futures = "0.3.28"
opendal = { version = ">=0", path = "../../core", features = [
"layers-blocking",
] }
pyo3 = "0.22.5"
pyo3-async-runtimes = { version = "0.22.0", features = [
"tokio-runtime",
] }
pyo3 = "0.23.2"
pyo3-async-runtimes = { version = "0.23.0", features = ["tokio-runtime"] }
tokio = "1"

[target.'cfg(unix)'.dependencies.opendal]
Expand Down
11 changes: 7 additions & 4 deletions bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use pyo3::buffer::PyBuffer;
use pyo3::exceptions::PyIOError;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;
use pyo3_async_runtimes::tokio::future_into_py;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -463,12 +464,13 @@ impl AsyncFile {
}
};

let ret = reader
let pos = reader
.seek(whence)
.await
.map_err(|err| PyIOError::new_err(err.to_string()))?;
Ok(Python::with_gil(|py| ret.into_py(py)))
Ok(pos)
})
.and_then(|pos| pos.into_bound_py_any(py))
}

/// Return the current stream position.
Expand All @@ -495,8 +497,9 @@ impl AsyncFile {
.stream_position()
.await
.map_err(|err| PyIOError::new_err(err.to_string()))?;
Ok(Python::with_gil(|py| pos.into_py(py)))
Ok(pos)
})
.and_then(|pos| pos.into_bound_py_any(py))
}

fn close<'p>(&'p mut self, py: Python<'p>) -> PyResult<Bound<PyAny>> {
Expand All @@ -514,7 +517,7 @@ impl AsyncFile {
}

fn __aenter__<'a>(slf: PyRef<'a, Self>, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
let slf = slf.into_py(py);
let slf = slf.into_py_any(py)?;
future_into_py(py, async move { Ok(slf) })
}

Expand Down
36 changes: 15 additions & 21 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,29 @@ fn _opendal(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<WriteOptions>()?;

// Layer module
let layers_module = PyModule::new_bound(py, "layers")?;
let layers_module = PyModule::new(py, "layers")?;
layers_module.add_class::<Layer>()?;
layers_module.add_class::<RetryLayer>()?;
layers_module.add_class::<ConcurrentLimitLayer>()?;
m.add_submodule(&layers_module)?;
py.import_bound("sys")?
py.import("sys")?
.getattr("modules")?
.set_item("opendal.layers", layers_module)?;

let exception_module = PyModule::new_bound(py, "exceptions")?;
exception_module.add("Error", py.get_type_bound::<Error>())?;
exception_module.add("Unexpected", py.get_type_bound::<UnexpectedError>())?;
exception_module.add("Unsupported", py.get_type_bound::<UnsupportedError>())?;
exception_module.add("ConfigInvalid", py.get_type_bound::<ConfigInvalidError>())?;
exception_module.add("NotFound", py.get_type_bound::<NotFoundError>())?;
exception_module.add(
"PermissionDenied",
py.get_type_bound::<PermissionDeniedError>(),
)?;
exception_module.add("IsADirectory", py.get_type_bound::<IsADirectoryError>())?;
exception_module.add("NotADirectory", py.get_type_bound::<NotADirectoryError>())?;
exception_module.add("AlreadyExists", py.get_type_bound::<AlreadyExistsError>())?;
exception_module.add("IsSameFile", py.get_type_bound::<IsSameFileError>())?;
exception_module.add(
"ConditionNotMatch",
py.get_type_bound::<ConditionNotMatchError>(),
)?;
let exception_module = PyModule::new(py, "exceptions")?;
exception_module.add("Error", py.get_type::<Error>())?;
exception_module.add("Unexpected", py.get_type::<UnexpectedError>())?;
exception_module.add("Unsupported", py.get_type::<UnsupportedError>())?;
exception_module.add("ConfigInvalid", py.get_type::<ConfigInvalidError>())?;
exception_module.add("NotFound", py.get_type::<NotFoundError>())?;
exception_module.add("PermissionDenied", py.get_type::<PermissionDeniedError>())?;
exception_module.add("IsADirectory", py.get_type::<IsADirectoryError>())?;
exception_module.add("NotADirectory", py.get_type::<NotADirectoryError>())?;
exception_module.add("AlreadyExists", py.get_type::<AlreadyExistsError>())?;
exception_module.add("IsSameFile", py.get_type::<IsSameFileError>())?;
exception_module.add("ConditionNotMatch", py.get_type::<ConditionNotMatchError>())?;
m.add_submodule(&exception_module)?;
py.import_bound("sys")?
py.import("sys")?
.getattr("modules")?
.set_item("opendal.exceptions", exception_module)?;
Ok(())
Expand Down
17 changes: 12 additions & 5 deletions bindings/python/src/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::Arc;

use futures::TryStreamExt;
use pyo3::exceptions::PyStopAsyncIteration;
use pyo3::prelude::*;
use pyo3::{prelude::*, IntoPyObjectExt};
use pyo3_async_runtimes::tokio::future_into_py;
use tokio::sync::Mutex;

Expand All @@ -42,7 +42,7 @@ impl BlockingLister {
}
fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult<Option<PyObject>> {
match slf.0.next() {
Some(Ok(entry)) => Ok(Some(Entry::new(entry).into_py(slf.py()))),
Some(Ok(entry)) => Ok(Some(Entry::new(entry).into_py_any(slf.py())?)),
Some(Err(err)) => {
let pyerr = format_pyerr(err);
Err(pyerr)
Expand Down Expand Up @@ -72,10 +72,17 @@ impl AsyncLister {
let mut lister = lister.lock().await;
let entry = lister.try_next().await.map_err(format_pyerr)?;
match entry {
Some(entry) => Ok(Python::with_gil(|py| Entry::new(entry).into_py(py))),
Some(entry) => Python::with_gil(|py| {
let py_obj = Entry::new(entry).into_py_any(py)?;
Ok(Some(py_obj))
}),
None => Err(PyStopAsyncIteration::new_err("stream exhausted")),
}
})?;
Ok(Some(fut.into()))
});

match fut {
Ok(fut) => Ok(Some(fut.into())),
Err(e) => Err(e),
}
}
}
23 changes: 13 additions & 10 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::types::PyDict;
use pyo3::types::PyTuple;
use pyo3::IntoPyObjectExt;
use pyo3_async_runtimes::tokio::future_into_py;

use crate::*;
Expand Down Expand Up @@ -228,10 +229,10 @@ impl Operator {
}

fn __getnewargs_ex__(&self, py: Python) -> PyResult<PyObject> {
let args = vec![self.__scheme.to_string().to_object(py)];
let args = PyTuple::new_bound(py, args);
let kwargs = self.__map.clone().into_py(py);
Ok(PyTuple::new_bound(py, [args.to_object(py), kwargs.to_object(py)]).to_object(py))
let args = vec![self.__scheme.to_string()];
let args = PyTuple::new(py, args)?.into_py_any(py)?;
let kwargs = self.__map.clone().into_py_any(py)?;
Ok(PyTuple::new(py, [args, kwargs])?.into_py_any(py)?)
}
}

Expand Down Expand Up @@ -434,7 +435,8 @@ impl AsyncOperator {
let this = self.core.clone();
future_into_py(py, async move {
let lister = this.lister(&path).await.map_err(format_pyerr)?;
let pylister: PyObject = Python::with_gil(|py| AsyncLister::new(lister).into_py(py));
let pylister = Python::with_gil(|py| AsyncLister::new(lister).into_py_any(py))?;

Ok(pylister)
})
}
Expand All @@ -448,7 +450,8 @@ impl AsyncOperator {
.recursive(true)
.await
.map_err(format_pyerr)?;
let pylister: PyObject = Python::with_gil(|py| AsyncLister::new(lister).into_py(py));
let pylister: PyObject =
Python::with_gil(|py| AsyncLister::new(lister).into_py_any(py))?;
Ok(pylister)
})
}
Expand Down Expand Up @@ -543,10 +546,10 @@ impl AsyncOperator {
}

fn __getnewargs_ex__(&self, py: Python) -> PyResult<PyObject> {
let args = vec![self.__scheme.to_string().to_object(py)];
let args = PyTuple::new_bound(py, args);
let kwargs = self.__map.clone().into_py(py);
Ok(PyTuple::new_bound(py, [args.to_object(py), kwargs.to_object(py)]).to_object(py))
let args = vec![self.__scheme.to_string()];
let args = PyTuple::new(py, args)?.into_py_any(py)?;
let kwargs = self.__map.clone().into_py_any(py)?;
Ok(PyTuple::new(py, [args, kwargs])?.into_py_any(py)?)
}
}

Expand Down
5 changes: 3 additions & 2 deletions bindings/python/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::os::raw::c_int;

use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;

/// A bytes-like object that implements buffer protocol.
#[pyclass(module = "opendal")]
Expand All @@ -33,14 +34,14 @@ impl Buffer {

/// Consume self to build a bytes
pub fn into_bytes(self, py: Python) -> PyResult<Py<PyAny>> {
let buffer = self.into_py(py);
let buffer = self.into_py_any(py)?;

unsafe { PyObject::from_owned_ptr_or_err(py, ffi::PyBytes_FromObject(buffer.as_ptr())) }
}

/// Consume self to build a bytes
pub fn into_bytes_ref(self, py: Python) -> PyResult<Bound<PyAny>> {
let buffer = self.into_py(py);
let buffer = self.into_py_any(py)?;
let view =
unsafe { Bound::from_owned_ptr_or_err(py, ffi::PyBytes_FromObject(buffer.as_ptr()))? };

Expand Down

0 comments on commit a686ba3

Please sign in to comment.