Skip to content

Commit cbcee9a

Browse files
[3.13] pythongh-140000: Traverse name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec (pythonGH-140016) (python#140073)
Correct traversing name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec
1 parent 0e78932 commit cbcee9a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
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: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ typevar_dealloc(PyObject *self)
259259

260260
_PyObject_GC_UNTRACK(self);
261261

262-
Py_DECREF(tv->name);
262+
Py_XDECREF(tv->name);
263263
Py_XDECREF(tv->bound);
264264
Py_XDECREF(tv->evaluate_bound);
265265
Py_XDECREF(tv->constraints);
@@ -278,6 +278,7 @@ typevar_traverse(PyObject *self, visitproc visit, void *arg)
278278
{
279279
Py_VISIT(Py_TYPE(self));
280280
typevarobject *tv = (typevarobject *)self;
281+
Py_VISIT(tv->name);
281282
Py_VISIT(tv->bound);
282283
Py_VISIT(tv->evaluate_bound);
283284
Py_VISIT(tv->constraints);
@@ -291,6 +292,7 @@ typevar_traverse(PyObject *self, visitproc visit, void *arg)
291292
static int
292293
typevar_clear(typevarobject *self)
293294
{
295+
Py_CLEAR(self->name);
294296
Py_CLEAR(self->bound);
295297
Py_CLEAR(self->evaluate_bound);
296298
Py_CLEAR(self->constraints);
@@ -912,7 +914,7 @@ paramspec_dealloc(PyObject *self)
912914

913915
_PyObject_GC_UNTRACK(self);
914916

915-
Py_DECREF(ps->name);
917+
Py_XDECREF(ps->name);
916918
Py_XDECREF(ps->bound);
917919
Py_XDECREF(ps->default_value);
918920
Py_XDECREF(ps->evaluate_default);
@@ -928,6 +930,7 @@ paramspec_traverse(PyObject *self, visitproc visit, void *arg)
928930
{
929931
Py_VISIT(Py_TYPE(self));
930932
paramspecobject *ps = (paramspecobject *)self;
933+
Py_VISIT(ps->name);
931934
Py_VISIT(ps->bound);
932935
Py_VISIT(ps->default_value);
933936
Py_VISIT(ps->evaluate_default);
@@ -938,6 +941,7 @@ paramspec_traverse(PyObject *self, visitproc visit, void *arg)
938941
static int
939942
paramspec_clear(paramspecobject *self)
940943
{
944+
Py_CLEAR(self->name);
941945
Py_CLEAR(self->bound);
942946
Py_CLEAR(self->default_value);
943947
Py_CLEAR(self->evaluate_default);
@@ -1244,7 +1248,7 @@ typevartuple_dealloc(PyObject *self)
12441248
_PyObject_GC_UNTRACK(self);
12451249
typevartupleobject *tvt = (typevartupleobject *)self;
12461250

1247-
Py_DECREF(tvt->name);
1251+
Py_XDECREF(tvt->name);
12481252
Py_XDECREF(tvt->default_value);
12491253
Py_XDECREF(tvt->evaluate_default);
12501254
PyObject_ClearManagedDict(self);
@@ -1408,17 +1412,21 @@ static int
14081412
typevartuple_traverse(PyObject *self, visitproc visit, void *arg)
14091413
{
14101414
Py_VISIT(Py_TYPE(self));
1411-
Py_VISIT(((typevartupleobject *)self)->default_value);
1412-
Py_VISIT(((typevartupleobject *)self)->evaluate_default);
1415+
typevartupleobject *tvt = (typevartupleobject *)self;
1416+
Py_VISIT(tvt->name);
1417+
Py_VISIT(tvt->default_value);
1418+
Py_VISIT(tvt->evaluate_default);
14131419
PyObject_VisitManagedDict(self, visit, arg);
14141420
return 0;
14151421
}
14161422

14171423
static int
14181424
typevartuple_clear(PyObject *self)
14191425
{
1420-
Py_CLEAR(((typevartupleobject *)self)->default_value);
1421-
Py_CLEAR(((typevartupleobject *)self)->evaluate_default);
1426+
typevartupleobject *tvt = (typevartupleobject *)self;
1427+
Py_CLEAR(tvt->name);
1428+
Py_CLEAR(tvt->default_value);
1429+
Py_CLEAR(tvt->evaluate_default);
14221430
PyObject_ClearManagedDict(self);
14231431
return 0;
14241432
}
@@ -1542,7 +1550,7 @@ typealias_dealloc(PyObject *self)
15421550
PyTypeObject *tp = Py_TYPE(self);
15431551
_PyObject_GC_UNTRACK(self);
15441552
typealiasobject *ta = (typealiasobject *)self;
1545-
Py_DECREF(ta->name);
1553+
Py_XDECREF(ta->name);
15461554
Py_XDECREF(ta->type_params);
15471555
Py_XDECREF(ta->compute_value);
15481556
Py_XDECREF(ta->value);
@@ -1660,6 +1668,7 @@ typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
16601668
static int
16611669
typealias_traverse(typealiasobject *self, visitproc visit, void *arg)
16621670
{
1671+
Py_VISIT(self->name);
16631672
Py_VISIT(self->type_params);
16641673
Py_VISIT(self->compute_value);
16651674
Py_VISIT(self->value);
@@ -1670,6 +1679,7 @@ typealias_traverse(typealiasobject *self, visitproc visit, void *arg)
16701679
static int
16711680
typealias_clear(typealiasobject *self)
16721681
{
1682+
Py_CLEAR(self->name);
16731683
Py_CLEAR(self->type_params);
16741684
Py_CLEAR(self->compute_value);
16751685
Py_CLEAR(self->value);

0 commit comments

Comments
 (0)