@@ -1743,18 +1743,21 @@ unicode_is_singleton(PyObject *unicode)
17431743}
17441744#endif
17451745
1746+ // If this function is updated, update also _PyUnicodeWriter_CanWrite().
17461747int
17471748_PyUnicode_IsModifiable (PyObject * unicode )
17481749{
17491750 assert (_PyUnicode_CHECK (unicode ));
1751+ if (!PyUnicode_CheckExact (unicode ))
1752+ return 0 ;
1753+ // On Free Threading, this test fails if called from a thread other
1754+ // than the one which created the str object.
17501755 if (!_PyObject_IsUniquelyReferenced (unicode ))
17511756 return 0 ;
17521757 if (PyUnicode_HASH (unicode ) != -1 )
17531758 return 0 ;
17541759 if (PyUnicode_CHECK_INTERNED (unicode ))
17551760 return 0 ;
1756- if (!PyUnicode_CheckExact (unicode ))
1757- return 0 ;
17581761#ifdef Py_DEBUG
17591762 /* singleton refcount is greater than 1 */
17601763 assert (!unicode_is_singleton (unicode ));
@@ -2006,6 +2009,7 @@ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
20062009 if (_PyUnicodeWriter_Prepare (writer , size - num_surrogates , maxchar ) < 0 ) {
20072010 return -1 ;
20082011 }
2012+ assert (_PyUnicodeWriter_CanWrite (writer ));
20092013
20102014 int kind = writer -> kind ;
20112015 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2303,6 +2307,7 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
23032307 if (_PyUnicodeWriter_Prepare (writer , size , maxchar ) < 0 ) {
23042308 return -1 ;
23052309 }
2310+ assert (_PyUnicodeWriter_CanWrite (writer ));
23062311
23072312 int kind = writer -> kind ;
23082313 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2589,8 +2594,10 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
25892594 else
25902595 maxchar = writer -> maxchar ;
25912596
2592- if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 )
2597+ if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 ) {
25932598 return -1 ;
2599+ }
2600+ assert (_PyUnicodeWriter_CanWrite (writer ));
25942601
25952602 fill = Py_MAX (width - length , 0 );
25962603 if (fill && !(flags & F_LJUST )) {
@@ -2880,8 +2887,10 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
28802887 Py_ssize_t spacepad = Py_MAX (width - precision - sign , 0 );
28812888 Py_ssize_t zeropad = Py_MAX (precision - len , 0 );
28822889
2883- if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 )
2890+ if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 ) {
28842891 return NULL ;
2892+ }
2893+ assert (_PyUnicodeWriter_CanWrite (writer ));
28852894
28862895 if (spacepad && !(flags & F_LJUST )) {
28872896 if (PyUnicode_Fill (writer -> buffer , writer -> pos , spacepad , ' ' ) == -1 )
@@ -5408,6 +5417,7 @@ _PyUnicode_DecodeUTF8Writer(_PyUnicodeWriter *writer,
54085417 if (_PyUnicodeWriter_Prepare (writer , size , 127 ) < 0 ) {
54095418 return -1 ;
54105419 }
5420+ assert (_PyUnicodeWriter_CanWrite (writer ));
54115421
54125422 const char * starts = s ;
54135423 const char * end = s + size ;
0 commit comments