Skip to content

Commit edd4879

Browse files
committed
Revert "clear weakrefs unconditionally in extension modules"
This reverts commit 9024079.
1 parent 76d2653 commit edd4879

20 files changed

+59
-22
lines changed

Modules/_collectionsmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,9 @@ deque_dealloc(PyObject *self)
15321532
Py_ssize_t i;
15331533

15341534
PyObject_GC_UnTrack(deque);
1535-
PyObject_ClearWeakRefs(self);
1535+
if (deque->weakreflist != NULL) {
1536+
PyObject_ClearWeakRefs(self);
1537+
}
15361538
if (deque->leftblock != NULL) {
15371539
(void)deque_clear(self);
15381540
assert(deque->leftblock != NULL);

Modules/_elementtree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ element_dealloc(PyObject *op)
690690
/* bpo-31095: UnTrack is needed before calling any callbacks */
691691
PyObject_GC_UnTrack(self);
692692

693-
PyObject_ClearWeakRefs(op);
693+
if (self->weakreflist != NULL)
694+
PyObject_ClearWeakRefs(op);
694695

695696
/* element_gc_clear clears all references and deallocates extra
696697
*/

Modules/_functoolsmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ partial_dealloc(PyObject *self)
351351
PyTypeObject *tp = Py_TYPE(self);
352352
/* bpo-31095: UnTrack is needed before calling any callbacks */
353353
PyObject_GC_UnTrack(self);
354-
PyObject_ClearWeakRefs(self);
354+
if (partialobject_CAST(self)->weakreflist != NULL) {
355+
PyObject_ClearWeakRefs(self);
356+
}
355357
(void)partial_clear(self);
356358
tp->tp_free(self);
357359
Py_DECREF(tp);
@@ -1619,7 +1621,9 @@ lru_cache_dealloc(PyObject *op)
16191621
PyTypeObject *tp = Py_TYPE(obj);
16201622
/* bpo-31095: UnTrack is needed before calling any callbacks */
16211623
PyObject_GC_UnTrack(obj);
1622-
PyObject_ClearWeakRefs(op);
1624+
if (obj->weakreflist != NULL) {
1625+
PyObject_ClearWeakRefs(op);
1626+
}
16231627

16241628
(void)lru_cache_tp_clear(op);
16251629
tp->tp_free(obj);

Modules/_io/bufferedio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ buffered_dealloc(PyObject *op)
421421
return;
422422
_PyObject_GC_UNTRACK(self);
423423
self->ok = 0;
424-
PyObject_ClearWeakRefs(op);
424+
if (self->weakreflist != NULL)
425+
PyObject_ClearWeakRefs(op);
425426
if (self->buffer) {
426427
PyMem_Free(self->buffer);
427428
self->buffer = NULL;

Modules/_io/fileio.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ fileio_dealloc(PyObject *op)
570570
PyMem_Free(self->stat_atopen);
571571
self->stat_atopen = NULL;
572572
}
573-
PyObject_ClearWeakRefs(op);
573+
if (self->weakreflist != NULL) {
574+
PyObject_ClearWeakRefs(op);
575+
}
574576
(void)fileio_clear(op);
575577

576578
PyTypeObject *tp = Py_TYPE(op);

Modules/_io/iobase.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ iobase_dealloc(PyObject *op)
383383
}
384384
PyTypeObject *tp = Py_TYPE(self);
385385
_PyObject_GC_UNTRACK(self);
386-
PyObject_ClearWeakRefs(op);
386+
if (self->weakreflist != NULL)
387+
PyObject_ClearWeakRefs(op);
387388
Py_CLEAR(self->dict);
388389
tp->tp_free(self);
389390
Py_DECREF(tp);

Modules/_io/textio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,8 @@ textiowrapper_dealloc(PyObject *op)
14691469
return;
14701470
self->ok = 0;
14711471
_PyObject_GC_UNTRACK(self);
1472-
PyObject_ClearWeakRefs(op);
1472+
if (self->weakreflist != NULL)
1473+
PyObject_ClearWeakRefs(op);
14731474
(void)textiowrapper_clear(op);
14741475
tp->tp_free(self);
14751476
Py_DECREF(tp);

Modules/_io/winconsoleio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ winconsoleio_dealloc(PyObject *op)
518518
if (_PyIOBase_finalize(op) < 0)
519519
return;
520520
_PyObject_GC_UNTRACK(self);
521-
PyObject_ClearWeakRefs(op);
521+
if (self->weakreflist != NULL)
522+
PyObject_ClearWeakRefs(op);
522523
Py_CLEAR(self->dict);
523524
tp->tp_free(self);
524525
Py_DECREF(tp);

Modules/_queuemodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ simplequeue_dealloc(PyObject *op)
221221

222222
PyObject_GC_UnTrack(self);
223223
(void)simplequeue_clear(op);
224-
PyObject_ClearWeakRefs(op);
224+
if (self->weakreflist != NULL) {
225+
PyObject_ClearWeakRefs(op);
226+
}
225227
tp->tp_free(self);
226228
Py_DECREF(tp);
227229
}

Modules/_sqlite/blob.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ blob_dealloc(PyObject *op)
5656

5757
close_blob(self);
5858

59-
PyObject_ClearWeakRefs(op);
59+
if (self->in_weakreflist != NULL) {
60+
PyObject_ClearWeakRefs(op);
61+
}
6062
(void)tp->tp_clear(op);
6163
tp->tp_free(self);
6264
Py_DECREF(tp);

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ cursor_dealloc(PyObject *op)
185185
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
186186
PyTypeObject *tp = Py_TYPE(self);
187187
PyObject_GC_UnTrack(self);
188-
PyObject_ClearWeakRefs(op);
188+
if (self->in_weakreflist != NULL) {
189+
PyObject_ClearWeakRefs(op);
190+
}
189191
(void)tp->tp_clear(op);
190192
tp->tp_free(self);
191193
Py_DECREF(tp);

