8000 Add `_Py_TryIncref` · python/cpython@37f9ceb · GitHub
[go: up one dir, main page]

Skip to content

Commit 37f9ceb

Browse files
committed
Add _Py_TryIncref
1 parent d742ed1 commit 37f9ceb

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

Include/internal/pycore_object.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,21 @@ _PyObject_SetMaybeWeakref(PyObject *op)
527527

528528
#endif
529529

530+
/* Tries to incref op and returns 1 if successful or 0 otherwise. */
531+
static inline int
532+
_Py_TryIncref(PyObject *op)
533+
{
534+
#ifdef Py_GIL_DISABLED
535+
return _Py_TryIncrefFast(op) || _Py_TryIncRefShared(op);
536+
#else
537+
if (Py_REFCNT(op) > 0) {
538+
Py_INCREF(op);
539+
return 1;
540+
}
541+
return 0;
542+
#endif
543+
}
544+
530545
#ifdef Py_REF_DEBUG
531546
extern void _PyInterpreterState_FinalizeRefTotal(PyInterpreterState *);
532547
extern void _Py_FinalizeRefTotal(_PyRuntimeState *);

Include/internal/pycore_weakref.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,20 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj)
5757
return NULL;
5858
}
5959

60-
#if !defined(Py_GIL_DISABLED)
61-
if (_is_dead(obj)) {
62-
return NULL;
63-
}
64-
assert(Py_REFCNT(obj) > 0);
65-
return Py_NewRef(obj);
66-
#else
6760
LOCK_WEAKREFS(obj);
61+
#ifdef Py_GIL_DISABLED
6862
if (ref->wr_object == Py_None) {
6963
// clear_weakref() was called
7064
UNLOCK_WEAKREFS(obj);
7165
return NULL;
7266
}
73-
if (_Py_TryIncrefCompare(&obj, obj)) {
67+
#endif
68+
if (_Py_TryIncref(obj)) {
7469
UNLOCK_WEAKREFS(obj);
7570
return obj;
7671
}
7772
UNLOCK_WEAKREFS(obj);
7873
return NULL;
79-
#endif
8074
}
8175

8276
static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)

Modules/_weakref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
100100
PyWeakReference *current = *list;
101101
// Weakrefs may be added or removed since the count was computed.
102102
while (num_added < count && current != NULL) {
103-
if (_Py_TryIncrefCompare((PyObject**) &current, (PyObject *) current)) {
103+
if (_Py_TryIncref((PyObject *) current)) {
104104
PyList_SET_ITEM(result, num_added, current);
105105
num_added++;
106106
}

Objects/weakrefobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ try_reuse_basic_ref(PyWeakReference *list, PyTypeObject *type, PyObject *callbac
358358
}
359359

360360
#ifdef Py_GIL_DISABLED
361-
if (cand != NULL && !_Py_TryIncrefCompare((PyObject **) &cand, (PyObject *) cand)) {
361+
if (cand != NULL && !_Py_TryIncref((PyObject *) cand)) {
362362
cand = NULL;
363363
}
364364
#else
@@ -1077,7 +1077,7 @@ PyObject_ClearWeakRefs(PyObject *object)
10771077
PyWeakReference *cur = *list;
10781078
if (cur != NULL) {
10791079
clear_weakref_lock_held(cur, &callback);
1080-
if (_Py_TryIncrefCompare((PyObject **) &cur, (PyObject *) cur)) {
1080+
if (_Py_TryIncref((PyObject *) cur)) {
10811081
assert(num_items / 2 < num_weakrefs);
10821082
PyTuple_SET_ITEM(tuple, num_items, (PyObject *) cur);
10831083
PyTuple_SET_ITEM(tuple, num_items + 1, callback);

0 commit comments

Comments
 (0)
0