Skip to content
Merged
Show file tree
Hide file tree
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
Iterator
  • Loading branch information
youknowone committed Dec 10, 2025
commit de11481072a67b9e28099327a557be67d2be214a
4 changes: 0 additions & 4 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4276,10 +4276,6 @@ def test_reconfigure_write_through(self):
def test_repr(self):
return super().test_repr()

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_telling(self):
return super().test_telling()

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_uninitialized(self):
return super().test_uninitialized()
Expand Down
44 changes: 43 additions & 1 deletion crates/vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,10 @@ mod _io {
vm.call_method(&textio.buffer, "flush", ())
}

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

impl Iterable for TextIOWrapper {
fn slot_iter(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
check_closed(&zelf, vm)?;
Ok(zelf)
}

fn iter(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyResult {
unreachable!("slot_iter is implemented")
}
}

impl IterNext for TextIOWrapper {
fn slot_iternext(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
// Set telling = false during iteration (matches CPython behavior)
let textio_ref: PyRef<TextIOWrapper> =
zelf.downcast_ref::<TextIOWrapper>().unwrap().to_owned();
{
let mut textio = textio_ref.lock(vm)?;
textio.telling = false;
}

let line = vm.call_method(zelf, "readline", ())?;

if !line.clone().try_to_bool(vm)? {
// Restore telling on StopIteration
let mut textio = textio_ref.lock(vm)?;
textio.snapshot = None;
textio.telling = textio.seekable;
Ok(PyIterReturn::StopIteration(None))
} else {
Ok(PyIterReturn::Return(line))
}
}

fn next(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyIterReturn> {
unreachable!("slot_iternext is implemented")
}
}

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