Repository navigation
Fix byte preservation for Python 2 interned strings - #194
Merged
Merged
Conversation
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.
Owner
|
@SakayanagiYuna: This looks good; thanks! The CI failures I will try to address outside of this PR. |
Owner
|
@SakayanagiYuna I was hoping this would also address #193 but it doesn't. Are you willing to look into that issue? |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python 2
TYPE_INTERNEDandTYPE_STRINGREFignore the per-readbytes_for_s=Trueflag 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 internedbytecode 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.