Skip to content

Fix byte preservation for Python 2 interned strings - #194

Merged
rocky merged 1 commit into
rocky:masterfrom
SakayanagiYuna:master
Nov 6, 2026
Merged

rocky merged 1 commit into
rocky:masterfrom
SakayanagiYuna:master

Conversation

@SakayanagiYuna

Copy link
Copy Markdown
Contributor

Python 2 TYPE_INTERNED and TYPE_STRINGREF ignore the per-read
bytes_for_s=True flag and always decode as UTF-8 with errors ignored.
For example, b"\xffA\x80" becomes "A", silently losing bytes.

This also affects co_code: a synthetic Python 2.7 pyc with interned
bytecode loses its 0x8c (CALL_FUNCTION_VAR) opcode byte.

The fix caches original bytes alongside text and returns the requested
representation, preserving identity for repeated references within each
representation and interned tags when re-marshalling, while leaving
Python 3 Unicode behavior unchanged.

Adds 33 regression cases using synthetic marshal fixtures verified with
stock CPython 2.7.18: 22 fail before the fix, and all 33 pass afterward.
The repository's pytest suite also passes on Python 3.12 and 3.14.

Honor the per-read bytes_for_s flag for Python 2 TYPE_INTERNED and
TYPE_STRINGREF records. Previously, these records were always decoded
as UTF-8 with errors ignored, silently dropping non-UTF-8 bytes.

Cache the original bytes alongside the existing text representation
so references can be read in either context without losing data,
preserving identity for repeated references within each representation.
Include both representations in code reference metadata to preserve
interned tags when re-marshalling.

Keep existing text decoding and Python 3 Unicode behavior unchanged.

Add synthetic regression tests for binary strings, mixed-context
references, interned bytecode in a Python 2.7 pyc, and marshal round trips.
@rocky

rocky commented Nov 6, 2026

Copy link
Copy Markdown
Owner

@SakayanagiYuna: This looks good; thanks! The CI failures I will try to address outside of this PR.

@rocky
rocky merged commit 5b18ff2 into rocky:master Nov 6, 2026
4 of 16 checks passed
@rocky

rocky commented Nov 6, 2026

Copy link
Copy Markdown
Owner

@SakayanagiYuna I was hoping this would also address #193 but it doesn't. Are you willing to look into that issue?

@SakayanagiYuna

Copy link
Copy Markdown
Contributor Author

Thanks for merging #194! I'll take a look at #193 and see what I can do.

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.

2 participants