From e52cce67e0efcc0f45004e594c8691f1438b0afc Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 9 May 2023 11:38:10 +0100 Subject: [PATCH 1/2] gh-103193: Fix refleaks in `test_inspect` and `test_typing` --- Lib/test/test_inspect.py | 1 + Lib/test/test_typing.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index d2b2f3171e785d..b896dafc941e3a 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -69,6 +69,7 @@ def revise(filename, *args): def tearDownModule(): if support.has_socket_support: asyncio.set_event_loop_policy(None) + inspect._shadowed_dict_from_mro_tuple.cache_clear() def signatures_with_lexicographic_keyword_only_parameters(): diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f162e587810ac0..39a2cc5a99bad9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -56,6 +56,10 @@ CANNOT_SUBCLASS_INSTANCE = 'Cannot subclass an instance of %s' +def tearDownModule(): + inspect._shadowed_dict_from_mro_tuple.cache_clear() + + class BaseTestCase(TestCase): def assertIsSubclass(self, cls, class_or_tuple, msg=None): From 60b18aacec05b8f8f90e5f733b389517b79a0b55 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 9 May 2023 11:48:18 +0100 Subject: [PATCH 2/2] better fix --- Lib/test/libregrtest/utils.py | 7 +++++++ Lib/test/test_inspect.py | 1 - Lib/test/test_typing.py | 4 ---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index fb13fa0e243ba7..fd46819fd903fe 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -210,6 +210,13 @@ def clear_caches(): else: fractions._hash_algorithm.cache_clear() + try: + inspect = sys.modules['inspect'] + except KeyError: + pass + else: + inspect._shadowed_dict_from_mro_tuple.cache_clear() + def get_build_info(): # Get most important configure and build options as a list of strings. diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index b896dafc941e3a..d2b2f3171e785d 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -69,7 +69,6 @@ def revise(filename, *args): def tearDownModule(): if support.has_socket_support: asyncio.set_event_loop_policy(None) - inspect._shadowed_dict_from_mro_tuple.cache_clear() def signatures_with_lexicographic_keyword_only_parameters(): diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 39a2cc5a99bad9..f162e587810ac0 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -56,10 +56,6 @@ CANNOT_SUBCLASS_INSTANCE = 'Cannot subclass an instance of %s' -def tearDownModule(): - inspect._shadowed_dict_from_mro_tuple.cache_clear() - - class BaseTestCase(TestCase): def assertIsSubclass(self, cls, class_or_tuple, msg=None):