Skip to content

Commit

Permalink
fix(bindings/python): Fix the semantic of size argument for python …
Browse files Browse the repository at this point in the history
…`File::read`
  • Loading branch information
reswqa committed Mar 14, 2024
1 parent 7533627 commit 05ba060
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl File {

#[pymethods]
impl File {
/// Read and return size bytes, or if size is not given, until EOF.
/// Read and return at most size bytes, or if size is not given, until EOF.
#[pyo3(signature = (size=None,))]
pub fn read<'p>(&'p mut self, py: Python<'p>, size: Option<usize>) -> PyResult<&'p PyAny> {
let reader = match &mut self.0 {
Expand All @@ -76,8 +76,7 @@ impl File {

let buffer = match size {
Some(size) => {
let bs = reader
.read_exact(size)
let bs = reader.read(size)
.map_err(|err| PyIOError::new_err(err.to_string()))?;
bs.to_vec()
}
Expand Down Expand Up @@ -218,7 +217,7 @@ impl AsyncFile {

#[pymethods]
impl AsyncFile {
/// Read and return size bytes, or if size is not given, until EOF.
/// Read and return at most size bytes, or if size is not given, until EOF.
pub fn read<'p>(&'p self, py: Python<'p>, size: Option<usize>) -> PyResult<&'p PyAny> {
let state = self.0.clone();

Expand All @@ -241,7 +240,7 @@ impl AsyncFile {
let buffer = match size {
Some(size) => {
let buffer = reader
.read_exact(size)
.read(size)
.await
.map_err(|err| PyIOError::new_err(err.to_string()))?;
buffer.to_vec()
Expand Down
10 changes: 10 additions & 0 deletions bindings/python/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def test_sync_reader(service_name, operator, async_operator):
assert read_content is not None
assert read_content == content

with operator.open(filename, "rb") as reader:
read_content = reader.read(size + 1)
assert read_content is not None
assert read_content == content

operator.delete(filename)


Expand Down Expand Up @@ -80,6 +85,11 @@ async def test_async_reader(service_name, operator, async_operator):
assert read_content is not None
assert read_content == content

async with await async_operator.open(filename, "rb") as reader:
read_content = await reader.read(size + 1)
assert read_content is not None
assert read_content == content

await async_operator.delete(filename)


Expand Down

0 comments on commit 05ba060

Please sign in to comment.