Skip to content

python: propagate the real error for unseekable streams in identify_stream (#1473) - #1475

Open
kaluli123123 wants to merge 8 commits into
google:mainfrom
kaluli123123:fix/identify-stream-unseekable-position
Open

kaluli123123 wants to merge 8 commits into
google:mainfrom
kaluli123123:fix/identify-stream-unseekable-position

Conversation

@kaluli123123

@kaluli123123 kaluli123123 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1473.

Keep tell() outside the restoring try/finally so unseekable streams retain their original error. Merged current main (8461733) and adapted the regression annotation to ty; upstream block-based reads remain unchanged.

Python 3.13: both stream regressions and the non-slow suite passed (42 passed, 1 skipped); lock, Ruff, formatting, ty, copyright, documentation, KB sync and real OS-pipe/CLI/module smokes passed.

Current-head workflows need maintainer authorization; formal approval is still required.

…tream

`identify_stream()` bound `current_position` inside the `try` block but
referenced it unconditionally in `finally`. For a readable-but-unseekable
binary stream (e.g. `sys.stdin.buffer` on a pipe) `stream.tell()` raises,
so the name was never bound and `finally` raised `UnboundLocalError`,
demoting the actionable `OSError` to `__context__`.

Hoist `tell()` out of the `try` block: there is nothing to restore until
the position is known, and the only statement left inside `try` is the one
that needs the restore.

Fixes google#1473
Resolve the conflict in identify_stream() against the new
stream-reading logic from main: keep main's block-based read, and
keep this PR's fix of calling tell() before the try block so the
real error of an unseekable stream is not masked by an
UnboundLocalError.
@kaluli123123

Copy link
Copy Markdown
Contributor Author

Merged current main (2790535) to resolve the conflict with the new PyO3-based identify_stream(). The resolution keeps main's block-based read and keeps this PR's fix of calling tell() before the try block; main still has the original bug (the new test fails there with UnboundLocalError).

Checks run locally in python/ after uv sync: pytest tests -m "not slow" (40 passed, 1 skipped), ruff check, ruff format --check, and mypy src tests all pass.

@ebursztein

Copy link
Copy Markdown
Collaborator

Thank you! I checked this against the current main (Python on the Rust library): it still merges cleanly, the bug still reproduces without it, and the full Python suite passes with it, including your new test. It's listed in #1537 as ready to merge.

@reyammer
reyammer enabled auto-merge (squash) November 12, 2026 08:38
@kaluli123123

Copy link
Copy Markdown
Contributor Author

Updated the branch with current main (bf5245f) in 4491d63 because the PR was behind its base. The merge was clean; the diff against main is unchanged (python/src/magika/magika.py +5/-1 and the new regression test).

Local checks in python/ after uv sync: pytest tests -m "not slow" (42 passed, 1 skipped), ruff check, ruff format --check, and ty check all pass. CI is rerunning on the new head.

@kaluli123123

Copy link
Copy Markdown
Contributor Author

Rebased-free update: merged the current main into this branch with a merge commit (no rebase, no force-push), so the branch is no longer behind.

  • Merged base: 9f2f5281a215f71ebfab72f90637df541e4a7113 (origin/main)
  • New head: 2867c9115d310fe5dc8292acd51d3396e0a4008a
  • No conflicts; the fix itself is unchanged.

Validation run locally in python/ at the new head (commands from python/justfile):

  • uv run pytest tests -m "not slow" -q -> 42 passed, 1 skipped, 1 deselected in 1.21s
  • uv lock --check -> resolved, up to date
  • uv run ruff check -> All checks passed!
  • uv run ruff format --check -> 23 files already formatted
  • uv run ty check -> All checks passed!
  • uv run ../scripts/check_copyright.py -> All files have valid copyright.

After the push, mergeStateStatus is no longer BEHIND; GitHub reports MERGEABLE / BLOCKED and the visible checks are check-changes: pass, cla/google: pass, with the zizmor jobs skipping. The remaining workflows appear to need maintainer approval to run on this fork branch.

@kaluli123123

Copy link
Copy Markdown
Contributor Author

Synced with latest main (merge commit, no conflicts). New head: 38531e7. The branch is no longer behind the base.

Local verification in python/ with uv:

  • uv sync --locked --all-extras --dev — lockfile in sync
  • uv run ruff check — All checks passed!
  • uv run ruff format --check — 23 files already formatted
  • uv run ty check — All checks passed!
  • uv run pytest tests -m "not slow" — 42 passed, 1 skipped, 1 deselected

The fix and its regression test are unchanged by the merge.

This branch has not been deployed

No deployments
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.

python: identify_stream() raises UnboundLocalError instead of the real OSError on unseekable streams

3 participants