Skip to content

Fix UnicodeForPython3.__repr__ for Python 2 literals - #196

Merged
rocky merged 1 commit into
rocky:masterfrom
SakayanagiYuna:codex/fix-py2-unicode-repr
Nov 6, 2026
Merged

rocky merged 1 commit into
rocky:masterfrom
SakayanagiYuna:codex/fix-py2-unicode-repr

Conversation

@SakayanagiYuna

Copy link
Copy Markdown
Contributor

UnicodeForPython3.__repr__ drops the first two characters of text
containing non-ASCII characters and builds invalid Unicode escapes.
For example, "中" becomes u'\u' instead of u'\u4e2d'.

The ASCII path also leaves quotes, backslashes, and control characters
unescaped. The fix uses ascii() on the decoded text and adds the u
prefix, preserving the stored bytes and str() output.

Adds 27 regression cases: 21 fail before the fix, and all 27 pass afterward.
The 21 Unicode literal fixtures were also verified with stock CPython
2.7.18.

Fixes #193.

Use ascii() on decoded UTF-8 text and prepend the u prefix in
UnicodeForPython3.__repr__.

The previous implementation dropped the first two characters of
strings containing non-ASCII characters and emitted invalid \u0x...
escapes. ASCII strings also lacked escaping for quotes, backslashes,
and control characters.

Preserve stored bytes and str() behavior while generating valid
Python 2 Unicode literals.

Add regression coverage for literal round trips and Unicode constants
read from Python 2 and Python 3 marshal records. Verify the 21 synthetic
Unicode literal fixtures with stock CPython 2.7.18.

Fixes rocky#193.
@rocky

rocky commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks! LGTM.

@rocky
rocky merged commit 4b90f69 into rocky:master Nov 6, 2026
12 checks passed
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.

UnicodeForPython3.__repr__ drops the first two characters and emits invalid \u0x… escapes

2 participants