@@ -1121,6 +1121,21 @@ unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
1121
1121
}
1122
1122
#endif
1123
1123
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
+
1124
1139
static PyObject *
1125
1140
resize_compact (PyObject * unicode , Py_ssize_t length )
1126
1141
{
@@ -1132,7 +1147,14 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
1132
1147
Py_ssize_t old_length = _PyUnicode_LENGTH (unicode );
1133
1148
#endif
1134
1149
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
+ }
1136
1158
assert (PyUnicode_IS_COMPACT (unicode ));
1137
1159
1138
1160
char_size = PyUnicode_KIND (unicode );
@@ -1232,21 +1254,6 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
1232
1254
return 0 ;
1233
1255
}
1234
1256
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
-
1250
1257
static const char *
1251
1258
unicode_kind_name (PyObject * unicode )
1252
1259
{
@@ -1836,7 +1843,7 @@ static int
1836
1843
unicode_modifiable (PyObject * unicode )
1837
1844
{
1838
1845
assert (_PyUnicode_CHECK (unicode ));
1839
- if (Py_REFCNT (unicode ) != 1 )
1846
+ if (! _PyObject_IsUniquelyReferenced (unicode ))
1840
1847
return 0 ;
1841
1848
if (PyUnicode_HASH (unicode ) != -1 )
1842
1849
return 0 ;
@@ -14025,7 +14032,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
14025
14032
assert (PyUnicode_IS_ASCII (result ));
14026
14033
14027
14034
/* To modify the string in-place, there can only be one reference. */
14028
- if (Py_REFCNT (result ) != 1 ) {
14035
+ if (! _PyObject_IsUniquelyReferenced (result )) {
14029
14036
Py_DECREF (result );
14030
14037
PyErr_BadInternalCall ();
14031
14038
return NULL ;
0 commit comments