Repository navigation
Fix JSONLIterator position queries on text streams - #519
theman6660 wants to merge 3 commits into
Conversation
vibhor-aggr
left a comment
There was a problem hiding this comment.
Forward reads become permanently exhausted after an EOF probe
Automated/AI-assisted review of 975629f84426b9b62b0a733c1e2579ad5c9d9ce4.
iter(self._file_obj.readline, empty) permanently exhausts the callable iterator when it first receives the sentinel. This changes the existing forward-reader behavior: after a caller probes EOF, records subsequently appended to the same JSONL file are never returned by that iterator. The position-query fix works, but append-only readers lose this behavior in both text and binary modes.
Minimal example (a separate writer appends; the reader is never repositioned):
from pathlib import Path
from tempfile import TemporaryDirectory
from boltons.jsonutils import JSONLIterator
with TemporaryDirectory() as directory:
path = Path(directory) / 'records.jsonl'
path.write_bytes(b'{"n": 1}\n')
with path.open(encoding='utf-8') as reader:
records = JSONLIterator(reader)
assert next(records) == {'n': 1}
assert next(records, None) is None
with path.open('ab') as writer:
writer.write(b'{"n": 2}\n')
assert next(records) == {'n': 2}This passes on upstream 4e5faa3d and raises StopIteration at the final line on this PR. The binary-mode version has the same result. The comparison was rerun outside both checkouts with assertions of the imported source path; the PR's existing 12 JSON utils tests still pass.
Suggested correction: drop the forward sentinel iterator and perform a direct readline() on each advance, raising StopIteration for that call when it returns empty. That avoids TextIOWrapper.__next__ disabling tell() without permanently discarding the reader:
if self._reverse:
line = next(self._line_iter)
else:
line = self._file_obj.readline()
if not line:
raise StopIteration
line = line.lstrip()I tested this local correction with both added append-after-EOF regressions and the position tests. It passes 683 tests/doctests plus 12 subtests under Python 3.14.5 / pytest 9.1.1; the unmodified PR passes 681 tests/doctests plus 12 subtests. Only Unix sockets and 127.0.0.1 were permitted for local socket tests; external connections remained blocked. Alternate Python/platform runtimes and a security audit were not verified. No competing PR or write to your branch was made.
|
@vibhor-aggr Fixed in c000374. I reproduced the append-after-EOF regression in both text and binary mode: both new cases failed on the previous PR head. The forward path now calls Validation on Windows / Python 3.12.10:
The final validation runs disabled unrelated globally installed pytest plugins with |
|
Folded into #609 (see the PR description for credit). Thanks for the report and the repro. |
The change
Forward iteration over a text stream calls
TextIOWrapper.__next__, which disablestell(). As a result, queryingJSONLIterator.cur_byte_posafter reading a record raisesOSError: telling position disabled by next() call.Call
readline()directly on each forward advance and raiseStopIterationfor that call when it returns empty. This preserves position queries in text and binary modes and allows the same reader to resume after new records are appended following an EOF probe. Regression tests cover text and binary streams, UTF-8 content, CRLF line endings, positions after records and EOF, invalid lines, and repeated EOF probes followed by multiple appends through a separate writer.Validation executed by Codex:
The backstory
I'm a computer science undergraduate looking to contribute small, verifiable fixes to established open-source projects. I asked Codex to help find suitable problems outside my existing Cline contributions; that investigation found this interaction between text-file iteration and the iterator's position property. This was found during contribution-oriented investigation rather than in a production application.
AI assistance
Follow-up prompt for the EOF regression: “有一个pr有回复了解决一下” (a PR received a reply; please address it).