Skip to content

Commit 6feeca1

Browse files
committed
use macros to clear weak refs
1 parent a176ac2 commit 6feeca1

32 files changed

+76
-33
lines changed

Include/internal/pycore_weakref.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ extern "C" {
2929
PyMutex_LockFlags(wr->weakrefs_lock, _Py_LOCK_DONT_DETACH)
3030
#define UNLOCK_WEAKREFS_FOR_WR(wr) PyMutex_Unlock(wr->weakrefs_lock)
3131

32+
#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
33+
PyObject_ClearWeakRefs(obj)
34+
3235
#else
3336

3437
#define LOCK_WEAKREFS(obj)
@@ -37,6 +40,13 @@ extern "C" {
3740
#define LOCK_WEAKREFS_FOR_WR(wr)
3841
#define UNLOCK_WEAKREFS_FOR_WR(wr)
3942

43+
#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
44+
do { \
45+
if (weakref_list != NULL) { \
46+
PyObject_ClearWeakRefs(obj); \
47+
} \
48+
} while (0)
49+
4050
#endif
4151

4252
static inline int _is_dead(PyObject *obj)

Modules/_collectionsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "pycore_moduleobject.h" // _PyModule_GetState()
66
#include "pycore_pyatomic_ft_wrappers.h"
77
#include "pycore_typeobject.h" // _PyType_GetModuleState()
8+
#include "pycore_weakref.h"
89

910
#include <stddef.h>
1011

@@ -1532,7 +1533,7 @@ deque_dealloc(PyObject *self)
15321533
Py_ssize_t i;
15331534

15341535
PyObject_GC_UnTrack(deque);
1535-
PyObject_ClearWeakRefs(self);
1536+
FT_CLEAR_WEAKREFS(self, deque->weakreflist);
15361537
if (deque->leftblock != NULL) {
15371538
(void)deque_clear(self);
15381539
assert(deque->leftblock != NULL);

Modules/_elementtree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "Python.h"
1919
#include "pycore_pyhash.h" // _Py_HashSecret
20+
#include "pycore_weakref.h"
2021

2122
#include <stddef.h> // offsetof()
2223
#include "expat.h"
@@ -690,7 +691,7 @@ element_dealloc(PyObject *op)
690691
/* bpo-31095: UnTrack is needed before calling any callbacks */
691692
PyObject_GC_UnTrack(self);
692693

693-
PyObject_ClearWeakRefs(op);
694+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
694695

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

Modules/_functoolsmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_pyatomic_ft_wrappers.h"
88
#include "pycore_pystate.h" // _PyThreadState_GET()
99
#include "pycore_tuple.h" // _PyTuple_ITEMS()
10+
#include "pycore_weakref.h"
1011

1112

1213
#include "clinic/_functoolsmodule.c.h"
@@ -351,7 +352,7 @@ partial_dealloc(PyObject *self)
351352
PyTypeObject *tp = Py_TYPE(self);
352353
/* bpo-31095: UnTrack is needed before calling any callbacks */
353354
PyObject_GC_UnTrack(self);
354-
PyObject_ClearWeakRefs(self);
355+
FT_CLEAR_WEAKREFS(self, partialobject_CAST(self)->weakreflist);
355356
(void)partial_clear(self);
356357
tp->tp_free(self);
357358
Py_DECREF(tp);
@@ -1619,7 +1620,7 @@ lru_cache_dealloc(PyObject *op)
16191620
PyTypeObject *tp = Py_TYPE(obj);
16201621
/* bpo-31095: UnTrack is needed before calling any callbacks */
16211622
PyObject_GC_UnTrack(obj);
1622-
PyObject_ClearWeakRefs(op);
1623+
FT_CLEAR_WEAKREFS(op, obj->weakreflist);
16231624

16241625
(void)lru_cache_tp_clear(op);
16251626
tp->tp_free(obj);

Modules/_io/bufferedio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
1414
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
1515
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
16+
#include "pycore_weakref.h"
1617

1718
#include "_iomodule.h"
1819

@@ -421,7 +422,7 @@ buffered_dealloc(PyObject *op)
421422
return;
422423
_PyObject_GC_UNTRACK(self);
423424
self->ok = 0;
424-
PyObject_ClearWeakRefs(op);
425+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
425426
if (self->buffer) {
426427
PyMem_Free(self->buffer);
427428
self->buffer = NULL;
@@ -2311,7 +2312,7 @@ bufferedrwpair_dealloc(PyObject *op)
23112312
rwpair *self = rwpair_CAST(op);
23122313
PyTypeObject *tp = Py_TYPE(self);
23132314
_PyObject_GC_UNTRACK(self);
2314-
PyObject_ClearWeakRefs(op);
2315+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
23152316
(void)bufferedrwpair_clear(op);
23162317
tp->tp_free(self);
23172318
Py_DECREF(tp);

Modules/_io/bytesio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pycore_object.h"
44
#include "pycore_pyatomic_ft_wrappers.h"
55
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
6+
#include "pycore_weakref.h"
67

78
#include <stddef.h> // offsetof()
89
#include "_iomodule.h"
@@ -979,7 +980,7 @@ bytesio_dealloc(PyObject *op)
979980
}
980981
Py_CLEAR(self->buf);
981982
Py_CLEAR(self->dict);
982-
PyObject_ClearWeakRefs(op);
983+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
983984
tp->tp_free(self);
984985
Py_DECREF(tp);
985986
}

Modules/_io/fileio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
55
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
66
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
7+
#include "pycore_weakref.h"
78

89
#include <stdbool.h> // bool
910
#ifdef HAVE_UNISTD_H
@@ -570,7 +571,7 @@ fileio_dealloc(PyObject *op)
570571
PyMem_Free(self->stat_atopen);
571572
self->stat_atopen = NULL;
572573
}
573-
PyObject_ClearWeakRefs(op);
574+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
574575
(void)fileio_clear(op);
575576

576577
PyTypeObject *tp = Py_TYPE(op);

Modules/_io/iobase.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pycore_long.h" // _PyLong_GetOne()
1515
#include "pycore_object.h" // _PyType_HasFeature()
1616
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
17+
#include "pycore_weakref.h"
1718

1819
#include <stddef.h> // offsetof()
1920
#include "_iomodule.h"
@@ -383,7 +384,7 @@ iobase_dealloc(PyObject *op)
383384
}
384385
PyTypeObject *tp = Py_TYPE(self);
385386
_PyObject_GC_UNTRACK(self);
386-
PyObject_ClearWeakRefs(op);
387+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
387388
Py_CLEAR(self->dict);
388389
tp->tp_free(self);
389390
Py_DECREF(tp);

