8000 Get rid of GetWeakrefCountThreadsafe · python/cpython@700cf4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 700cf4c

Browse files
committed
Get rid of GetWeakrefCountThreadsafe
1 parent eb12c06 commit 700cf4c

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

Include/internal/pycore_weakref.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)
111111
return ret;
112112
}
113113

114-
// NB: In free-threaded builds the weakref list lock for the referenced object
115-
// must be held around calls to this function.
116-
extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
117-
118-
extern Py_ssize_t _PyWeakref_GetWeakrefCountThreadsafe(PyObject *obj);
114+
extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyObject *obj);
119115

120116
// Clear all the weak references to obj but leave their callbacks uncalled and
121117
// intact.

Modules/_weakref.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ static Py_ssize_t
2626
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
2727
/*[clinic end generated code: output=301806d59558ff3e input=7d4d04fcaccf64d5]*/
2828
{
29-
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
30-
return 0;
31-
}
32-
return _PyWeakref_GetWeakrefCountThreadsafe(object);
29+
return _PyWeakref_GetWeakrefCount(object);
3330
}
3431

3532

@@ -90,7 +87,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
9087
}
9188

9289
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
93-
Py_ssize_t count = _PyWeakref_GetWeakrefCountThreadsafe(object);
90+
Py_ssize_t count = _PyWeakref_GetWeakrefCount(object);
9491

9592
PyObject *result = PyList_New(count);
9693
if (result == NULL) {

Objects/weakrefobject.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,19 @@
3939

4040

4141
Py_ssize_t
42-
_PyWeakref_GetWeakrefCount(PyWeakReference *head)
42+
_PyWeakref_GetWeakrefCount(PyObject *obj)
4343
{
44-
Py_ssize_t count = 0;
44+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(obj))) {
45+
return 0;
46+
}
4547

48+
LOCK_WEAKREFS(obj);
49+
Py_ssize_t count = 0;
50+
PyWeakReference *head = *GET_WEAKREFS_LISTPTR(obj);
4651
while (head != NULL) {
4752
++count;
4853
head = head->wr_next;
4954
}
50-
return count;
51-
}
52-
53-
Py_ssize_t
54-
_PyWeakref_GetWeakrefCountThreadsafe(PyObject *obj)
55-
{
56-
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(obj))) {
57-
return 0;
58-
}
59-
Py_ssize_t count;
60-
LOCK_WEAKREFS(obj);
61-
count = _PyWeakref_GetWeakrefCount(*GET_WEAKREFS_LISTPTR(obj));
6255
UNLOCK_WEAKREFS(obj);
6356
return count;
6457
}
@@ -1114,7 +1107,7 @@ PyObject_ClearWeakRefs(PyObject *object)
11141107
}
11151108

11161109
/* Deal with non-canonical (subtypes or refs with callbacks) references. */
1117-
Py_ssize_t num_weakrefs = _PyWeakref_GetWeakrefCountThreadsafe(object);
1110+
Py_ssize_t num_weakrefs = _PyWeakref_GetWeakrefCount(object);
11181111
if (num_weakrefs == 0) {
11191112
return;
11201113
}
@@ -1168,7 +1161,7 @@ PyObject_ClearWeakRefs(PyObject *object)
11681161
}
11691162
if (*list != NULL) {
11701163
PyWeakReference *current = *list;
1171-
Py_ssize_t count = _PyWeakref_GetWeakrefCount(current);
1164+
Py_ssize_t count = _PyWeakref_GetWeakrefCount(object);
11721165
PyObject *exc = PyErr_GetRaisedException();
11731166

11741167
if (count == 1) {

0 commit comments

317E Comments
 (0)
0