Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75cbb8d

Browse files
corona10kumaraditya303serhiy-storchaka
authored
gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039)
---------Co-authored-by: Kumar Aditya <kumaraditya@python.org>Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parentbdd23c0 commit75cbb8d

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+
staticPyObject*
1143+
resize_copy(PyObject*unicode,Py_ssize_tlength)
1144+
{
1145+
Py_ssize_tcopy_length;
1146+
PyObject*copy;
1147+
1148+
copy=PyUnicode_New(length,PyUnicode_MAX_CHAR_VALUE(unicode));
1149+
if (copy==NULL)
1150+
returnNULL;
1151+
1152+
copy_length=Py_MIN(length,PyUnicode_GET_LENGTH(unicode));
1153+
_PyUnicode_FastCopyCharacters(copy,0,unicode,0,copy_length);
1154+
returncopy;
1155+
}
1156+
11421157
staticPyObject*
11431158
resize_compact(PyObject*unicode,Py_ssize_tlength)
11441159
{
@@ -1150,7 +1165,14 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11501165
Py_ssize_told_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+
returnNULL;
1172+
}
1173+
Py_DECREF(unicode);
1174+
returncopy;
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
return0;
12511273
}
12521274

1253-
staticPyObject*
1254-
resize_copy(PyObject*unicode,Py_ssize_tlength)
1255-
{
1256-
Py_ssize_tcopy_length;
1257-
PyObject*copy;
1258-
1259-
copy=PyUnicode_New(length,PyUnicode_MAX_CHAR_VALUE(unicode));
1260-
if (copy==NULL)
1261-
returnNULL;
1262-
1263-
copy_length=Py_MIN(length,PyUnicode_GET_LENGTH(unicode));
1264-
_PyUnicode_FastCopyCharacters(copy,0,unicode,0,copy_length);
1265-
returncopy;
1266-
}
1267-
12681275
staticconstchar*
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
return0;
18211828
if (PyUnicode_HASH(unicode)!=-1)
18221829
return0;
@@ -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
returnNULL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp