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

refactor(bindings/python): return bytes directly and add type stub file #1514

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions bindings/python/opendal.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Operator:
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
def __init__(scheme: str, **kwargs): ...
def read(self, path: str) -> bytes: ...
def write(self, path: str, bs: bytes): ...
def stat(self, path: str) -> Metadata: ...

class AsyncOperator:
def __init__(scheme: str, **kwargs): ...
async def read(self, path: str) -> bytes: ...
async def write(self, path: str, bs: bytes): ...
async def stat(self, path: str) -> Metadata: ...

class Metadata:
def content_length(self) -> int: ...
18 changes: 10 additions & 8 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use std::collections::HashMap;
use std::str::FromStr;

use ::opendal as od;
use pyo3::exceptions::PyBaseException;
use pyo3::exceptions::PyFileNotFoundError;
use pyo3::exceptions::{PyFileNotFoundError, PyRuntimeError};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyBytes, PyDict};
use pyo3_asyncio::tokio::future_into_py;

fn build_operator(scheme: od::Scheme, map: HashMap<String, String>) -> PyResult<od::Operator> {
Expand Down Expand Up @@ -65,8 +64,9 @@ impl AsyncOperator {
let this = self.0.clone();
let path = path.to_string();
future_into_py(py, async move {
let res: Vec<u8> = this.read(&path).await.map_err(format_pyerr)?;
Ok(res)
let res = this.read(&path).await.map_err(format_pyerr)?;
let bytes = Python::with_gil(|py| PyBytes::new(py, &res).to_object(py));
Ok(bytes)
})
}

Expand Down Expand Up @@ -112,8 +112,10 @@ impl Operator {
Ok(Operator(build_operator(scheme, map)?.blocking()))
}

pub fn read(&self, path: &str) -> PyResult<Vec<u8>> {
self.0.read(path).map_err(format_pyerr)
pub fn read<'p>(&'p self, py: Python<'p>, path: &str) -> PyResult<&'p PyBytes> {
let res = self.0.read(path).map_err(format_pyerr)?;
let bytes = PyBytes::new(py, &res);
Ok(bytes)
}

pub fn write(&self, path: &str, bs: Vec<u8>) -> PyResult<()> {
Expand All @@ -139,7 +141,7 @@ fn format_pyerr(err: od::Error) -> PyErr {
use od::ErrorKind::*;
match err.kind() {
NotFound => PyFileNotFoundError::new_err(err.to_string()),
_ => PyBaseException::new_err(err.to_string()),
_ => PyRuntimeError::new_err(err.to_string()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/python/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_blocking():
op = opendal.Operator("memory")
op.write("test", b"Hello, World!")
bs = op.read("test")
print(bytes(bs).decode("utf-8"))
print(bs.decode("utf-8"))
meta = op.stat("test")
print(f"content_length: {meta.content_length()}")

Expand All @@ -31,7 +31,7 @@ async def test_async():
op = opendal.AsyncOperator("memory")
await op.write("test", b"Hello, World!")
bs = await op.read("test")
print(bytes(bs).decode("utf-8"))
print(bs.decode("utf-8"))
meta = await op.stat("test")
print(f"content_length: {meta.content_length()}")

Expand Down