Skip to content

Escape control characters in string constants as Python, not as Rust - #6397

Merged
Tpt merged 3 commits into
PyO3:mainfrom
dylanpulver:fix-str-constant-escapes
Sep 9, 2026
Merged

Escape control characters in string constants as Python, not as Rust#6397
Tpt merged 3 commits into
PyO3:mainfrom
dylanpulver:fix-str-constant-escapes

Conversation

@dylanpulver

Copy link
Copy Markdown
Contributor

Display for PyStaticExpr renders string constants with {value:?}, which applies Rust's escaping rules. Rust writes an unprintable character as \u{1b}, and Python's \u escape takes exactly four hex digits, so that form is a SyntaxError rather than an escape.

Any string constant holding a C0 control character other than NUL, tab, newline or carriage return therefore renders as invalid Python. A signature default such as "\x1b[0m" is the realistic way to reach it.

The sibling renderer in pyo3-introspection/src/stubs.rs already writes \x1b here. This mirrors its escape table so the two agree. I noticed the split while reading both renderers after #6393.

The existing test covers "\0\t\\\"", the four characters whose Rust and Python escapes are identical, so the divergence did not show up.

`Display for PyStaticExpr` rendered string constants with `{value:?}`,
which uses Rust's escaping rules. Rust writes an unprintable character
as `\u{1b}`; Python's `\u` escape takes exactly four hex digits, so that
form is a SyntaxError rather than an escape. Any string constant holding
a C0 control character other than NUL, tab, newline or carriage return
therefore rendered as invalid Python -- an ANSI sequence in a signature
default such as `"\x1b[0m"` is the realistic way to hit it.

The sibling renderer in `pyo3-introspection/src/stubs.rs` already gets
this right, writing `\x1b`. This mirrors its escape table so the two
agree.

The existing test covered `"\0\t\\\""`, the four characters whose Rust
and Python escapes happen to be identical, so the divergence did not
show up.
@codspeed-hq

codspeed-hq Bot commented Sep 9, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 15.72%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 5 regressed benchmarks
✅ 136 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
into_biguint_zero 1.1 µs 1.4 µs -21.57%
into_biguint_small 1.2 µs 1.5 µs -21.54%
into_bigint_small 2.6 µs 3 µs -12.59%
critical_section_creation 1.3 µs 1.5 µs -11.24%
extract_bigint_small 1.4 µs 1.5 µs -10.94%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing dylanpulver:fix-str-constant-escapes (99852a5) with main (fe2eb7a)

Open in CodSpeed

Footnotes

  1. 6 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.

@Tpt Tpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! That's a bit unfortunate to get this code duplicated but not sure setting up a way to get this code shared would be better

@Tpt
Tpt enabled auto-merge September 9, 2026 08:52
auto-merge was automatically disabled September 9, 2026 09:06

Head branch was pushed to by a user without write access

@Tpt
Tpt enabled auto-merge September 9, 2026 09:07
@Tpt
Tpt added this pull request to the merge queue Sep 9, 2026
Merged via the queue into PyO3:main with commit 360f899 Sep 9, 2026
49 of 53 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