Skip to content

Fix JSONLIterator position queries on text streams - #519

Closed
theman6660 wants to merge 3 commits into
mahmoud:masterfrom
theman6660:fix/jsonl-text-position
Closed

theman6660 wants to merge 3 commits into
mahmoud:masterfrom
theman6660:fix/jsonl-text-position

Conversation

@theman6660

@theman6660 theman6660 commented Sep 30, 2026 •

Copy link
Copy Markdown

The change

Forward iteration over a text stream calls TextIOWrapper.__next__, which disables tell(). As a result, querying JSONLIterator.cur_byte_pos after reading a record raises OSError: telling position disabled by next() call.

Call readline() directly on each forward advance and raise StopIteration for 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:

  • Focused JSON utils tests: 14 passed (Windows, Python 3.12.10).
  • Full tests and doctests: 683 passed, 12 subtests passed (Windows, Python 3.12.10).
  • Direct reproduction against upstream raises the position-query error; the same sample on this branch returns position 9.

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

  • Harness/tooling: Codex (ChatGPT Work).
  • Model(s): GPT-6 for current validation; earlier implementation model not retained.
  • Original prompt(s): “再去找其他仓库给我工作去” (find work in other repositories), “继续” (continue).

Follow-up prompt for the EOF regression: “有一个pr有回复了解决一下” (a PR received a reply; please address it).

@vibhor-aggr vibhor-aggr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@theman6660

Copy link
Copy Markdown
Author

@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 readline() directly on each advance and raises StopIteration only for that empty read. This preserves tell() without retaining an exhausted sentinel iterator. The new regression test probes EOF twice, appends through a separate writer without repositioning the reader, and repeats that cycle for a second append; it also checks cur_byte_pos throughout, in both modes.

Validation on Windows / Python 3.12.10:

  • python -m pytest tests/test_jsonutils.py -q: 14 passed.
  • python -m pytest --doctest-modules boltons tests -q: 683 passed, 12 subtests passed.
  • git diff --check: passed.

The final validation runs disabled unrelated globally installed pytest plugins with PYTEST_DISABLE_PLUGIN_AUTOLOAD=1. The PR description has also been updated to describe the direct-read implementation and current results. This follow-up was implemented and validated with Codex assistance.

@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants