Skip to content

Commit 58f9c88

Browse files
[3.12] gh-106403: Restore weakref support for TypeVar and friends (GH-106418) (#106635)
gh-106403: Restore weakref support for TypeVar and friends (GH-106418) (cherry picked from commit 945d3cb) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 6015059 commit 58f9c88

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

Lib/test/test_type_params.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import types
44
import unittest
55
import pickle
6+
import weakref
67
from test.support import requires_working_socket, check_syntax_error, run_code
78

89
from typing import Generic, Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args
@@ -921,3 +922,33 @@ def test_pickling_classes(self):
921922
# These instances are not equal,
922923
# but class check is good enough:
923924
self.assertIsInstance(pickle.loads(pickled), real_class)
925+
926+
927+
class TypeParamsWeakRefTest(unittest.TestCase):
928+
def test_weakrefs(self):
929+
T = TypeVar('T')
930+
P = ParamSpec('P')
931+
class OldStyle(Generic[T]):
932+
pass
933+
934+
class NewStyle[T]:
935+
pass
936+
937+
cases = [
938+
T,
939+
TypeVar('T', bound=int),
940+
P,
941+
P.args,
942+
P.kwargs,
943+
TypeVarTuple('Ts'),
944+
OldStyle,
945+
OldStyle[int],
946+
OldStyle(),
947+
NewStyle,
948+
NewStyle[int],
949+
NewStyle(),
950+
Generic[T],
951+
]
952+
for case in cases:
953+
with self.subTest(case=case):
954+
weakref.ref(case)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Instances of :class:`typing.TypeVar`, :class:`typing.ParamSpec`,
2+
:class:`typing.ParamSpecArgs`, :class:`typing.ParamSpecKwargs`, and
3+
:class:`typing.TypeVarTuple` once again support weak references, fixing a
4+
regression introduced in Python 3.12.0 beta 1. Patch by Jelle Zijlstra.

Objects/typevarobject.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ PyType_Spec typevar_spec = {
500500
.name = "typing.TypeVar",
501501
.basicsize = sizeof(typevarobject),
502502
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
503-
| Py_TPFLAGS_MANAGED_DICT,
503+
| Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_MANAGED_WEAKREF,
504504
.slots = typevar_slots,
505505
};
506506

@@ -647,7 +647,8 @@ static PyType_Slot paramspecargs_slots[] = {
647647
PyType_Spec paramspecargs_spec = {
648648
.name = "typing.ParamSpecArgs",
649649
.basicsize = sizeof(paramspecattrobject),
650-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
650+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
651+
| Py_TPFLAGS_MANAGED_WEAKREF,
651652
.slots = paramspecargs_slots,
652653
};
653654

@@ -726,7 +727,8 @@ static PyType_Slot paramspeckwargs_slots[] = {
726727
PyType_Spec paramspeckwargs_spec = {
727728
.name = "typing.ParamSpecKwargs",
728729
.basicsize = sizeof(paramspecattrobject),
729-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
730+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
731+
| Py_TPFLAGS_MANAGED_WEAKREF,
730732
.slots = paramspeckwargs_slots,
731733
};
732734

@@ -1007,7 +1009,7 @@ PyType_Spec paramspec_spec = {
10071009
.name = "typing.ParamSpec",
10081010
.basicsize = sizeof(paramspecobject),
10091011
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
1010-
| Py_TPFLAGS_MANAGED_DICT,
1012+
| Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_MANAGED_WEAKREF,
10111013
.slots = paramspec_slots,
10121014
};
10131015

@@ -1228,7 +1230,7 @@ PyType_Spec typevartuple_spec = {
12281230
.name = "typing.TypeVarTuple",
12291231
.basicsize = sizeof(typevartupleobject),
12301232
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT
1231-
| Py_TPFLAGS_HAVE_GC,
1233+
| Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_WEAKREF,
12321234
.slots = typevartuple_slots,
12331235
};
12341236

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy