@@ -1121,6 +1121,21 @@ unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
11211121}
11221122#endif
11231123
1124+ static PyObject *
1125+ resize_copy (PyObject * unicode , Py_ssize_t length )
1126+ {
1127+ Py_ssize_t copy_length ;
1128+ PyObject * copy ;
1129+
1130+ copy = PyUnicode_New (length , PyUnicode_MAX_CHAR_VALUE (unicode ));
1131+ if (copy == NULL )
1132+ return NULL ;
1133+
1134+ copy_length = Py_MIN (length , PyUnicode_GET_LENGTH (unicode ));
1135+ _PyUnicode_FastCopyCharacters (copy , 0 , unicode , 0 , copy_length );
1136+ return copy ;
1137+ }
1138+
11241139static PyObject *
11251140resize_compact (PyObject * unicode , Py_ssize_t length )
11261141{
@@ -1132,7 +1147,14 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11321147 Py_ssize_t old_length = _PyUnicode_LENGTH (unicode );
11331148#endif
11341149
1135- assert (unicode_modifiable (unicode ));
1150+ if (!unicode_modifiable (unicode )) {
1151+ PyObject * copy = resize_copy (unicode , length );
1152+ if (copy == NULL ) {
1153+ return NULL ;
1154+ }
1155+ Py_DECREF (unicode );
1156+ return copy ;
1157+ }
11361158 assert (PyUnicode_IS_COMPACT (unicode ));
11371159
11381160 char_size = PyUnicode_KIND (unicode );
@@ -1232,21 +1254,6 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
12321254 return 0 ;
12331255}
12341256
1235- static PyObject *
1236- resize_copy (PyObject * unicode , Py_ssize_t length )
1237- {
1238- Py_ssize_t copy_length ;
1239- PyObject * copy ;
1240-
1241- copy = PyUnicode_New (length , PyUnicode_MAX_CHAR_VALUE (unicode ));
1242- if (copy == NULL )
1243- return NULL ;
1244-
1245- copy_length = Py_MIN (length , PyUnicode_GET_LENGTH (unicode ));
1246- _PyUnicode_FastCopyCharacters (copy , 0 , unicode , 0 , copy_length );
1247- return copy ;
1248- }
1249-
12501257static const char *
12511258unicode_kind_name (PyObject * unicode )
12521259{
@@ -1836,7 +1843,7 @@ static int
18361843unicode_modifiable (PyObject * unicode )
18371844{
18381845 assert (_PyUnicode_CHECK (unicode ));
1839- if (Py_REFCNT (unicode ) != 1 )
1846+ if (! _PyObject_IsUniquelyReferenced (unicode ))
18401847 return 0 ;
18411848 if (PyUnicode_HASH (unicode ) != -1 )
18421849 return 0 ;
@@ -14025,7 +14032,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
1402514032 assert (PyUnicode_IS_ASCII (result ));
1402614033
1402714034 /* To modify the string in-place, there can only be one reference. */
14028- if (Py_REFCNT (result ) != 1 ) {
14035+ if (! _PyObject_IsUniquelyReferenced (result )) {
1402914036 Py_DECREF (result );
1403014037 PyErr_BadInternalCall ();
1403114038 return NULL ;
0 commit comments