Skip to content

gh-157710: Add more asserts to internal PyUnicode operations - #159033

Merged
encukou merged 7 commits into
python:mainfrom
encukou:more-unicode-protection
Nov 5, 2026
Merged

encukou merged 7 commits into
python:mainfrom
encukou:more-unicode-protection

Conversation

@encukou

@encukou encukou commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Add checks based on the review on #159028:

  • _Py*UnicodeObject_CAST macros assert they have the right bits for the shape
    (this means that _PyUnicode_UTF8 asserts it's not called on a compact ASCII object.)

  • _PyUnicode_IsModifiable & _PyUnicodeWriter_CanWrite assert that UTF-8 storage hasn't been allocated yet

  • Move _PyUnicode_UTF8 to the internal header for use in _PyUnicodeWriter_CanWrite

- `_Py*UnicodeObject_CAST` macros assert they have the right bits for the shape
- `_PyUnicode_IsModifiable` & `_PyUnicodeWriter_CanWrite` assert that
  UTF-8 storage hasn't been allocated yet
Comment thread Include/internal/pycore_unicodeobject.h Outdated
assert(!_Py_IsImmortal(buffer));
assert(PyUnicode_IS_COMPACT_ASCII(buffer)
|| _PyUnicode_UTF8(buffer) == NULL
|| _PyUnicode_UTF8(buffer) == PyUnicode_DATA(buffer));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer moving _PyUnicode_HAS_UTF8_MEMORY() static inline function to pycore_unicodeobject.h and just test:

assert(!_PyUnicode_HAS_UTF8_MEMORY(buffer);

Comment thread Include/cpython/unicodeobject.h Resolved
Comment thread Include/internal/pycore_unicodeobject.h Resolved
Co-authored-by: Victor Stinner <vstinner@python.org>
@encukou

encukou commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

Turns out I broke a private debug helper. Added a test so it doesn't happen again.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 16, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit df1fc4e 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F159033%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 16, 2026
@encukou
encukou merged commit 3c1848d into python:main Nov 5, 2026
135 of 144 checks passed
@encukou
encukou deleted the more-unicode-protection branch November 5, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants