--- /dev/null
+Fixing the checking of whether an object is uniquely referenced to ensure
+free-threaded compatibility. Patch by Sergey Miryanov.
return Py_NewRef(object);
}
- if (Py_REFCNT(object) == 1) {
+ if (_PyObject_IsUniquelyReferenced(object)) {
if (PyDict_CheckExact(object)) {
PyObject *key, *value;
Py_ssize_t pos = 0;
self->data = Py_NewRef(data);
} else {
/* more than one item; use a list to collect items */
- if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 &&
- PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
+ if (PyBytes_CheckExact(self->data)
+ && _PyObject_IsUniquelyReferenced(self->data)
+ && PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
/* XXX this code path unused in Python 3? */
/* expat often generates single character data sections; handle
the most common case by resizing the existing string... */
if (kw == NULL) {
pto->kw = PyDict_New();
}
- else if (Py_REFCNT(kw) == 1) {
+ else if (_PyObject_IsUniquelyReferenced(kw)) {
pto->kw = Py_NewRef(kw);
}
else {
for (;;) {
PyObject *op2;
- if (Py_REFCNT(args) > 1) {
+ if (!_PyObject_IsUniquelyReferenced(args)) {
Py_DECREF(args);
if ((args = PyTuple_New(2)) == NULL)
goto Fail;
}
result = po->result;
- if (Py_REFCNT(result) == 1) {
+ if (_PyObject_IsUniquelyReferenced(result)) {
Py_INCREF(result);
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
static void
teedataobject_safe_decref(PyObject *obj)
{
- while (obj && Py_REFCNT(obj) == 1) {
+ while (obj && _PyObject_IsUniquelyReferenced(obj)) {
teedataobject *tmp = teedataobject_CAST(obj);
PyObject *nextlink = tmp->nextlink;
tmp->nextlink = NULL;
Py_ssize_t *indices = lz->indices;
/* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
+ if (!_PyObject_IsUniquelyReferenced(result)) {
PyObject *old_result = result;
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), npools);
if (result == NULL)
}
} else {
/* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
+ if (!_PyObject_IsUniquelyReferenced(result)) {
PyObject *old_result = result;
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
if (result == NULL)
}
} else {
/* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
+ if (!_PyObject_IsUniquelyReferenced(result)) {
PyObject *old_result = result;
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
if (result == NULL)
goto empty;
/* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
+ if (!_PyObject_IsUniquelyReferenced(result)) {
PyObject *old_result = result;
result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r);
if (result == NULL)
return NULL;
if (lz->numactive == 0)
return NULL;
- if (Py_REFCNT(result) == 1) {
+ if (_PyObject_IsUniquelyReferenced(result)) {
Py_INCREF(result);
for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i);
return;
}
- if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) {
+ if (_PyObject_IsUniquelyReferenced(*pv) && PyBytes_CheckExact(*pv)) {
/* Only one reference, so we can resize in place */
Py_ssize_t oldsize;
Py_buffer wb;
Py_DECREF(v);
return 0;
}
- if (Py_REFCNT(v) != 1) {
+ if (!_PyObject_IsUniquelyReferenced(v)) {
if (oldsize < newsize) {
*pv = _PyBytes_FromSize(newsize, 0);
if (*pv) {
#endif
-static bool
-has_unique_reference(PyObject *op)
-{
-#ifdef Py_GIL_DISABLED
- return (_Py_IsOwnedByCurrentThread(op) &&
- op->ob_ref_local == 1 &&
- _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared) == 0);
-#else
- return Py_REFCNT(op) == 1;
-#endif
-}
-
static bool
acquire_iter_result(PyObject *result)
{
- if (has_unique_reference(result)) {
+ if (_PyObject_IsUniquelyReferenced(result)) {
Py_INCREF(result);
return true;
}
}
else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
result = di->di_result;
- if (Py_REFCNT(result) == 1) {
+ if (_PyObject_IsUniquelyReferenced(result)) {
PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
PyLongObject *x;
x = (PyLongObject *)*x_p;
- if (Py_REFCNT(x) == 1) {
+ if (_PyObject_IsUniquelyReferenced((PyObject *)x)) {
_PyLong_FlipSign(x);
return;
}
assert(size_a >= 0);
_PyLong_SetSignAndDigitCount(c, 1, size_a);
}
- else if (Py_REFCNT(a) == 1) {
+ else if (_PyObject_IsUniquelyReferenced((PyObject *)a)) {
c = (PyLongObject*)Py_NewRef(a);
}
else {
assert(size_a >= 0);
_PyLong_SetSignAndDigitCount(d, 1, size_a);
}
- else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
+ else if (_PyObject_IsUniquelyReferenced((PyObject *)b)
+ && size_a <= alloc_b) {
d = (PyLongObject*)Py_NewRef(b);
assert(size_a >= 0);
_PyLong_SetSignAndDigitCount(d, 1, size_a);
if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
return -1;
- if (Py_REFCNT(self) == 1 && self->fill == 0) {
+ if (_PyObject_IsUniquelyReferenced((PyObject *)self) && self->fill == 0) {
self->hash = -1;
if (iterable == NULL) {
return 0;
PySet_Add(PyObject *anyset, PyObject *key)
{
if (!PySet_Check(anyset) &&
- (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
+ (!PyFrozenSet_Check(anyset) || !_PyObject_IsUniquelyReferenced(anyset))) {
PyErr_BadInternalCall();
return -1;
}
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
{
PyObject **p;
- if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
+ if (!PyTuple_Check(op) || !_PyObject_IsUniquelyReferenced(op)) {
Py_XDECREF(newitem);
PyErr_BadInternalCall();
return -1;
v = (PyTupleObject *) *pv;
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
- (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
+ (Py_SIZE(v) != 0 && !_PyObject_IsUniquelyReferenced(*pv))) {
*pv = 0;
Py_XDECREF(v);
PyErr_BadInternalCall();
#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()
* 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;
}