Skip to content

Commit

Permalink
fix: python binding kwargs parsing (#5458)
Browse files Browse the repository at this point in the history
* fix: python binding kwargs parsing

* add for test
  • Loading branch information
trim21 authored Dec 26, 2024
1 parent 4aa0e57 commit 93f3aa1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ doc = false

[dependencies]
bytes = "1.5.0"
dict_derive = "0.6.0"
futures = "0.3.28"
# this crate won't be published, we always use the local version
opendal = { version = ">=0", path = "../../core", features = [
Expand Down
9 changes: 7 additions & 2 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ impl Operator {
#[pyo3(signature = (path, bs, **kwargs))]
pub fn write(&self, path: &str, bs: Vec<u8>, kwargs: Option<WriteOptions>) -> PyResult<()> {
let kwargs = kwargs.unwrap_or_default();
let mut write = self.core.write_with(path, bs).append(kwargs.append);
let mut write = self
.core
.write_with(path, bs)
.append(kwargs.append.unwrap_or(false));
if let Some(chunk) = kwargs.chunk {
write = write.chunk(chunk);
}
Expand Down Expand Up @@ -333,7 +336,9 @@ impl AsyncOperator {
let this = self.core.clone();
let bs = bs.as_bytes().to_vec();
future_into_py(py, async move {
let mut write = this.write_with(&path, bs).append(kwargs.append);
let mut write = this
.write_with(&path, bs)
.append(kwargs.append.unwrap_or(false));
if let Some(buffer) = kwargs.chunk {
write = write.chunk(buffer);
}
Expand Down
5 changes: 3 additions & 2 deletions bindings/python/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
// specific language governing permissions and limitations
// under the License.

use pyo3::{pyclass, FromPyObject};
use dict_derive::FromPyObject;
use pyo3::pyclass;

#[pyclass(module = "opendal")]
#[derive(FromPyObject, Default)]
pub struct WriteOptions {
pub append: bool,
pub append: Option<bool>,
pub chunk: Option<usize>,
pub content_type: Option<String>,
pub content_disposition: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_sync_write(service_name, operator, async_operator):
filename = f"test_file_{str(uuid4())}.txt"
content = os.urandom(size)
size = len(content)
operator.write(filename, content)
operator.write(filename, content, content_type="text/plain")
metadata = operator.stat(filename)
assert metadata is not None
assert metadata.mode.is_file()
Expand Down

0 comments on commit 93f3aa1

Please sign in to comment.