Skip to content

Commit 75cbb8d

Browse files
corona10kumaraditya303serhiy-storchaka
authored
gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039)
--------- Co-authored-by: Kumar Aditya <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent bdd23c0 commit 75cbb8d

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

Objects/unicodeobject.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,21 @@ unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
11391139
}
11401140
#endif
11411141

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+
11421157
static PyObject*
11431158
resize_compact(PyObject *unicode, Py_ssize_t length)
11441159
{
@@ -1150,7 +1165,14 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11501165
Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
11511166
#endif
11521167

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+
}
11541176
assert(PyUnicode_IS_COMPACT(unicode));
11551177

11561178
char_size = PyUnicode_KIND(unicode);
@@ -1250,21 +1272,6 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
12501272
return 0;
12511273
}
12521274

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-
12681275
static const char*
12691276
unicode_kind_name(PyObject *unicode)
12701277
{
@@ -1816,7 +1823,7 @@ static int
18161823
unicode_modifiable(PyObject *unicode)
18171824
{
18181825
assert(_PyUnicode_CHECK(unicode));
1819-
if (Py_REFCNT(unicode) != 1)
1826+
if (!_PyObject_IsUniquelyReferenced(unicode))
18201827
return 0;
18211828
if (PyUnicode_HASH(unicode) != -1)
18221829
return 0;
@@ -14738,7 +14745,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
1473814745
assert(PyUnicode_IS_ASCII(result));
1473914746

1474014747
/* To modify the string in-place, there can only be one reference. */
14741-
if (Py_REFCNT(result) != 1) {
14748+
if (!_PyObject_IsUniquelyReferenced(result)) {
1474214749
Py_DECREF(result);
1474314750
PyErr_BadInternalCall();
1474414751
return NULL;

0 commit comments

Comments
 (0)