@@ -102,9 +102,7 @@ NOTE: In the interpreter's initialization phase, some globals are currently
102102
103103*/
104104
105- // Maximum code point of Unicode 6.0: 0x10ffff (1,114,111).
106- // The value must be the same in fileutils.c.
107- #define MAX_UNICODE 0x10ffff
105+ #define MAX_UNICODE _Py_MAX_UNICODE
108106
109107#ifdef Py_DEBUG
110108# define _PyUnicode_CHECK (op ) _PyUnicode_CheckConsistency(op, 0)
@@ -166,12 +164,6 @@ static inline void PyUnicode_SET_UTF8_LENGTH(PyObject *op, Py_ssize_t length)
166164#define _PyUnicode_HASH (op ) \
167165 (_PyASCIIObject_CAST(op)->hash)
168166
169- static inline Py_hash_t PyUnicode_HASH (PyObject * op )
170- {
171- assert (_PyUnicode_CHECK (op ));
172- return FT_ATOMIC_LOAD_SSIZE_RELAXED (_PyASCIIObject_CAST (op )-> hash );
173- }
174-
175167static inline void PyUnicode_SET_HASH (PyObject * op , Py_hash_t hash )
176168{
177169 FT_ATOMIC_STORE_SSIZE_RELAXED (_PyASCIIObject_CAST (op )-> hash , hash );
@@ -1841,18 +1833,21 @@ unicode_is_singleton(PyObject *unicode)
18411833}
18421834#endif
18431835
1836+ // If this function is updated, update also _PyUnicodeWriter_CanWrite().
18441837static int
18451838unicode_modifiable (PyObject * unicode )
18461839{
18471840 assert (_PyUnicode_CHECK (unicode ));
1841+ if (!PyUnicode_CheckExact (unicode ))
1842+ return 0 ;
1843+ // On Free Threading, this test fails if called from a thread other
1844+ // than the one which created the str object.
18481845 if (!_PyObject_IsUniquelyReferenced (unicode ))
18491846 return 0 ;
18501847 if (PyUnicode_HASH (unicode ) != -1 )
18511848 return 0 ;
18521849 if (PyUnicode_CHECK_INTERNED (unicode ))
18531850 return 0 ;
1854- if (!PyUnicode_CheckExact (unicode ))
1855- return 0 ;
18561851#ifdef Py_DEBUG
18571852 /* singleton refcount is greater than 1 */
18581853 assert (!unicode_is_singleton (unicode ));
@@ -2156,6 +2151,7 @@ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
21562151 if (_PyUnicodeWriter_Prepare (writer , size - num_surrogates , maxchar ) < 0 ) {
21572152 return -1 ;
21582153 }
2154+ assert (_PyUnicodeWriter_CanWrite (writer ));
21592155
21602156 int kind = writer -> kind ;
21612157 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2453,6 +2449,7 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
24532449 if (_PyUnicodeWriter_Prepare (writer , size , maxchar ) < 0 ) {
24542450 return -1 ;
24552451 }
2452+ assert (_PyUnicodeWriter_CanWrite (writer ));
24562453
24572454 int kind = writer -> kind ;
24582455 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2739,8 +2736,10 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
27392736 else
27402737 maxchar = writer -> maxchar ;
27412738
2742- if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 )
2739+ if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 ) {
27432740 return -1 ;
2741+ }
2742+ assert (_PyUnicodeWriter_CanWrite (writer ));
27442743
27452744 fill = Py_MAX (width - length , 0 );
27462745 if (fill && !(flags & F_LJUST )) {
@@ -3030,8 +3029,10 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
30303029 Py_ssize_t spacepad = Py_MAX (width - precision - sign , 0 );
30313030 Py_ssize_t zeropad = Py_MAX (precision - len , 0 );
30323031
3033- if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 )
3032+ if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 ) {
30343033 return NULL ;
3034+ }
3035+ assert (_PyUnicodeWriter_CanWrite (writer ));
30353036
30363037 if (spacepad && !(flags & F_LJUST )) {
30373038 if (PyUnicode_Fill (writer -> buffer , writer -> pos , spacepad , ' ' ) == -1 )
@@ -5587,6 +5588,7 @@ unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
55875588 if (_PyUnicodeWriter_Prepare (writer , size , 127 ) < 0 ) {
55885589 return -1 ;
55895590 }
5591+ assert (_PyUnicodeWriter_CanWrite (writer ));
55905592
55915593 const char * starts = s ;
55925594 const char * end = s + size ;
@@ -13854,10 +13856,13 @@ void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
1385413856static inline void
1385513857_PyUnicodeWriter_InitWithBuffer (_PyUnicodeWriter * writer , PyObject * buffer )
1385613858{
13859+ assert (PyUnstable_Object_IsUniquelyReferenced (buffer ));
13860+
1385713861 memset (writer , 0 , sizeof (* writer ));
1385813862 writer -> buffer = buffer ;
1385913863 _PyUnicodeWriter_Update (writer );
1386013864 writer -> min_length = writer -> size ;
13865+ assert (_PyUnicodeWriter_CanWrite (writer ));
1386113866}
1386213867
1386313868
@@ -13966,6 +13971,7 @@ _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch)
1396613971 assert (ch <= MAX_UNICODE );
1396713972 if (_PyUnicodeWriter_Prepare (writer , 1 , ch ) < 0 )
1396813973 return -1 ;
13974+ assert (_PyUnicodeWriter_CanWrite (writer ));
1396913975 PyUnicode_WRITE (writer -> kind , writer -> data , writer -> pos , ch );
1397013976 writer -> pos ++ ;
1397113977 return 0 ;
@@ -14013,6 +14019,8 @@ _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str)
1401314019 if (_PyUnicodeWriter_PrepareInternal (writer , len , maxchar ) == -1 )
1401414020 return -1 ;
1401514021 }
14022+
14023+ assert (_PyUnicodeWriter_CanWrite (writer ));
1401614024 _PyUnicode_FastCopyCharacters (writer -> buffer , writer -> pos ,
1401714025 str , 0 , len );
1401814026 writer -> pos += len ;
@@ -14090,6 +14098,7 @@ _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str,
1409014098 if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 ) {
1409114099 return -1 ;
1409214100 }
14101+ assert (_PyUnicodeWriter_CanWrite (writer ));
1409314102
1409414103 _PyUnicode_FastCopyCharacters (writer -> buffer , writer -> pos ,
1409514104 str , start , len );
@@ -14147,8 +14156,10 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
1414714156 return 0 ;
1414814157 }
1414914158
14150- if (_PyUnicodeWriter_Prepare (writer , len , 127 ) == -1 )
14159+ if (_PyUnicodeWriter_Prepare (writer , len , 127 ) == -1 ) {
1415114160 return -1 ;
14161+ }
14162+ assert (_PyUnicodeWriter_CanWrite (writer ));
1415214163
1415314164 switch (writer -> kind )
1415414165 {
@@ -14252,6 +14263,7 @@ _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
1425214263 maxchar = ucs1lib_find_max_char ((const Py_UCS1 * )str , (const Py_UCS1 * )str + len );
1425314264 if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) == -1 )
1425414265 return -1 ;
14266+ assert (_PyUnicodeWriter_CanWrite (writer ));
1425514267 unicode_write_cstr (writer -> buffer , writer -> pos , str , len );
1425614268 writer -> pos += len ;
1425714269 return 0 ;
0 commit comments