@@ -1139,6 +1139,21 @@ unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
1139
1139
}
1140
1140
#endif
1141
1141
1142
+ static PyObject *
1143
+ resize_copy (PyObject * unicode , Py_ssize_t length )
1144
+ {
1145
+ Py_ssize_t copy_length ;
1146
+ PyObject * copy ;
1147
+
1148
+ copy = PyUnicode_New (length , PyUnicode_MAX_CHAR_VALUE (unicode ));
1149
+ if (copy == NULL )
1150
+ return NULL ;
1151
+
1152
+ copy_length = Py_MIN (length , PyUnicode_GET_LENGTH (unicode ));
1153
+ _PyUnicode_FastCopyCharacters (copy , 0 , unicode , 0 , copy_length );
1154
+ return copy ;
1155
+ }
1156
+
1142
1157
static PyObject *
1143
1158
resize_compact (PyObject * unicode , Py_ssize_t length )
1144
1159
{
@@ -1150,7 +1165,14 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
1150
1165
Py_ssize_t old_length = _PyUnicode_LENGTH (unicode );
1151
1166
#endif
1152
1167
1153
- assert (unicode_modifiable (unicode ));
1168
+ if (!unicode_modifiable (unicode )) {
1169
+ PyObject * copy = resize_copy (unicode , length );
1170
+ if (copy == NULL ) {
1171
+ return NULL ;
1172
+ }
1173
+ Py_DECREF (unicode );
1174
+ return copy ;
1175
+ }
1154
1176
assert (PyUnicode_IS_COMPACT (unicode ));
1155
1177
1156
1178
char_size = PyUnicode_KIND (unicode );
@@ -1250,21 +1272,6 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
1250
1272
return 0 ;
1251
1273
}
1252
1274
1253
- static PyObject *
1254
- resize_copy (PyObject * unicode , Py_ssize_t length )
1255
- {
1256
- Py_ssize_t copy_length ;
1257
- PyObject * copy ;
1258
-
1259
- copy = PyUnicode_New (length , PyUnicode_MAX_CHAR_VALUE (unicode ));
1260
- if (copy == NULL )
1261
- return NULL ;
1262
-
1263
- copy_length = Py_MIN (length , PyUnicode_GET_LENGTH (unicode ));
1264
- _PyUnicode_FastCopyCharacters (copy , 0 , unicode , 0 , copy_length );
1265
- return copy ;
1266
- }
1267
-
1268
1275
static const char *
1269
1276
unicode_kind_name (PyObject * unicode )
1270
1277
{
@@ -1816,7 +1823,7 @@ static int
1816
1823
unicode_modifiable (PyObject * unicode )
1817
1824
{
1818
1825
assert (_PyUnicode_CHECK (unicode ));
1819
- if (Py_REFCNT (unicode ) != 1 )
1826
+ if (! _PyObject_IsUniquelyReferenced (unicode ))
1820
1827
return 0 ;
1821
1828
if (PyUnicode_HASH (unicode ) != -1 )
1822
1829
return 0 ;
@@ -14738,7 +14745,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
14738
14745
assert (PyUnicode_IS_ASCII (result ));
14739
14746
14740
14747
/* To modify the string in-place, there can only be one reference. */
14741
- if (Py_REFCNT (result ) != 1 ) {
14748
+ if (! _PyObject_IsUniquelyReferenced (result )) {
14742
14749
Py_DECREF (result );
14743
14750
PyErr_BadInternalCall ();
14744
14751
return NULL ;
0 commit comments