Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IO Desctructors
  • Loading branch information
youknowone committed Dec 10, 2025
commit 98bed0a7bb8e2be702ca61d797b8efc5cac08d8c
82 changes: 77 additions & 5 deletions crates/vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1753,11 +1753,23 @@ mod _io {
}

#[pyclass(
with(Constructor, BufferedMixin, BufferedReadable),
with(Constructor, BufferedMixin, BufferedReadable, Destructor),
flags(BASETYPE, HAS_DICT)
)]
impl BufferedReader {}

impl Destructor for BufferedReader {
fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let _ = vm.call_method(zelf, "close", ());
Ok(())
}

#[cold]
fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_del is implemented")
}
}

impl DefaultConstructor for BufferedReader {}

#[pyclass]
Expand Down Expand Up @@ -1810,11 +1822,23 @@ mod _io {
}

#[pyclass(
with(Constructor, BufferedMixin, BufferedWritable),
with(Constructor, BufferedMixin, BufferedWritable, Destructor),
flags(BASETYPE, HAS_DICT)
)]
impl BufferedWriter {}

impl Destructor for BufferedWriter {
fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let _ = vm.call_method(zelf, "close", ());
Ok(())
}

#[cold]
fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_del is implemented")
}
}

impl DefaultConstructor for BufferedWriter {}

#[pyattr]
Expand Down Expand Up @@ -1852,11 +1876,29 @@ mod _io {
}

#[pyclass(
with(Constructor, BufferedMixin, BufferedReadable, BufferedWritable),
with(
Constructor,
BufferedMixin,
BufferedReadable,
BufferedWritable,
Destructor
),
flags(BASETYPE, HAS_DICT)
)]
impl BufferedRandom {}

impl Destructor for BufferedRandom {
fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let _ = vm.call_method(zelf, "close", ());
Ok(())
}

#[cold]
fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_del is implemented")
}
}

impl DefaultConstructor for BufferedRandom {}

#[pyattr]
Expand Down Expand Up @@ -1900,7 +1942,13 @@ mod _io {
}

#[pyclass(
with(Constructor, Initializer, BufferedReadable, BufferedWritable),
with(
Constructor,
Initializer,
BufferedReadable,
BufferedWritable,
Destructor
),
flags(BASETYPE, HAS_DICT)
)]
impl BufferedRWPair {
Expand Down Expand Up @@ -1942,6 +1990,18 @@ mod _io {
}
}

impl Destructor for BufferedRWPair {
fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let _ = vm.call_method(zelf, "close", ());
Ok(())
}

#[cold]
fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_del is implemented")
}
}

#[derive(FromArgs)]
struct TextIOWrapperArgs {
#[pyarg(any, default)]
Expand Down Expand Up @@ -2413,7 +2473,7 @@ mod _io {
vm.call_method(&textio.buffer, "flush", ())
}

#[pyclass(with(Constructor, Initializer), flags(BASETYPE))]
#[pyclass(with(Constructor, Initializer, Destructor), flags(BASETYPE))]
impl TextIOWrapper {
#[pymethod]
fn reconfigure(&self, args: TextIOWrapperArgs, vm: &VirtualMachine) -> PyResult<()> {
Expand Down Expand Up @@ -3276,6 +3336,18 @@ mod _io {
}
}

impl Destructor for TextIOWrapper {
fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let _ = vm.call_method(zelf, "close", ());
Ok(())
}

#[cold]
fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_del is implemented")
}
}

#[pyattr]
#[pyclass(name)]
#[derive(Debug, PyPayload, Default)]
Expand Down