@@ -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 ));
@@ -2008,6 +2011,7 @@ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
20082011 if (_PyUnicodeWriter_Prepare (writer , size - num_surrogates , maxchar ) < 0 ) {
20092012 return -1 ;
20102013 }
2014+ assert (_PyUnicodeWriter_CanWrite (writer ));
20112015
20122016 int kind = writer -> kind ;
20132017 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2266,6 +2270,7 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
22662270 if (_PyUnicodeWriter_Prepare (writer , size , max_char ) < 0 ) {
22672271 return -1 ;
22682272 }
2273+ assert (_PyUnicodeWriter_CanWrite (writer ));
22692274
22702275 int kind = writer -> kind ;
22712276 void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
@@ -2552,8 +2557,10 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
25522557 else
25532558 maxchar = writer -> maxchar ;
25542559
2555- if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 )
2560+ if (_PyUnicodeWriter_Prepare (writer , arglen , maxchar ) == -1 ) {
25562561 return -1 ;
2562+ }
2563+ assert (_PyUnicodeWriter_CanWrite (writer ));
25572564
25582565 fill = Py_MAX (width - length , 0 );
25592566 if (fill && !(flags & F_LJUST )) {
@@ -2843,8 +2850,10 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
28432850 Py_ssize_t spacepad = Py_MAX (width - precision - sign , 0 );
28442851 Py_ssize_t zeropad = Py_MAX (precision - len , 0 );
28452852
2846- if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 )
2853+ if (_PyUnicodeWriter_Prepare (writer , width , 127 ) == -1 ) {
28472854 return NULL ;
2855+ }
2856+ assert (_PyUnicodeWriter_CanWrite (writer ));
28482857
28492858 if (spacepad && !(flags & F_LJUST )) {
28502859 if (PyUnicode_Fill (writer -> buffer , writer -> pos , spacepad , ' ' ) == -1 )
@@ -5371,6 +5380,7 @@ _PyUnicode_DecodeUTF8Writer(_PyUnicodeWriter *writer,
53715380 if (_PyUnicodeWriter_Prepare (writer , size , 127 ) < 0 ) {
53725381 return -1 ;
53735382 }
5383+ assert (_PyUnicodeWriter_CanWrite (writer ));
53745384
53755385 const char * starts = s ;
53765386 const char * end = s + size ;
0 commit comments