From 568aa5468567f2b03c7af64e1f301e02818c9cc2 Mon Sep 17 00:00:00 2001 From: suyanhanx Date: Thu, 1 Feb 2024 21:05:20 +0800 Subject: [PATCH] fix(bindings/python): sync writer exit close raise error Signed-off-by: suyanhanx --- bindings/python/src/file.rs | 13 +++++++++---- bindings/python/tests/conftest.py | 2 +- bindings/python/tests/test_write.py | 27 ++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/bindings/python/src/file.rs b/bindings/python/src/file.rs index b7db1997d562..692bb4896dba 100644 --- a/bindings/python/src/file.rs +++ b/bindings/python/src/file.rs @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -// Remove this allow after fixed. +// Remove this `allow` after fixed. #![allow(clippy::unnecessary_fallible_conversions)] use std::io::Read; @@ -118,7 +118,7 @@ impl File { } /// Change the stream position to the given byte offset. - /// offset is interpreted relative to the position indicated by `whence`. + /// Offset is interpreted relative to the position indicated by `whence`. /// The default value for whence is `SEEK_SET`. Values for `whence` are: /// /// * `SEEK_SET` or `0` – start of the stream (the default); offset should be zero or positive @@ -188,8 +188,13 @@ impl File { slf } - pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) { - let _ = self.close(); + pub fn __exit__( + &mut self, + _exc_type: PyObject, + _exc_value: PyObject, + _traceback: PyObject, + ) -> PyResult<()> { + self.close() } } diff --git a/bindings/python/tests/conftest.py b/bindings/python/tests/conftest.py index 5e0f4ccfb4b4..86fa77f8a968 100644 --- a/bindings/python/tests/conftest.py +++ b/bindings/python/tests/conftest.py @@ -54,7 +54,7 @@ def setup_config(service_name): True if os.environ.get("OPENDAL_DISABLE_RANDOM_ROOT") == "true" else False ) if not disable_random_root: - config["root"] = f"{config.get('root', '/')}{str(uuid4())}/" + config["root"] = f"{config.get('root', '/')}/{str(uuid4())}/" return config diff --git a/bindings/python/tests/test_write.py b/bindings/python/tests/test_write.py index e98c6e959be9..7d01910eaf5b 100644 --- a/bindings/python/tests/test_write.py +++ b/bindings/python/tests/test_write.py @@ -99,4 +99,29 @@ async def test_async_delete(service_name, operator, async_operator): await async_operator.write(filename, content) await async_operator.delete(filename) with pytest.raises(NotFound): - await operator.stat(filename) + await async_operator.stat(filename) + +@pytest.mark.asyncio +@pytest.mark.need_capability("write", "delete") +async def test_async_writer(service_name, operator, async_operator): + size = randint(1, 1024) + filename = f"test_file_{str(uuid4())}.txt" + content = os.urandom(size) + f = await async_operator.open(filename, "wb") + await f.write(content) + await f.close() + await async_operator.delete(filename) + with pytest.raises(NotFound): + await async_operator.stat(filename) + +@pytest.mark.need_capability("write", "delete") +def test_sync_writer(service_name, operator, async_operator): + size = randint(1, 1024) + filename = f"test_file_{str(uuid4())}.txt" + content = os.urandom(size) + f = operator.open(filename, "wb") + f.write(content) + f.close() + operator.delete(filename) + with pytest.raises(NotFound): + operator.stat(filename)