Repository navigation
Conversation
- `_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
vstinner
reviewed
Oct 8, 2026
| assert(!_Py_IsImmortal(buffer)); | ||
| assert(PyUnicode_IS_COMPACT_ASCII(buffer) | ||
| || _PyUnicode_UTF8(buffer) == NULL | ||
| || _PyUnicode_UTF8(buffer) == PyUnicode_DATA(buffer)); |
Member
There was a problem hiding this comment.
I would prefer moving _PyUnicode_HAS_UTF8_MEMORY() static inline function to pycore_unicodeobject.h and just test:
assert(!_PyUnicode_HAS_UTF8_MEMORY(buffer);
| _Py_CAST(PyASCIIObject*, (op))) | ||
| #define _PyCompactUnicodeObject_CAST(op) \ | ||
| (assert(PyUnicode_Check(op)), \ | ||
| (assert(!_PyASCIIObject_CAST(op)->state.ascii || !_PyASCIIObject_CAST(op)->state.compact), \ |
Member
There was a problem hiding this comment.
nitpick: you might write the test as "make sure that it's not a compact ASCII string":
Suggested change
| (assert(!_PyASCIIObject_CAST(op)->state.ascii || !_PyASCIIObject_CAST(op)->state.compact), \ | |
| (assert(!(_PyASCIIObject_CAST(op)->state.ascii \ | |
| && _PyASCIIObject_CAST(op)->state.compact)), \ |
It's just a minor coding style suggestion, feel free to ignore it.
| return 0; | ||
| } | ||
|
|
||
| static inline char* _PyUnicode_UTF8(PyObject *op) |
Member
There was a problem hiding this comment.
Suggested change
| static inline char* _PyUnicode_UTF8(PyObject *op) | |
| static inline char* | |
| _PyUnicode_UTF8(PyObject *op) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add checks based on the review on #159028:
_Py*UnicodeObject_CASTmacros assert they have the right bits for the shape(this means that
_PyUnicode_UTF8asserts it's not called on a compact ASCII object.)_PyUnicode_IsModifiable&_PyUnicodeWriter_CanWriteassert that UTF-8 storage hasn't been allocated yetMove
_PyUnicode_UTF8to the internal header for use in_PyUnicodeWriter_CanWrite