Skip to content

Commit 61c2357

Browse files
ZeroIntensitylkollar
authored andcommitted
pythongh-138342: Use a common utility for visiting an object's type (pythonGH-138343)
Add `_PyObject_VisitType` in place of `tp_traverse` functions that only visit the object's type.
1 parent f009159 commit 61c2357

24 files changed

+41
-203
lines changed

Include/cpython/object.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,7 @@ PyAPI_FUNC(int) PyUnstable_TryIncRef(PyObject *);
491491
PyAPI_FUNC(void) PyUnstable_EnableTryIncRef(PyObject *);
492492

493493
PyAPI_FUNC(int) PyUnstable_Object_IsUniquelyReferenced(PyObject *);
494+
495+
/* Utility for the tp_traverse slot of mutable heap types that have no other
496+
* references. */
497+
PyAPI_FUNC(int) _PyObject_VisitType(PyObject *op, visitproc visit, void *arg);

Modules/_dbmmodule.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ newdbmobject(_dbm_state *state, const char *file, int flags, int mode)
9696
}
9797

9898
/* Methods */
99-
static int
100-
dbm_traverse(PyObject *dp, visitproc visit, void *arg)
101-
{
102-
Py_VISIT(Py_TYPE(dp));
103-
return 0;
104-
}
10599

