messense commented on code in PR #3474:
URL:
https://github.com/apache/incubator-opendal/pull/3474#discussion_r1382307159
##########
bindings/python/src/file.rs:
##########
@@ -99,88 +123,110 @@ impl Reader {
/// Return the new absolute position.
#[pyo3(signature = (pos, whence = 0))]
pub fn seek(&mut self, pos: i64, whence: u8) -> PyResult<u64> {
+ let reader = match &mut self.0 {
+ FileState::Reader(r) => r,
+ FileState::Writer(_) => {
+ return Err(PyIOError::new_err(
+ "I/O operation failed for reading on write only file.",
+ ));
+ }
+ FileState::Closed => {
+ return Err(PyIOError::new_err(
+ "I/O operation failed for reading on closed file.",
+ ));
+ }
+ };
+
let whence = match whence {
0 => SeekFrom::Start(pos as u64),
1 => SeekFrom::Current(pos),
2 => SeekFrom::End(pos),
_ => return Err(PyValueError::new_err("invalid whence")),
};
- let reader = self.as_mut()?;
+
reader
.seek(whence)
.map_err(|err| PyIOError::new_err(err.to_string()))
}
/// Return the current stream position.
pub fn tell(&mut self) -> PyResult<u64> {
- let reader = self.as_mut()?;
+ let reader = match &mut self.0 {
+ FileState::Reader(r) => r,
+ FileState::Writer(_) => {
+ return Err(PyIOError::new_err(
+ "I/O operation failed for reading on write only file.",
+ ));
+ }
+ FileState::Closed => {
+ return Err(PyIOError::new_err(
+ "I/O operation failed for reading on closed file.",
+ ));
+ }
+ };
+
reader
.stream_position()
.map_err(|err| PyIOError::new_err(err.to_string()))
}
+ fn close(&mut self) -> PyResult<()> {
+ self.0 = FileState::Closed;
+ Ok(())
+ }
+
pub fn __enter__(slf: Py<Self>) -> Py<Self> {
slf
}
pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject,
_traceback: PyObject) {
- drop(self.0.take());
+ self.0 = FileState::Closed;
}
}
-enum ReaderState {
- Init {
- operator: ocore::Operator,
- path: String,
- },
- Open(ocore::Reader),
+/// A file-like async reader.
+/// Can be used as an async context manager.
+#[pyclass(module = "opendal")]
+pub struct AsyncFile(Arc<Mutex<AsyncFileState>>);
Review Comment:
We might be able to use
[`GILProtected`](https://docs.rs/pyo3/0.19.2/pyo3/sync/struct.GILProtected.html)
instead of `Mutex` here since python object access is already synchronized
with Python GIL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]