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

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Apr 27, 2025

@corona10 corona10 changed the title Kumar test gh-132070: use _PyObject_IsUniquelyReferenced in unicodeobject Apr 27, 2025
@corona10
Copy link
Member Author

corona10 commented Apr 27, 2025

@kumaraditya303 @colesbury
I continued working on Kumar's PR #132091. In the new change, more states of unicode_modifiable are observed in free-threading builds. So I added a fallback to use resize_copy when unicode_modifiable fails, but I'm not sure if this is the right approach.
But the test failure issues are resolved.

@corona10 corona10 marked this pull request as ready for review April 27, 2025 10:12
@corona10 corona10 changed the title gh-132070: use _PyObject_IsUniquelyReferenced in unicodeobject gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject Apr 27, 2025
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into the case that requires resize_copy: We have an io.StringIO, which is used first by one thread and then by a second thread. (It's not used by two threads simultaneously). The io.StringIO has the only reference to a PyUnicodeWriter, which in turn has the only reference to unicode object that is treated as a buffer.

The _PyObject_IsUniquelyReferenced() check is too conservative in this case because the unicode object is owned by a different thread. The unicode object is actually safe to modify, we just can't tell that from _PyObject_IsUniquelyReferenced().

@corona10 corona10 merged commit 75cbb8d into python:main Apr 29, 2025
45 checks passed
@miss-islington-app
Copy link

Thanks @corona10 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 29, 2025
…ythongh-133039)

---------
(cherry picked from commit 75cbb8d)

Co-authored-by: Donghee Na <donghee.na@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Apr 29, 2025

GH-133121 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Apr 29, 2025
@corona10 corona10 deleted the kumar-test branch April 29, 2025 02:07
@bedevere-app
Copy link

bedevere-app bot commented Apr 29, 2025

GH-133126 is a backport of this pull request to the 3.13 branch.

corona10 added a commit that referenced this pull request Apr 29, 2025
…gh-133039) (gh-133126)

* gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039)

---------
(cherry picked from commit 75cbb8d)

Co-authored-by: Donghee Na <donghee.na@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

* Add _PyObject_IsUniquelyReferenced

---------

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants