Skip to content

Commit

Permalink
fix(bindings/python): missed to call close for the file internally (#…
Browse files Browse the repository at this point in the history
…4122)

* fix(bindings/python): missed to call close for the file internally (#4120)

* style: cargo fmt

* style: clippy check
  • Loading branch information
zzl221000 authored Feb 1, 2024
1 parent 66595d0 commit f05b94c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ impl File {
}

fn close(&mut self) -> PyResult<()> {
if let FileState::Writer(w) = &mut self.0 {
w.close()
.map_err(|err| PyIOError::new_err(err.to_string()))?;
};
self.0 = FileState::Closed;
Ok(())
}
Expand All @@ -185,7 +189,7 @@ impl File {
}

pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) {
self.0 = FileState::Closed;
let _ = self.close();
}
}

Expand Down Expand Up @@ -361,6 +365,11 @@ impl AsyncFile {
let state = self.0.clone();
future_into_py(py, async move {
let mut state = state.lock().await;
if let AsyncFileState::Writer(w) = &mut *state {
w.close()
.await
.map_err(|err| PyIOError::new_err(err.to_string()))?;
}
*state = AsyncFileState::Closed;
Ok(())
})
Expand All @@ -381,6 +390,11 @@ impl AsyncFile {
let state = self.0.clone();
future_into_py(py, async move {
let mut state = state.lock().await;
if let AsyncFileState::Writer(w) = &mut *state {
w.close()
.await
.map_err(|err| PyIOError::new_err(err.to_string()))?;
}
*state = AsyncFileState::Closed;
Ok(())
})
Expand Down

0 comments on commit f05b94c

Please sign in to comment.