Skip to content

Commit

Permalink
fix(bindings/python): sync writer exit close raise error (#4127)
Browse files Browse the repository at this point in the history
  • Loading branch information
suyanhanx authored Feb 1, 2024
1 parent f05b94c commit f4a3027
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
13 changes: 9 additions & 4 deletions bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

// Remove this allow after <https://github.com/rust-lang/rust-clippy/issues/12039> fixed.
// Remove this `allow` after <https://github.com/rust-lang/rust-clippy/issues/12039> fixed.
#![allow(clippy::unnecessary_fallible_conversions)]

use std::io::Read;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
27 changes: 26 additions & 1 deletion bindings/python/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f4a3027

Please sign in to comment.