@@ -1744,18 +1744,21 @@ unicode_is_singleton(PyObject *unicode)
17441744}
17451745#endif
17461746
1747+ // If this function is updated, update also _PyUnicodeWriter_CanWrite().
17471748int
17481749_PyUnicode_IsModifiable (PyObject * unicode )
17491750{
17501751 assert (_PyUnicode_CHECK (unicode ));
1752+ if (!PyUnicode_CheckExact (unicode ))
1753+ return 0 ;
1754+ // On Free Threading, this test fails if called from a thread other
1755+ // than the one which created the str object.
17511756 if (!_PyObject_IsUniquelyReferenced (unicode ))
17521757 return 0 ;
17531758 if (PyUnicode_HASH (unicode ) != -1 )
17541759 return 0 ;
17551760 if (PyUnicode_CHECK_INTERNED (unicode ))
17561761 return 0 ;
1757- if (!PyUnicode_CheckExact (unicode ))
1758- return 0 ;
17591762#ifdef Py_DEBUG
17601763 /* singleton refcount is greater than 1 */
17611764 assert (!unicode_is_singleton (unicode ));
@@ -2009,6 +2012,7 @@ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
20092012 if (_PyUnicodeWriter_Prepare (writer , size - num_surrogates , maxchar ) < 0 ) {
20102013 return -1 ;
20112014 }
2015+ assert (_PyUnicodeWriter_CanWrite (writer ));
20122016
20132017 int kind = writer -> kind ;
20142018 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2267,6 +2271,7 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
22672271 if (_PyUnicodeWriter_Prepare (writer , size , max_char ) < 0 ) {
22682272 return -1 ;
22692273 }
2274+ assert (_PyUnicodeWriter_CanWrite (writer ));
22702275
22712276 int kind = writer -> kind ;
22722277 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2553,8 +2558,10 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
25532558 else
25542559 maxchar = writer -> maxchar ;
25552560
2556- if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 )
2561+ if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 ) {
25572562 return -1 ;
2563+ }
2564+ assert (_PyUnicodeWriter_CanWrite (writer ));
25582565
25592566 fill = Py_MAX (width - length , 0 );
25602567 if (fill && !(flags & F_LJUST )) {
@@ -2844,8 +2851,10 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
28442851 Py_ssize_t spacepad = Py_MAX (width - precision - sign , 0 );
28452852 Py_ssize_t zeropad = Py_MAX (precision - len , 0 );
28462853
2847- if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 )
2854+ if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 ) {
28482855 return NULL ;
2856+ }
2857+ assert (_PyUnicodeWriter_CanWrite (writer ));
28492858
28502859 if (spacepad && !(flags & F_LJUST )) {
28512860 if (PyUnicode_Fill (writer -> buffer , writer -> pos , spacepad , ' ' ) == -1 )
@@ -5372,6 +5381,7 @@ _PyUnicode_DecodeUTF8Writer(_PyUnicodeWriter *writer,
53725381 if (_PyUnicodeWriter_Prepare (writer , size , 127 ) < 0 ) {
53735382 return -1 ;
53745383 }
5384+ assert (_PyUnicodeWriter_CanWrite (writer ));
53755385
53765386 const char * starts = s ;
53775387 const char * end = s + size ;
0 commit comments