Skip to content

Fix encoding declarations - #325

Merged
seddonym merged 6 commits into
mainfrom
fix-encoding-declarations
Oct 12, 2026
Merged

seddonym merged 6 commits into
mainfrom
fix-encoding-declarations

Conversation

@seddonym

@seddonym seddonym commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #322, with a couple of tweaks.

@codspeed

codspeed Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing fix-encoding-declarations (a87c498) with main (ce4cceb)

Open in CodSpeed

Footnotes

  1. 23 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Copilot AI 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.

🟡 Changes recommended

Latin-1 alias handling remains incomplete, and BOM-prefixed UTF-8-sig files are not handled correctly.

3 open findings
What changed in this PR

Improves Python source encoding handling in the Rust scanner and propagates decoding failures safely to Python.

Changes:

  • Normalize supported encoding declarations and preserve true Latin-1 semantics.
  • Replace decoding panics with Python UnicodeError propagation.
  • Add functional tests and release metadata.
File Description
rust/​src/​filesystem.rs Adds encoding normalization and structured read errors.
rust/​src/​errors.rs Maps filesystem errors to Python exceptions.
rust/​src/​import_scanning.rs Propagates read failures instead of unwrapping.
tests/​functional/​test_encoding_handling.py Tests aliases, Latin-1, and decoding failures.
CHANGELOG.rst Documents the encoding fix.
AUTHORS.rst Adds the contributor.
uv.lock Synchronizes the project version.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread rust/src/filesystem.rs
Comment on lines +45 to +47
if ["latin-1", "iso-8859-1", "iso-latin-1"]
.into_iter()
.any(is_spelling_of)
Comment thread rust/src/filesystem.rs
Comment on lines +51 to +52
if is_spelling_of("utf-8") {
return encoding_rs::Encoding::for_label(b"utf-8").map(Encoding::Other);
Comment thread CHANGELOG.rst
Comment on lines +10 to +11
* Fix panic when a module declares its encoding using a name that Python accepts but that isn't a
WHATWG label, such as ``latin-1`` or ``utf_8``.
seddonym and others added 6 commits October 12, 2026 15:12
Modules can declare their encoding (PEP 263) using any name that Python
accepts, such as `latin-1`, `utf_8` or `euc_jp`. But encoding_rs only knows
the WHATWG labels (`latin1`, `utf-8`, `euc-jp`...), so these names weren't
recognized, and building the graph panicked with:

    pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err`
    value: PyErr { type: <class 'TypeError'>, value: TypeError('function
    takes exactly 5 arguments (1 given)'), traceback: None }

Normalize the declared name like Python does before looking it up.

Modules that really can't be decoded also caused this panic, as the error
was created as a UnicodeDecodeError, which can't be created from just a
message, and was then unwrapped. Raise it as a UnicodeError instead, which
keeps the message naming the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
The previous commit stored file read errors in GrimpError as a PyErr.
GrimpError's Debug and Display implementations then call into Python, so
the Rust test binaries started linking libpython, and `just test-rust`
failed wherever libpython isn't on the loader path:

    error while loading shared libraries: libpython3.13.so.1.0: cannot
    open shared object file: No such file or directory

Make FileSystem::read return Rust errors instead (GrimpError::FileNotFound
and GrimpError::UndecodableFile), and convert them to FileNotFoundError
and UnicodeError where they reach Python, like the other GrimpError
variants. The exception types and messages are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
As suggested by AI review.
@seddonym
seddonym force-pushed the fix-encoding-declarations branch from 70b4a8b to a87c498 Compare October 12, 2026 14:12
@seddonym
seddonym merged commit ad710a3 into main Oct 12, 2026
27 checks passed
@seddonym
seddonym deleted the fix-encoding-declarations branch October 12, 2026 14:53
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