Skip to content

Commit de11481

Browse files
committed
Iterator
1 parent 98bed0a commit de11481

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎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: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,10 @@ mod _io {
24732473
vm.call_method(&textio.buffer, "flush", ())
24742474
}
24752475

2476-
#[pyclass(with(Constructor, Initializer, Destructor), flags(BASETYPE))]
2476+
#[pyclass(
2477+
with(Constructor, Initializer, Destructor, Iterable, IterNext),
2478+
flags(BASETYPE)
2479+
)]
24772480
impl TextIOWrapper {
24782481
#[pymethod]
24792482
fn reconfigure(&self, args: TextIOWrapperArgs, vm: &VirtualMachine) -> PyResult<()> {
@@ -3348,6 +3351,45 @@ mod _io {
33483351
}
33493352
}
33503353

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

0 commit comments

Comments
 (0)