Skip to content

gh-154668: Add escaping mode to PyUnicode_FromFormat()#154669

Draft
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:fromformat-escape
Draft

gh-154668: Add escaping mode to PyUnicode_FromFormat()#154669
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:fromformat-escape

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 25, 2026

Copy link
Copy Markdown
Member

With the # flag, the c, s, S, U and V conversions now escape special and non-printable characters like repr() does, but without adding quotes, and with the additional + flag -- also all non-ASCII characters.

Bytes which cannot be decoded from UTF-8 in the s and V conversions are escaped as \xNN, while valid non-ASCII characters are always escaped as \uNNNN or \UNNNNNNNN, so they can always be distinguished.

The precision is applied to the string before escaping, the width -- after escaping.

With the "#" flag, the "c", "s", "S", "U" and "V" conversions now
escape special and non-printable characters, and with the additional
"+" flag -- also all non-ASCII characters.  Bytes which cannot be
decoded from UTF-8 in the "s" and "V" conversions are escaped as
\xNN, valid non-ASCII characters are escaped as \uNNNN or \UNNNNNNNN.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Jul 25, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33753430 | 📁 Comparing dd009c8 against main (5062427)

  🔍 Preview build  

3 files changed
± c-api/unicode.html
± whatsnew/3.16.html
± whatsnew/changelog.html

This avoids creating temporary objects.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant