gh-140061: Use _PyObject_IsUniquelyReferenced() to check if objects are uniquely referenced (gh-140062)
The previous `Py_REFCNT(x) == 1` checks can have data races in the free threaded build. `_PyObject_IsUniquelyReferenced(x)` is a more conservative check that is safe in the free threaded build and is identical to `Py_REFCNT(x) == 1` in the default GIL-enabled build.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "pycore_code.h" // _PyCode_New()
|
||||
#include "pycore_hashtable.h" // _Py_hashtable_t
|
||||
#include "pycore_long.h" // _PyLong_IsZero()
|
||||
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced
|
||||
#include "pycore_pystate.h" // _PyInterpreterState_GET()
|
||||
#include "pycore_setobject.h" // _PySet_NextEntryRef()
|
||||
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal()
|
||||
@@ -388,7 +389,7 @@ w_ref(PyObject *v, char *flag, WFILE *p)
|
||||
* But we use TYPE_REF always for interned string, to PYC file stable
|
||||
* as possible.
|
||||
*/
|
||||
if (Py_REFCNT(v) == 1 &&
|
||||
if (_PyObject_IsUniquelyReferenced(v) &&
|
||||
!(PyUnicode_CheckExact(v) && PyUnicode_CHECK_INTERNED(v))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user