Skip to content

Commit 7a13953

Browse files
[3.14] pythongh-140000: Traverse name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec (pythonGH-140016) (python#140063)
pythongh-140000: Traverse `name` attribute for `TypeVar`, `TypeVarTuple`, `TypeAliasType`, `ParamSpec` (pythonGH-140016) (cherry picked from commit be60e4b) Co-authored-by: Mikhail Efimov <[email protected]>
1 parent c5edf7d commit 7a13953

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix potential memory leak when a reference cycle exists between an instance
2+
of :class:`typing.TypeAliasType`, :class:`typing.TypeVar`,
3+
:class:`typing.ParamSpec`, or :class:`typing.TypeVarTuple` and its
4+
``__name__`` attribute. Patch by Mikhail Efimov.

Objects/typevarobject.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ typevar_dealloc(PyObject *self)
472472

473473
_PyObject_GC_UNTRACK(self);
474474

475-
Py_DECREF(tv->name);
475+
Py_XDECREF(tv->name);
476476
Py_XDECREF(tv->bound);
477477
Py_XDECREF(tv->evaluate_bound);
478478
Py_XDECREF(tv->constraints);
@@ -491,6 +491,7 @@ typevar_traverse(PyObject *self, visitproc visit, void *arg)
491491
{
492492
Py_VISIT(Py_TYPE(self));
493493
typevarobject *tv = typevarobject_CAST(self);
494+
Py_VISIT(tv->name);
494495
Py_VISIT(tv->bound);
495496
Py_VISIT(tv->evaluate_bound);
496497
Py_VISIT(tv->constraints);
@@ -505,6 +506,7 @@ static int
505506
typevar_clear(PyObject *op)
506507
{
507508
typevarobject *self = typevarobject_CAST(op);
509+
Py_CLEAR(self->name);
508510
Py_CLEAR(self->bound);
509511
Py_CLEAR(self->evaluate_bound);
510512
Py_CLEAR(self->constraints);
@@ -1171,7 +1173,7 @@ paramspec_dealloc(PyObject *self)
11711173

11721174
_PyObject_GC_UNTRACK(self);
11731175

1174-
Py_DECREF(ps->name);
1176+
Py_XDECREF(ps->name);
11751177
Py_XDECREF(ps->bound);
11761178
Py_XDECREF(ps->default_value);
11771179
Py_XDECREF(ps->evaluate_default);
@@ -1187,6 +1189,7 @@ paramspec_traverse(PyObject *self, visitproc visit, void *arg)
11871189
{
11881190
Py_VISIT(Py_TYPE(self));
11891191
paramspecobject *ps = paramspecobject_CAST(self);
1192+
Py_VISIT(ps->name);
11901193
Py_VISIT(ps->bound);
11911194
Py_VISIT(ps->default_value);
11921195
Py_VISIT(ps->evaluate_default);
@@ -1198,6 +1201,7 @@ static int
11981201
paramspec_clear(PyObject *op)
11991202
{
12001203
paramspecobject *self = paramspecobject_CAST(op);
1204+
Py_CLEAR(self->name);
12011205
Py_CLEAR(self->bound);
12021206
Py_CLEAR(self->default_value);
12031207
Py_CLEAR(self->evaluate_default);
@@ -1519,7 +1523,7 @@ typevartuple_dealloc(PyObject *self)
15191523
_PyObject_GC_UNTRACK(self);
15201524
typevartupleobject *tvt = typevartupleobject_CAST(self);
15211525

1522-
Py_DECREF(tvt->name);
1526+
Py_XDECREF(tvt->name);
15231527
Py_XDECREF(tvt->default_value);
15241528
Py_XDECREF(tvt->evaluate_default);
15251529
PyObject_ClearManagedDict(self);
@@ -1683,6 +1687,7 @@ typevartuple_traverse(PyObject *self, visitproc visit, void *arg)
16831687
{
16841688
Py_VISIT(Py_TYPE(self));
16851689
typevartupleobject *tvt = typevartupleobject_CAST(self);
1690+
Py_VISIT(tvt->name);
16861691
Py_VISIT(tvt->default_value);
16871692
Py_VISIT(tvt->evaluate_default);
16881693
PyObject_VisitManagedDict(self, visit, arg);
@@ -1693,6 +1698,7 @@ static int
16931698
typevartuple_clear(PyObject *self)
16941699
{
16951700
typevartupleobject *tvt = typevartupleobject_CAST(self);
1701+
Py_CLEAR(tvt->name);
16961702
Py_CLEAR(tvt->default_value);
16971703
Py_CLEAR(tvt->evaluate_default);
16981704
PyObject_ClearManagedDict(self);
@@ -1851,7 +1857,7 @@ typealias_dealloc(PyObject *self)
18511857
PyTypeObject *tp = Py_TYPE(self);
18521858
_PyObject_GC_UNTRACK(self);
18531859
typealiasobject *ta = typealiasobject_CAST(self);
1854-
Py_DECREF(ta->name);
1860+
Py_XDECREF(ta->name);
18551861
Py_XDECREF(ta->type_params);
18561862
Py_XDECREF(ta->compute_value);
18571863
Py_XDECREF(ta->value);
@@ -2032,6 +2038,7 @@ static int
20322038
typealias_traverse(PyObject *op, visitproc visit, void *arg)
20332039
{
20342040
typealiasobject *self = typealiasobject_CAST(op);
2041+
Py_VISIT(self->name);
20352042
Py_VISIT(self->type_params);
20362043
Py_VISIT(self->compute_value);
20372044
Py_VISIT(self->value);
@@ -2043,6 +2050,7 @@ static int
20432050
typealias_clear(PyObject *op)
20442051
{
20452052
typealiasobject *self = typealiasobject_CAST(op);
2053+
Py_CLEAR(self->name);
20462054
Py_CLEAR(self->type_params);
20472055
Py_CLEAR(self->compute_value);
20482056
Py_CLEAR(self->value);

0 commit comments

Comments
 (0)