Modules/_sre/sre.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,10 @@ pattern_dealloc(PyObject *self)
736736
{
737737
PyTypeObject *tp = Py_TYPE(self);
738738
PyObject_GC_UnTrack(self);
739-
PyObject_ClearWeakRefs(self);
739+
PatternObject *obj = _PatternObject_CAST(self);
740+
if (obj->weakreflist != NULL) {
741+
PyObject_ClearWeakRefs(self);
742+
}
740743
(void)pattern_clear(self);
741744
tp->tp_free(self);
742745
Py_DECREF(tp);

Modules/_struct.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,9 @@ s_dealloc(PyObject *op)
17941794
PyStructObject *s = PyStructObject_CAST(op);
17951795
PyTypeObject *tp = Py_TYPE(s);
17961796
PyObject_GC_UnTrack(s);
1797-
PyObject_ClearWeakRefs(op);
1797+
if (s->weakreflist != NULL) {
1798+
PyObject_ClearWeakRefs(op);
1799+
}
17981800
if (s->s_codes != NULL) {
17991801
PyMem_Free(s->s_codes);
18001802
}

Modules/_testcapi/heaptype.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@ heapctypewithweakref_dealloc(PyObject *op)
10341034
{
10351035
HeapCTypeWithWeakrefObject *self = (HeapCTypeWithWeakrefObject*)op;
10361036
PyTypeObject *tp = Py_TYPE(self);
1037-
PyObject_ClearWeakRefs((PyObject *) self);
1037+
if (self->weakreflist != NULL)
1038+
PyObject_ClearWeakRefs((PyObject *) self);
10381039
Py_XDECREF(self->weakreflist);
10391040
PyObject_Free(self);
10401041
Py_DECREF(tp);

Modules/_testlimitedcapi/heaptype_relative.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ heapctypewithrelativeweakref_dealloc(PyObject* self)
297297
{
298298
PyTypeObject *tp = Py_TYPE(self);
299299
HeapCTypeWithWeakrefStruct *data = PyObject_GetTypeData(self, tp);
300-
PyObject_ClearWeakRefs(self);
300+
if (data->weakreflist != NULL) {
301+
PyObject_ClearWeakRefs(self);
302+
}
301303
Py_XDECREF(data->weakreflist);
302304
PyObject_Free(self);
303305
Py_DECREF(tp);

Modules/_threadmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,9 @@ static void
13651365
localdummy_dealloc(PyObject *op)
13661366
{
13671367
localdummyobject *self = localdummyobject_CAST(op);
1368-
PyObject_ClearWeakRefs(op);
1368+
if (self->weakreflist != NULL) {
1369+
PyObject_ClearWeakRefs(op);
1370+
}
13691371
PyTypeObject *tp = Py_TYPE(self);
13701372
tp->tp_free(self);
13711373
Py_DECREF(tp);
@@ -1535,7 +1537,9 @@ local_dealloc(PyObject *op)
15351537
localobject *self = localobject_CAST(op);
15361538
/* Weakrefs must be invalidated right now, otherwise they can be used
15371539
from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
1538-
PyObject_ClearWeakRefs(op);
1540+
if (self->weakreflist != NULL) {
1541+
PyObject_ClearWeakRefs(op);
1542+
}
15391543
PyObject_GC_UnTrack(self);
15401544
(void)local_clear(op);
15411545
PyTypeObject *tp = Py_TYPE(self);

Modules/_zoneinfo.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ zoneinfo_dealloc(PyObject *obj_self)
375375
PyTypeObject *tp = Py_TYPE(self);
376376
PyObject_GC_UnTrack(self);
377377

378-
PyObject_ClearWeakRefs(obj_self);
378+
if (self->weakreflist != NULL) {
379+
PyObject_ClearWeakRefs(obj_self);
380+
}
379381

380382
if (self->trans_list_utc != NULL) {
381383
PyMem_Free(self->trans_list_utc);

Modules/arraymodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,9 @@ array_dealloc(PyObject *op)
728728
PyObject_GC_UnTrack(op);
729729

730730
arrayobject *self = arrayobject_CAST(op);
731-
PyObject_ClearWeakRefs(op);
731+
if (self->weakreflist != NULL) {
732+
PyObject_ClearWeakRefs(op);
733+
}
732734
if (self->ob_item != NULL) {
733735
PyMem_Free(self->ob_item);
734736
}

Modules/itertoolsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,8 @@ static int
10181018
tee_clear(PyObject *op)
10191019
{
10201020
teeobject *to = teeobject_CAST(op);
1021-
PyObject_ClearWeakRefs(op);
1021+
if (to->weakreflist != NULL)
1022+
PyObject_ClearWeakRefs(op);
10221023
Py_CLEAR(to->dataobj);
10231024
return 0;
10241025
}

Modules/mmapmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ mmap_object_dealloc(PyObject *op)
163163
Py_END_ALLOW_THREADS
164164
#endif /* UNIX */
165165

166-
PyObject_ClearWeakRefs(op);
166+
if (m_obj->weakreflist != NULL)
167+
PyObject_ClearWeakRefs(op);
167168

168169
tp->tp_free(m_obj);
169170
Py_DECREF(tp);

0 commit comments

Comments
 (0)