Modules/_io/stringio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include <stddef.h> // offsetof()
33
#include "pycore_object.h"
4+
#include "pycore_weakref.h"
45
#include "_iomodule.h"
56

67
/* Implementation note: the buffer is always at least one character longer
@@ -638,7 +639,7 @@ stringio_dealloc(PyObject *op)
638639
}
639640
PyUnicodeWriter_Discard(self->writer);
640641
(void)stringio_clear(op);
641-
PyObject_ClearWeakRefs(op);
642+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
642643
tp->tp_free(self);
643644
Py_DECREF(tp);
644645
}

Modules/_io/textio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
1717
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1818
#include "pycore_unicodeobject.h" // _PyUnicode_AsASCIIString()
19+
#include "pycore_weakref.h"
1920

2021
#include "_iomodule.h"
2122

@@ -1469,7 +1470,7 @@ textiowrapper_dealloc(PyObject *op)
14691470
return;
14701471
self->ok = 0;
14711472
_PyObject_GC_UNTRACK(self);
1472-
PyObject_ClearWeakRefs(op);
1473+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
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
@@ -10,6 +10,7 @@
1010
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
1111
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
1212
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
13+
#include "pycore_weakref.h"
1314

1415
#ifdef HAVE_WINDOWS_CONSOLE_IO
1516

@@ -518,7 +519,7 @@ winconsoleio_dealloc(PyObject *op)
518519
if (_PyIOBase_finalize(op) < 0)
519520
return;
520521
_PyObject_GC_UNTRACK(self);
521-
PyObject_ClearWeakRefs(op);
522+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
522523
Py_CLEAR(self->dict);
523524
tp->tp_free(self);
524525
Py_DECREF(tp);

Modules/_queuemodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_moduleobject.h" // _PyModule_GetState()
88
#include "pycore_parking_lot.h"
99
#include "pycore_time.h" // _PyTime_FromSecondsObject()
10+
#include "pycore_weakref.h"
1011

1112
#include <stdbool.h>
1213
#include <stddef.h> // offsetof()
@@ -221,7 +222,7 @@ simplequeue_dealloc(PyObject *op)
221222

222223
PyObject_GC_UnTrack(self);
223224
(void)simplequeue_clear(op);
224-
PyObject_ClearWeakRefs(op);
225+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
225226
tp->tp_free(self);
226227
Py_DECREF(tp);
227228
}

Modules/_sqlite/blob.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "blob.h"
66
#include "util.h"
7+
#include "pycore_weakref.h"
78

89
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
910
#include "clinic/blob.c.h"
@@ -56,7 +57,7 @@ blob_dealloc(PyObject *op)
5657

5758
close_blob(self);
5859

59-
PyObject_ClearWeakRefs(op);
60+
FT_CLEAR_WEAKREFS(op, self->in_weakreflist);
6061
(void)tp->tp_clear(op);
6162
tp->tp_free(self);
6263
Py_DECREF(tp);

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "util.h"
3232

3333
#include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
34+
#include "pycore_weakref.h"
3435

3536
typedef enum {
3637
TYPE_LONG,
@@ -185,7 +186,7 @@ cursor_dealloc(PyObject *op)
185186
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
186187
PyTypeObject *tp = Py_TYPE(self);
187188
PyObject_GC_UnTrack(self);
188-
PyObject_ClearWeakRefs(op);
189+
FT_CLEAR_WEAKREFS(op, self->in_weakreflist);
189190
(void)tp->tp_clear(op);
190191
tp->tp_free(self);
191192
Py_DECREF(tp);

Modules/_sre/sre.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static const char copyright[] =
4444
#include "pycore_long.h" // _PyLong_GetZero()
4545
#include "pycore_moduleobject.h" // _PyModule_GetState()
4646
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
47+
#include "pycore_weakref.h"
4748

4849
#include "sre.h" // SRE_CODE
4950

@@ -736,7 +737,8 @@ pattern_dealloc(PyObject *self)
736737
{
737738
PyTypeObject *tp = Py_TYPE(self);
738739
PyObject_GC_UnTrack(self);
739-
PyObject_ClearWeakRefs(self);
740+
PatternObject *obj = _PatternObject_CAST(self);
741+
FT_CLEAR_WEAKREFS(self, obj->weakreflist);
740742
(void)pattern_clear(self);
741743
tp->tp_free(self);
742744
Py_DECREF(tp);

Modules/_struct.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_bytesobject.h" // _PyBytesWriter
1212
#include "pycore_long.h" // _PyLong_AsByteArray()
1313
#include "pycore_moduleobject.h" // _PyModule_GetState()
14+
#include "pycore_weakref.h"
1415

1516
#include <stddef.h> // offsetof()
1617

@@ -1794,7 +1795,7 @@ s_dealloc(PyObject *op)
17941795
PyStructObject *s = PyStructObject_CAST(op);
17951796
PyTypeObject *tp = Py_TYPE(s);
17961797
PyObject_GC_UnTrack(s);
1797-
PyObject_ClearWeakRefs(op);
1798+
FT_CLEAR_WEAKREFS(op, s->weakreflist);
17981799
if (s->s_codes != NULL) {
17991800
PyMem_Free(s->s_codes);
18001801
}

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(op);
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/_zoneinfo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_long.h" // _PyLong_GetOne()
88
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
99
#include "pycore_typeobject.h" // _PyType_GetModuleState()
10+
#include "pycore_weakref.h"
1011

1112
#include "datetime.h" // PyDateTime_TZInfo
1213

@@ -375,7 +376,7 @@ zoneinfo_dealloc(PyObject *obj_self)
375376
PyTypeObject *tp = Py_TYPE(self);
376377
PyObject_GC_UnTrack(self);
377378

378-
PyObject_ClearWeakRefs(obj_self);
379+
FT_CLEAR_WEAKREFS(obj_self, self->weakreflist);
379380

380381
if (self->trans_list_utc != NULL) {
381382
PyMem_Free(self->trans_list_utc);

Modules/arraymodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
1414
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
1515
#include "pycore_moduleobject.h" // _PyModule_GetState()
16+
#include "pycore_weakref.h"
1617

1718
#include <stddef.h> // offsetof()
1819
#include <stdbool.h>
@@ -728,7 +729,7 @@ array_dealloc(PyObject *op)
728729
PyObject_GC_UnTrack(op);
729730

730731
arrayobject *self = arrayobject_CAST(op);
731-
PyObject_ClearWeakRefs(op);
732+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
732733
if (self->ob_item != NULL) {
733734
PyMem_Free(self->ob_item);
734735
}

Modules/mmapmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <Python.h>
2626
#include "pycore_bytesobject.h" // _PyBytes_Find()
2727
#include "pycore_fileutils.h" // _Py_stat_struct
28+
#include "pycore_weakref.h"
2829

2930
#include <stddef.h> // offsetof()
3031
#ifndef MS_WINDOWS
@@ -163,7 +164,7 @@ mmap_object_dealloc(PyObject *op)
163164
Py_END_ALLOW_THREADS
164165
#endif /* UNIX */
165166

166-
PyObject_ClearWeakRefs(op);
167+
FT_CLEAR_WEAKREFS(op, m_obj->weakreflist);
167168

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

Objects/classobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_object.h"
88
#include "pycore_pyerrors.h"
99
#include "pycore_pystate.h" // _PyThreadState_GET()
10+
#include "pycore_weakref.h"
1011

1112

1213
#include "clinic/classobject.c.h"
@@ -245,7 +246,7 @@ method_dealloc(PyObject *self)
245246
{
246247
PyMethodObject *im = _PyMethodObject_CAST(self);
247248
_PyObject_GC_UNTRACK(im);
248-
PyObject_ClearWeakRefs(self);
249+
FT_CLEAR_WEAKREFS(self, im->im_weakreflist);
249250
Py_DECREF(im->im_func);
250251
Py_XDECREF(im->im_self);
251252
assert(Py_IS_TYPE(self, &PyMethod_Type));

0 commit comments

Comments
 (0)