Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ngoldbaum
Copy link
Contributor

Adds a shim for PyUnstable_Object_IsUniquelyReferenced and a test for the shim.

I'd like to use this function in a few libraries but don't want to have to have a thread-unsafe check in 3.13 or add new uses of internal headers outside of the compat header here.

I'm not sure whether the using stuff from internal headers is ok in a shim like this. I'm also not sure if you want to avoid PyUnstable functions in the compat headers.

The implementation is copy/pasted from the implementation of _PyObject_IsUniquelyReferenced.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

Can you please rebase your PR on the updated main branch to get tests on Free Threaded builds?

@@ -1953,6 +1953,28 @@ test_unicodewriter_format(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
}
#endif

#ifndef PYPY_VERSION
Copy link
Member

Choose a reason for hiding this comment

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

The function doesn't make sense on PyPy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently not, maybe an interned string? I rewrote it to use an empty tuple and that seems to work on PyPy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrote it to use an empty tuple and that seems to work on PyPy.

but that seems to have broken other builds, let me see if I can get it working everywhere...

@vstinner
Copy link
Member

vstinner commented Sep 1, 2025

Tests fail on Python 2:

python2.7: test_pythoncapi_compat_cext.c:1983: test_uniquely_referenced: Assertion `PyUnstable_Object_IsUniquelyReferenced(obj)' failed.

@ngoldbaum
Copy link
Contributor Author

Tests fail on Python 2

I guess empty tuples have had reference counting optimizations for a long time!

@vstinner
Copy link
Member

vstinner commented Sep 1, 2025

Please update also the doc:

  • docs/changelog.rst
  • docs/api.rst

@vstinner vstinner merged commit 90c06a4 into python:main Sep 1, 2025
36 checks passed
@vstinner
Copy link
Member

vstinner commented Sep 1, 2025

Merged, thanks for your contribution.

I'm not very excited by adding PyUnstable functions to pythoncapi-compat, but I'm fine with this specific function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants