Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade pyo3 to 0.23 #5368

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 8 additions & 4 deletions bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,14 @@ 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_pyobject(py).map_err(Into::into))
.map(|pyobj| pyobj.into_any())
}

/// Return the current stream position.
Expand All @@ -495,8 +497,10 @@ 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_pyobject(py).map_err(Into::into))
.map(|pyobj| pyobj.into_any())
}

fn close<'p>(&'p mut self, py: Python<'p>) -> PyResult<Bound<PyAny>> {
Expand All @@ -514,7 +518,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_pyobject(py)?.into_any().unbind();
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
20 changes: 16 additions & 4 deletions bindings/python/src/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ 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_pyobject(slf.py())?
.into_any()
.unbind(),
XmchxUp marked this conversation as resolved.
Show resolved Hide resolved
)),
Some(Err(err)) => {
let pyerr = format_pyerr(err);
Err(pyerr)
Expand Down Expand Up @@ -72,10 +77,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_pyobject(py)?.into_any().unbind();
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),
}
}
}
32 changes: 22 additions & 10 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,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_any().unbind();
let kwargs = self.__map.clone().into_pyobject(py)?.into_any().unbind();
Ok(PyTuple::new(py, [args, kwargs])?.into_any().unbind())
}
}

Expand Down Expand Up @@ -434,7 +434,14 @@ 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_pyobject(py)
.map_err(|err| PyErr::new::<pyo3::exceptions::PyException, _>(err.to_string()))
.map(|py_obj| py_obj.into_any().unbind())
})?;

Ok(pylister)
})
}
Expand All @@ -448,7 +455,12 @@ 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_pyobject(py)
.map_err(|err| PyErr::new::<pyo3::exceptions::PyException, _>(err.to_string()))
.map(|py_obj| py_obj.into_any().unbind())
})?;
Ok(pylister)
})
}
Expand Down Expand Up @@ -543,10 +555,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_any().unbind();
let kwargs = self.__map.clone().into_pyobject(py)?.into_any().unbind();
Ok(PyTuple::new(py, [args, kwargs])?.into_any().unbind())
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/python/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,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_pyobject(py)?.into_any().unbind();

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_pyobject(py)?.into_any().unbind();
let view =
unsafe { Bound::from_owned_ptr_or_err(py, ffi::PyBytes_FromObject(buffer.as_ptr()))? };

Expand Down