106100
static void
107101
dbm_dealloc(PyObject *self)
@@ -540,7 +534,7 @@ static PyMethodDef dbm_methods[] = {
540534

541535
static PyType_Slot dbmtype_spec_slots[] = {
542536
{Py_tp_dealloc, dbm_dealloc},
543-
{Py_tp_traverse, dbm_traverse},
537+
{Py_tp_traverse, _PyObject_VisitType},
544538
{Py_tp_methods, dbm_methods},
545539
{Py_sq_contains, dbm_contains},
546540
{Py_mp_length, dbm_length},

Modules/_decimal/_decimal.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,6 @@ signaldict_setitem(PyObject *self, PyObject *key, PyObject *value)
746746
return 0;
747747
}
748748

749-
static int
750-
signaldict_traverse(PyObject *self, visitproc visit, void *arg)
751-
{
752-
Py_VISIT(Py_TYPE(self));
753-
return 0;
754-
}
755-
756749
static void
757750
signaldict_dealloc(PyObject *self)
758751
{
@@ -845,7 +838,7 @@ static PyMethodDef signaldict_methods[] = {
845838

846839
static PyType_Slot signaldict_slots[] = {
847840
{Py_tp_dealloc, signaldict_dealloc},
848-
{Py_tp_traverse, signaldict_traverse},
841+
{Py_tp_traverse, _PyObject_VisitType},
849842
{Py_tp_repr, signaldict_repr},
850843
{Py_tp_hash, PyObject_HashNotImplemented},
851844
{Py_tp_getattro, PyObject_GenericGetAttr},
@@ -2194,13 +2187,6 @@ PyDecType_New(decimal_state *state, PyTypeObject *type)
21942187
}
21952188
#define dec_alloc(st) PyDecType_New(st, (st)->PyDec_Type)
21962189

2197-
static int
2198-
dec_traverse(PyObject *dec, visitproc visit, void *arg)
2199-
{
2200-
Py_VISIT(Py_TYPE(dec));
2201-
return 0;
2202-
}
2203-
22042190
static void
22052191
dec_dealloc(PyObject *dec)
22062192
{
@@ -6032,7 +6018,7 @@ static PyType_Slot dec_slots[] = {
60326018
{Py_tp_token, Py_TP_USE_SPEC},
60336019
{Py_tp_dealloc, dec_dealloc},
60346020
{Py_tp_getattro, PyObject_GenericGetAttr},
6035-
{Py_tp_traverse, dec_traverse},
6021+
{Py_tp_traverse, _PyObject_VisitType},
60366022
{Py_tp_repr, dec_repr},
60376023
{Py_tp_hash, dec_hash},
60386024
{Py_tp_str, dec_str},

Modules/_functoolsmodule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,13 @@ placeholder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
108108
return placeholder;
109109
}
110110

111-
static int
112-
placeholder_traverse(PyObject *self, visitproc visit, void *arg)
113-
{
114-
Py_VISIT(Py_TYPE(self));
115-
return 0;
116-
}
117-
118111
static PyType_Slot placeholder_type_slots[] = {
119112
{Py_tp_dealloc, placeholder_dealloc},
120113
{Py_tp_repr, placeholder_repr},
121114
{Py_tp_doc, (void *)placeholder_doc},
122115
{Py_tp_methods, placeholder_methods},
123116
{Py_tp_new, placeholder_new},
124-
{Py_tp_traverse, placeholder_traverse},
117+
{Py_tp_traverse, _PyObject_VisitType},
125118
{0, 0}
126119
};
127120

Modules/_gdbmmodule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ newgdbmobject(_gdbm_state *state, const char *file, int flags, int mode)
122122
}
123123

124124
/* Methods */
125-
static int
126-
gdbm_traverse(PyObject *op, visitproc visit, void *arg)
127-
{
128-
Py_VISIT(Py_TYPE(op));
129-
return 0;
130-
}
131-
132125
static void
133126
gdbm_dealloc(PyObject *op)
134127
{
@@ -714,7 +707,7 @@ static PyMethodDef gdbm_methods[] = {
714707

715708
static PyType_Slot gdbmtype_spec_slots[] = {
716709
{Py_tp_dealloc, gdbm_dealloc},
717-
{Py_tp_traverse, gdbm_traverse},
710+
{Py_tp_traverse, _PyObject_VisitType},
718711
{Py_tp_methods, gdbm_methods},
719712
{Py_sq_contains, gdbm_contains},
720713
{Py_mp_length, gdbm_length},

Modules/_multiprocessing/semaphore.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,6 @@ _multiprocessing_SemLock___exit___impl(SemLockObject *self,
720720
return _multiprocessing_SemLock_release_impl(self);
721721
}
722722

723-
static int
724-
semlock_traverse(PyObject *s, visitproc visit, void *arg)
725-
{
726-
Py_VISIT(Py_TYPE(s));
727-
return 0;
728-
}
729-
730723
/*
731724
* Semaphore methods
732725
*/
@@ -773,7 +766,7 @@ static PyType_Slot _PyMp_SemLockType_slots[] = {
773766
{Py_tp_members, semlock_members},
774767
{Py_tp_alloc, PyType_GenericAlloc},
775768
{Py_tp_new, _multiprocessing_SemLock},
776-
{Py_tp_traverse, semlock_traverse},
769+
{Py_tp_traverse, _PyObject_VisitType},
777770
{Py_tp_free, PyObject_GC_Del},
778771
{Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
779772
{0, 0},

Modules/_pickle.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,6 @@ typedef struct {
413413

414414
#define Pdata_CAST(op) ((Pdata *)(op))
415415

416-
static int
417-
Pdata_traverse(PyObject *self, visitproc visit, void *arg)
418-
{
419-
Py_VISIT(Py_TYPE(self));
420-
return 0;
421-
}
422-
423416
static void
424417
Pdata_dealloc(PyObject *op)
425418
{
@@ -437,7 +430,7 @@ Pdata_dealloc(PyObject *op)
437430

438431
static PyType_Slot pdata_slots[] = {
439432
{Py_tp_dealloc, Pdata_dealloc},
440-
{Py_tp_traverse, Pdata_traverse},
433+
{Py_tp_traverse, _PyObject_VisitType},
441434
{0, NULL},
442435
};
443436

Modules/_sqlite/prepare_protocol.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ pysqlite_prepare_protocol_init(PyObject *self, PyObject *args, PyObject *kwargs)
2929
return 0;
3030
}
3131

32-
static int
33-
pysqlite_prepare_protocol_traverse(PyObject *self, visitproc visit, void *arg)
34-
{
35-
Py_VISIT(Py_TYPE(self));
36-
return 0;
37-
}
38-
3932
static void
4033
pysqlite_prepare_protocol_dealloc(PyObject *self)
4134
{
@@ -50,7 +43,7 @@ PyDoc_STRVAR(doc, "PEP 246 style object adaption protocol type.");
5043
static PyType_Slot type_slots[] = {
5144
{Py_tp_dealloc, pysqlite_prepare_protocol_dealloc},
5245
{Py_tp_init, pysqlite_prepare_protocol_init},
53-
{Py_tp_traverse, pysqlite_prepare_protocol_traverse},
46+
{Py_tp_traverse, _PyObject_VisitType},
5447
{Py_tp_doc, (void *)doc},
5548
{0, NULL},
5649
};

Modules/_sqlite/statement.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ stmt_dealloc(PyObject *op)
116116
Py_DECREF(tp);
117117
}
118118

119-
static int
120-
stmt_traverse(PyObject *self, visitproc visit, void *arg)
121-
{
122-
Py_VISIT(Py_TYPE(self));
123-
return 0;
124-
}
125-
126119
/*
127120
* Strip leading whitespace and comments from incoming SQL (null terminated C
128121
* string) and return a pointer to the first non-whitespace, non-comment
@@ -183,7 +176,7 @@ lstrip_sql(const char *sql)
183176

184177
static PyType_Slot stmt_slots[] = {
185178
{Py_tp_dealloc, stmt_dealloc},
186-
{Py_tp_traverse, stmt_traverse},
179+
{Py_tp_traverse, _PyObject_VisitType},
187180
{0, NULL},
188181
};
189182

Modules/_ssl.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,13 +5692,6 @@ _ssl_MemoryBIO_impl(PyTypeObject *type)
56925692
return (PyObject *) self;
56935693
}
56945694

5695-
static int
5696-
memory_bio_traverse(PyObject *self, visitproc visit, void *arg)
5697-
{
5698-
Py_VISIT(Py_TYPE(self));
5699-
return 0;
5700-
}
5701-
57025695
static void
57035696
memory_bio_dealloc(PyObject *op)
57045697
{
@@ -5869,7 +5862,7 @@ static PyType_Slot PySSLMemoryBIO_slots[] = {
58695862
{Py_tp_getset, memory_bio_getsetlist},
58705863
{Py_tp_new, _ssl_MemoryBIO},
58715864
{Py_tp_dealloc, memory_bio_dealloc},
5872-
{Py_tp_traverse, memory_bio_traverse},
5865+
{Py_tp_traverse, _PyObject_VisitType},
58735866
{0, 0},
58745867
};
58755868

0 commit comments

Comments
 (0)