Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,76 @@ static inline int PyUnstable_Object_IsUniquelyReferenced(PyObject *obj)
}
#endif

// gh-128926 added PyUnstable_TryIncRef() and PyUnstable_EnableTryIncRef() to
// Python 3.14.0a5. Adapted from _Py_TryIncref and _PyObject_SetMaybeWeakref.
#if PY_VERSION_HEX < 0x030E00A5
static inline int PyUnstable_TryIncRef(PyObject *op)
{
#ifndef Py_GIL_DISABLED
if (Py_REFCNT(op) > 0) {
Py_INCREF(op);
return 1;
}
return 0;
#else
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
local += 1;
if (local == 0) {
return 1;
}
if (_Py_IsOwnedByCurrentThread(op)) {
_Py_INCREF_STAT_INC();
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
#ifdef Py_REF_DEBUG
_Py_INCREF_IncRefTotal();
#endif
return 1;
}
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
for (;;) {
// If the shared refcount is zero and the object is either merged
// or may not have weak references, then we cannot incref it.
if (shared == 0 || shared == _Py_REF_MERGED) {
return 0;
}

if (_Py_atomic_compare_exchange_ssize(
&op->ob_ref_shared,
&shared,
shared + (1 << _Py_REF_SHARED_SHIFT))) {
#ifdef Py_REF_DEBUG
_Py_INCREF_IncRefTotal();
#endif
_Py_INCREF_STAT_INC();
return 1;
}
}
#endif
}

static inline void PyUnstable_EnableTryIncRef(PyObject *op)
{
#ifdef Py_GIL_DISABLED
if (_Py_IsImmortal(op)) {
return;
}
for (;;) {
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) {
// Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states.
return;
}
if (_Py_atomic_compare_exchange_ssize(
&op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
return;
}
}
#else
(void)op; // unused argument
#endif
}
#endif


#if PY_VERSION_HEX < 0x030F0000
static inline PyObject*
Expand Down
48 changes: 48 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,46 @@ test_tuple(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
return test_tuple_fromarray();
}

// Test adapted from CPython's _testcapi/object.c
static int MyObject_dealloc_called = 0;

static void
MyObject_dealloc(PyObject *op)
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind to rename it to TryIncref_dealloc() and rename also MyObject_dealloc_called?

{
// PyUnstable_TryIncRef should return 0 if object is being deallocated
assert(Py_REFCNT(op) == 0);
assert(!PyUnstable_TryIncRef(op));
assert(Py_REFCNT(op) == 0);

MyObject_dealloc_called++;
Py_TYPE(op)->tp_free(op);
}

static PyTypeObject MyType;

static PyObject*
test_try_incref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
MyObject_dealloc_called = 0;

PyObject *obj = PyObject_New(PyObject, &MyType);
if (obj == _Py_NULL) {
return _Py_NULL;
}

PyUnstable_EnableTryIncRef(obj);

Py_ssize_t refcount = Py_REFCNT(obj);
assert(PyUnstable_TryIncRef(obj));
assert(Py_REFCNT(obj) == refcount + 1);

Py_DECREF(obj);
Py_DECREF(obj);

assert(MyObject_dealloc_called == 1);
Py_RETURN_NONE;
}


static struct PyMethodDef methods[] = {
{"test_object", test_object, METH_NOARGS, _Py_NULL},
Expand Down Expand Up @@ -2504,6 +2544,7 @@ static struct PyMethodDef methods[] = {
{"test_uniquely_referenced", test_uniquely_referenced, METH_NOARGS, _Py_NULL},
{"test_byteswriter", test_byteswriter, METH_NOARGS, _Py_NULL},
{"test_tuple", test_tuple, METH_NOARGS, _Py_NULL},
{"test_try_incref", test_try_incref, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down Expand Up @@ -2532,6 +2573,13 @@ module_exec(PyObject *module)
return -1;
}
#endif
MyType.tp_name = "MyType";
Copy link
Member

Choose a reason for hiding this comment

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

Can you please use a less generic name, such as TryIncrefType? (for the variable name and the type name)

MyType.tp_basicsize = sizeof(PyObject);
MyType.tp_dealloc = MyObject_dealloc;
MyType.tp_free = PyObject_Del;
if (PyType_Ready(&MyType) < 0) {
return -1;
}
return 0;
}

Expand Down
Loading