Skip to content

Preserve lone surrogates in Python AST constants - #10159

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:preserve-wtf8-python-ast-constant
Dec 4, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:preserve-wtf8-python-ast-constant

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

CPython's Python-to-AST bridge retains the original object for Constant.value. RustPython's bridge otherwise passes a lone surrogate through Ruff's UTF-8-only representation and reads it back as U+FFFD. This can change returned strings and nested tuple/frozenset values, or make two distinct mapping-pattern keys look identical.

Keep the original value in the existing AST node-identity metadata only for constants that cannot fit in Ruff's string representation. This leaves ordinary parse input, AST round-trip metadata, source positions, Ruff, and Lib unchanged. The percent-format preprocessor keeps the original modulo operation rather than building a lossy literal; full optimized AST shape parity for surrogate formats is separate. The intermediate constants in f- and t-strings keep their values too. The error type for a surrogate used as an integer format code and docstring cleanup are out of scope.

Linux verification: 132 differential observations against CPython 3.15.2 (35 mismatches corrected; 7 remain as noted above, no previously matching result changed), 340 selected canonical cases (one CPython-only skip on RustPython), 799 codegen unit tests, and the newly added snippet section. The new snippet fails on the same main build and passes on both CPython and the candidate. Full hosted platforms, full test suites and performance are not verified locally.

Refs #9852.

Retain exact values for surrogate-bearing constants in the node metadata used for AST input. Use the original values for code generation and mapping patterns, and do not rewrite percent formats when the generated literal would be lossy.

Refs: RustPython#9852

Assisted-by: Codex:GPT-6.2
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@youknowone
youknowone marked this pull request as ready for review December 4, 2026 02:16
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T02:22:50.594538Z 8cc92ca Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@youknowone
youknowone merged commit d84caf0 into RustPython:main Dec 4, 2026
19 of 20 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.

2 participants