Skip to content

csv: csv.reader does not detect re-entrant iterator advancement #8323

Description

@widehyo1

Feature

When a csv.reader input iterator calls next() on the same reader from its
own __next__() method, the inner call advances the reader. RustPython then
allows the outer call to continue parsing with stale reader state instead of
raising csv.Error like CPython.

import csv


class ReentrantIterator:
    def __init__(self):
        self.reader = None
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.index += 1
        if self.index == 1:
            next(self.reader)
            return "a,b"
        if self.index == 2:
            return "x"
        raise StopIteration


iterator = ReentrantIterator()
reader = csv.reader(iterator)
iterator.reader = reader
print(next(reader))

# CPython main:
# _csv.Error: iterator has already advanced the reader
#
# RustPython:
# ['a', 'b']

CPython rejects the outer call once it detects that the re-entrant inner call
has already advanced the reader. This prevents the outer call from continuing
with parser state that no longer belongs to it.

Python Documentation or reference to CPython source code

Drafted with AI assistance (Codex).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions