Skip to content

Commit 32c2649

Browse files
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.
1 parent fe9ac7f commit 32c2649

File tree

10 files changed

+29
-36
lines changed

10 files changed

+29
-36
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixing the checking of whether an object is uniquely referenced to ensure
2+
free-threaded compatibility. Patch by Sergey Miryanov.

Modules/_elementtree.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
912912
return Py_NewRef(object);
913913
}
914914

915-
if (Py_REFCNT(object) == 1) {
915+
if (_PyObject_IsUniquelyReferenced(object)) {
916916
if (PyDict_CheckExact(object)) {
917917
PyObject *key, *value;
918918
Py_ssize_t pos = 0;
@@ -2794,8 +2794,9 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
27942794
self->data = Py_NewRef(data);
27952795
} else {
27962796
/* more than one item; use a list to collect items */
2797-
if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 &&
2798-
PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
2797+
if (PyBytes_CheckExact(self->data)
2798+
&& _PyObject_IsUniquelyReferenced(self->data)
2799+
&& PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
27992800
/* XXX this code path unused in Python 3? */
28002801
/* expat often generates single character data sections; handle
28012802
the most common case by resizing the existing string... */

Modules/_functoolsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
291291
if (kw == NULL) {
292292
pto->kw = PyDict_New();
293293
}
294-
else if (Py_REFCNT(kw) == 1) {
294+
else if (_PyObject_IsUniquelyReferenced(kw)) {
295295
pto->kw = Py_NewRef(kw);
296296
}
297297
else {
@@ -1076,7 +1076,7 @@ _functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
10761076
for (;;) {
10771077
PyObject *op2;
10781078

1079-
if (Py_REFCNT(args) > 1) {
1079+
if (!_PyObject_IsUniquelyReferenced(args)) {
10801080
Py_DECREF(args);
10811081
if ((args = PyTuple_New(2)) == NULL)
10821082
goto Fail;

Modules/itertoolsmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pairwise_next(PyObject *op)
376376
}
377377

378378
result = po->result;
379-
if (Py_REFCNT(result) == 1) {
379+
if (_PyObject_IsUniquelyReferenced(result)) {
380380
Py_INCREF(result);
381381
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
382382
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
@@ -802,7 +802,7 @@ teedataobject_traverse(PyObject *op, visitproc visit, void * arg)
802802
static void
803803
teedataobject_safe_decref(PyObject *obj)
804804
{
805-
while (obj && Py_REFCNT(obj) == 1) {
805+
while (obj && _PyObject_IsUniquelyReferenced(obj)) {
806806
teedataobject *tmp = teedataobject_CAST(obj);
807807
PyObject *nextlink = tmp->nextlink;
808808
tmp->nextlink = NULL;
@@ -2129,7 +2129,7 @@ product_next_lock_held(PyObject *op)
21292129
Py_ssize_t *indices = lz->indices;
21302130

21312131
/* Copy the previous result tuple or re-use it if available */
2132-
if (Py_REFCNT(result) > 1) {
2132+
if (!_PyObject_IsUniquelyReferenced(result)) {
21332133
PyObject *old_result = result;
21342134
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), npools);
21352135
if (result == NULL)
@@ -2364,7 +2364,7 @@ combinations_next_lock_held(PyObject *op)
23642364
}
23652365
} else {
23662366
/* Copy the previous result tuple or re-use it if available */
2367-
if (Py_REFCNT(result) > 1) {
2367+
if (!_PyObject_IsUniquelyReferenced(result)) {
23682368
PyObject *old_result = result;
23692369
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
23702370
if (result == NULL)
@@ -2618,7 +2618,7 @@ cwr_next(PyObject *op)
26182618
}
26192619
} else {
26202620
/* Copy the previous result tuple or re-use it if available */
2621-
if (Py_REFCNT(result) > 1) {
2621+
if (!_PyObject_IsUniquelyReferenced(result)) {
26222622
PyObject *old_result = result;
26232623
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
26242624
if (result == NULL)
@@ -2879,7 +2879,7 @@ permutations_next(PyObject *op)
28792879
goto empty;
28802880

28812881
/* Copy the previous result tuple or re-use it if available */
2882-
if (Py_REFCNT(result) > 1) {
2882+
if (!_PyObject_IsUniquelyReferenced(result)) {
28832883
PyObject *old_result = result;
28842884
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
28852885
if (result == NULL)
@@ -3847,7 +3847,7 @@ zip_longest_next(PyObject *op)
38473847
return NULL;
38483848
if (lz->numactive == 0)
38493849
return NULL;
3850-
if (Py_REFCNT(result) == 1) {
3850+
if (_PyObject_IsUniquelyReferenced(result)) {
38513851
Py_INCREF(result);
38523852
for (i=0 ; i < tuplesize ; i++) {
38533853
it = PyTuple_GET_ITEM(lz->ittuple, i);

Objects/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w)
31713171
return;
31723172
}
31733173

3174-
if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) {
3174+
if (_PyObject_IsUniquelyReferenced(*pv) && PyBytes_CheckExact(*pv)) {
31753175
/* Only one reference, so we can resize in place */
31763176
Py_ssize_t oldsize;
31773177
Py_buffer wb;
@@ -3256,7 +3256,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
32563256
Py_DECREF(v);
32573257
return 0;
32583258
}
3259-
if (Py_REFCNT(v) != 1) {
3259+
if (!_PyObject_IsUniquelyReferenced(v)) {
32603260
if (oldsize < newsize) {
32613261
*pv = _PyBytes_FromSize(newsize, 0);
32623262
if (*pv) {

Objects/dictobject.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5667,22 +5667,10 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self,
56675667

56685668
#endif
56695669

5670-
static bool
5671-
has_unique_reference(PyObject *op)
5672-
{
5673-
#ifdef Py_GIL_DISABLED
5674-
return (_Py_IsOwnedByCurrentThread(op) &&
5675-
op->ob_ref_local == 1 &&
5676-
_Py_atomic_load_ssize_relaxed(&op->ob_ref_shared) == 0);
5677-
#else
5678-
return Py_REFCNT(op) == 1;
5679-
#endif
5680-
}
5681-
56825670
static bool
56835671
acquire_iter_result(PyObject *result)
56845672
{
5685-
if (has_unique_reference(result)) {
5673+
if (_PyObject_IsUniquelyReferenced(result)) {
56865674
Py_INCREF(result);
56875675
return true;
56885676
}
@@ -5831,7 +5819,7 @@ dictreviter_iter_lock_held(PyDictObject *d, PyObject *self)
58315819
}
58325820
else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
58335821
result = di->di_result;
5834-
if (Py_REFCNT(result) == 1) {
5822+
if (_PyObject_IsUniquelyReferenced(result)) {
58355823
PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
58365824
PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
58375825
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));

Objects/longobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ _PyLong_Negate(PyLongObject **x_p)
352352
PyLongObject *x;
353353

354354
x = (PyLongObject *)*x_p;
355-
if (Py_REFCNT(x) == 1) {
355+
if (_PyObject_IsUniquelyReferenced((PyObject *)x)) {
356356
_PyLong_FlipSign(x);
357357
return;
358358
}
@@ -5849,7 +5849,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
58495849
assert(size_a >= 0);
58505850
_PyLong_SetSignAndDigitCount(c, 1, size_a);
58515851
}
5852-
else if (Py_REFCNT(a) == 1) {
5852+
else if (_PyObject_IsUniquelyReferenced((PyObject *)a)) {
58535853
c = (PyLongObject*)Py_NewRef(a);
58545854
}
58555855
else {
@@ -5863,7 +5863,8 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
58635863
assert(size_a >= 0);
58645864
_PyLong_SetSignAndDigitCount(d, 1, size_a);
58655865
}
5866-
else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
5866+
else if (_PyObject_IsUniquelyReferenced((PyObject *)b)
5867+
&& size_a <= alloc_b) {
58675868
d = (PyLongObject*)Py_NewRef(b);
58685869
assert(size_a >= 0);
58695870
_PyLong_SetSignAndDigitCount(d, 1, size_a);

Objects/setobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ set_init(PyObject *so, PyObject *args, PyObject *kwds)
24442444
if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
24452445
return -1;
24462446

2447-
if (Py_REFCNT(self) == 1 && self->fill == 0) {
2447+
if (_PyObject_IsUniquelyReferenced((PyObject *)self) && self->fill == 0) {
24482448
self->hash = -1;
24492449
if (iterable == NULL) {
24502450
return 0;
@@ -2774,7 +2774,7 @@ int
27742774
PySet_Add(PyObject *anyset, PyObject *key)
27752775
{
27762776
if (!PySet_Check(anyset) &&
2777-
(!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2777+
(!PyFrozenSet_Check(anyset) || !_PyObject_IsUniquelyReferenced(anyset))) {
27782778
PyErr_BadInternalCall();
27792779
return -1;
27802780
}

Objects/tupleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int
118118
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
119119
{
120120
PyObject **p;
121-
if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
121+
if (!PyTuple_Check(op) || !_PyObject_IsUniquelyReferenced(op)) {
122122
Py_XDECREF(newitem);
123123
PyErr_BadInternalCall();
124124
return -1;
@@ -923,7 +923,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
923923

924924
v = (PyTupleObject *) *pv;
925925
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
926-
(Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
926+
(Py_SIZE(v) != 0 && !_PyObject_IsUniquelyReferenced(*pv))) {
927927
*pv = 0;
928928
Py_XDECREF(v);
929929
PyErr_BadInternalCall();

Python/marshal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_code.h" // _PyCode_New()
1212
#include "pycore_hashtable.h" // _Py_hashtable_t
1313
#include "pycore_long.h" // _PyLong_IsZero()
14+
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced
1415
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1516
#include "pycore_setobject.h" // _PySet_NextEntryRef()
1617
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal()
@@ -388,7 +389,7 @@ w_ref(PyObject *v, char *flag, WFILE *p)
388389
* But we use TYPE_REF always for interned string, to PYC file stable
389390
* as possible.
390391
*/
391-
if (Py_REFCNT(v) == 1 &&
392+
if (_PyObject_IsUniquelyReferenced(v) &&
392393
!(PyUnicode_CheckExact(v) && PyUnicode_CHECK_INTERNED(v))) {
393394
return 0;
394395
}

0 commit comments

Comments
 (0)