Skip to content

Commit 42a0743

Browse files
committed
use macros to clear weak refs
1 parent 83c3c32 commit 42a0743

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);

0 commit comments

Comments
 (0)