You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2026/08/27 06:08:03 UTC

(opendal) 01/01: fix(bindings/python): fix sized readline

This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-python-readline-size
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit c419222bfeae71e61f6d0f99d810a0fb0466c5c2
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Thu Aug 27 14:07:37 2026 +0800

    fix(bindings/python): fix sized readline
---
 bindings/python/src/file.rs        |  9 ++++-----
 bindings/python/tests/test_read.py | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/bindings/python/src/file.rs b/bindings/python/src/file.rs
index 8d5cd50a7..f73ff39e3 100644
--- a/bindings/python/src/file.rs
+++ b/bindings/python/src/file.rs
@@ -164,13 +164,12 @@ impl File {
                 buffer
             }
             Some(size) => {
-                let mut bs = vec![0; size];
+                let mut buffer = Vec::new();
                 let mut reader = reader.take(size as u64);
-                let n = reader
-                    .read_until(b'\n', &mut bs)
+                reader
+                    .read_until(b'\n', &mut buffer)
                     .map_err(|err| PyIOError::new_err(err.to_string()))?;
-                bs.truncate(n);
-                bs
+                buffer
             }
         };
 
diff --git a/bindings/python/tests/test_read.py b/bindings/python/tests/test_read.py
index 002be4ae2..4ffbe0617 100644
--- a/bindings/python/tests/test_read.py
+++ b/bindings/python/tests/test_read.py
@@ -118,6 +118,20 @@ def test_sync_reader_readline(service_name, operator, async_operator):
     operator.delete(filename)
 
 
+@pytest.mark.need_capability("read", "write", "delete")
+def test_sync_reader_readline_with_size(operator):
+    filename = f"random_file_{str(uuid4())}"
+    content = b"alpha\nbeta\ngamma"
+    operator.write(filename, content)
+
+    expected = io.BytesIO(content)
+    with operator.open(filename, "rb") as reader:
+        for size in [3, 10, 0, 5, 2, 10, 10]:
+            assert reader.readline(size) == expected.readline(size)
+
+    operator.delete(filename)
+
+
 @pytest.mark.asyncio
 @pytest.mark.need_capability("read", "write", "delete")
 async def test_async_read(service_name, operator, async_operator):