8000 gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject by corona10 · Pull Request #133039 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject #133039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tmp
  • Loading branch information
corona10 committed Apr 27, 2025
commit 6ca1abb2cbc4c7e003770c912c0efb28eb7b1979
34 changes: 18 additions & 16 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,21 @@ unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length)
}
#endif

static PyObject*
resize_copy(PyObject *unicode, Py_ssize_t length)
{
Py_ssize_t copy_length;
PyObject *copy;

copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
if (copy == NULL)
return NULL;

copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
_PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
return copy;
}

static PyObject*
resize_compact(PyObject *unicode, Py_ssize_t length)
{
Expand All @@ -1150,7 +1165,9 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
#endif

assert(unicode_modifiable(unicode));
if (!unicode_modifiable(unicode)) {
return resize_copy(unicode, length);
}
assert(PyUnicode_IS_COMPACT(unicode));

char_size = PyUnicode_KIND(unicode);
Expand Down Expand Up @@ -1250,21 +1267,6 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
return 0;
}

static PyObject*
resize_copy(PyObject *unicode, Py_ssize_t length)
{
Py_ssize_t copy_length;
PyObject *copy;

copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
if (copy == NULL)
return NULL;

copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
_PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
return copy;
}

static const char*
unicode_kind_name(PyObject *unicode)
{
Expand Down
0