8000 Fixups · python/cpython@0021bc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0021bc5

Browse files
committed
Fixups
1 parent 8c85fd3 commit 0021bc5

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

Include/internal/pycore_weakref.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj)
5353
// clear_weakref() was called
5454
return NULL;
5555
}
56+
5657
if (_is_dead(obj)) {
5758
return NULL;
5859
}

Modules/_weakref.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
3232
return _PyWeakref_GetWeakrefCountThreadsafe(object);
3333
}
3434

35+
3536
static int
3637
is_dead_weakref(PyObject *value)
3738
{

Objects/weakrefobject.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
* them, but we only hold borrowed references and they may also be destroyed
5555
* concurrently.
5656
*
57-
* We can probably solve this by using QSBR to ensure that the memory backing
58-
* either the weakref or the referenced object is not freed until we're ready,
59-
* but for now we've chosen to work around it using a few different strategies:
57+
* For now we've chosen to address this in a straightforward way:
6058
*
6159
* - The weakref's hash is protected using the weakref's per-object lock.
6260
* - The other mutable is protected by a striped lock owned by the interpreter.
@@ -79,6 +77,7 @@ Py_ssize_t
7977
_PyWeakref_GetWeakrefCount(PyWeakReference *head)
8078
{
8179
Py_ssize_t count = 0;
80+
8281
while (head != NULL) {
8382
++count;
8483
head = head->wr_next;
@@ -468,6 +467,12 @@ is_basic_proxy(PyWeakReference *proxy)
468467
return (proxy->wr_callback == NULL) && PyWeakref_CheckProxy(proxy);
469468
}
470469

470+
static int
471+
is_basic_ref_or_proxy(PyWeakReference *wr)
472+
{
473+
return is_basic_ref(wr) || is_basic_proxy(wr);
474+
}
475+
471476
/* Return the node that `newref` should be inserted after or NULL if `newref`
472477
* should be inserted at the head of the list.
473478
*/
@@ -1119,13 +1124,10 @@ PyObject_ClearWeakRefs(PyObject *object)
11191124
for (int done = 0; !done;) {
11201125
PyObject *callback = NULL;
11211126
LOCK_WEAKREFS(object);
1122-
if (*list != NULL && (*list)->wr_callback == NULL) {
1127+
if (*list != NULL && is_basic_ref_or_proxy(*list)) {
11231128
clear_weakref_lock_held(*list, &callback);
1124-
done = (*list == NULL);
1125-
}
1126-
else {
1127-
done = 1;
11281129
}
1130+
done = (*list == NULL) || !is_basic_ref_or_proxy(*list);
11291131
UNLOCK_WEAKREFS(object);
11301132
Py_XDECREF(callback);
11311133
}
@@ -1158,9 +1160,7 @@ PyObject_ClearWeakRefs(PyObject *object)
11581160
callback = NULL;
11591161
}
11601162
}
1161-
else {
1162-
done = 1;
1163-
}
1163+
done = (*list == NULL);
11641164
UNLOCK_WEAKREFS(object);
11651165

11661166
Py_XDECREF(callback);

0 commit comments

Comments
 (0)
0