Skip to content

Commit cad1023

Browse files
committed
Iterator
1 parent c4ebaa8 commit cad1023

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Lib/test/test_io.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4276,10 +4276,6 @@ def test_reconfigure_write_through(self):
42764276
def test_repr(self):
42774277
return super().test_repr()
42784278

4279-
@unittest.expectedFailure # TODO: RUSTPYTHON
4280-
def test_telling(self):
4281-
return super().test_telling()
4282-
42834279
@unittest.expectedFailure # TODO: RUSTPYTHON
42844280
def test_uninitialized(self):
42854281
return super().test_uninitialized()

crates/vm/src/stdlib/io.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,7 @@ mod _io {
24732473
vm.call_method(&textio.buffer, "flush", ())
24742474
}
24752475

2476-
#[pyclass(with(Constructor, Initializer, Destructor), flags(BASETYPE))]
2476+
#[pyclass(with(Constructor, Initializer, Destructor, Iterable, IterNext), flags(BASETYPE))]
24772477
impl TextIOWrapper {
24782478
#[pymethod]
24792479
fn reconfigure(&self, args: TextIOWrapperArgs, vm: &VirtualMachine) -> PyResult<()> {
@@ -3348,6 +3348,44 @@ mod _io {
33483348
}
33493349
}
33503350

3351+
impl Iterable for TextIOWrapper {
3352+
fn slot_iter(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
3353+
check_closed(&zelf, vm)?;
3354+
Ok(zelf)
3355+
}
3356+
3357+
fn iter(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyResult {
3358+
unreachable!("slot_iter is implemented")
3359+
}
3360+
}
3361+
3362+
impl IterNext for TextIOWrapper {
3363+
fn slot_iternext(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
3364+
// Set telling = false during iteration (matches CPython behavior)
3365+
let textio_ref: PyRef<TextIOWrapper> = zelf.downcast_ref::<TextIOWrapper>().unwrap().to_owned();
3366+
{
3367+
let mut textio = textio_ref.lock(vm)?;
3368+
textio.telling = false;
3369+
}
3370+
3371+
let line = vm.call_method(zelf, "readline", ())?;
3372+
3373+
if !line.clone().try_to_bool(vm)? {
3374+
// Restore telling on StopIteration
3375+
let mut textio = textio_ref.lock(vm)?;
3376+
textio.snapshot = None;
3377+
textio.telling = textio.seekable;
3378+
Ok(PyIterReturn::StopIteration(None))
3379+
} else {
3380+
Ok(PyIterReturn::Return(line))
3381+
}
3382+
}
3383+
3384+
fn next(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyIterReturn> {
3385+
unreachable!("slot_iternext is implemented")
3386+
}
3387+
}
3388+
33513389
#[pyattr]
33523390
#[pyclass(name)]
33533391
#[derive(Debug, PyPayload, Default)]

0 commit comments

Comments
 (0)