From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:18:42 +0000 (+0200) Subject: [3.12] gh-123780: Make test_pkgutil clean up `spam` module (GH-123036) (GH-123782) X-Git-Tag: v3.12.7~111 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=349cc27ca9c9ef38cf8cb4d6b58c4e354b6157e8;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-123780: Make test_pkgutil clean up `spam` module (GH-123036) (GH-123782) (cherry picked from commit eca3fe40c251d51964172dd4e6e9c7d0d85d7d4a) Co-authored-by: Malcolm Smith --- diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 20fba87e4ec1..ca6927554b05 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -624,8 +624,11 @@ class ImportlibMigrationTests(unittest.TestCase): mod = type(sys)(name) del mod.__spec__ with CleanImport(name): - sys.modules[name] = mod - loader = pkgutil.get_loader(name) + try: + sys.modules[name] = mod + loader = pkgutil.get_loader(name) + finally: + sys.modules.pop(name, None) self.assertIsNone(loader) @ignore_warnings(category=DeprecationWarning) @@ -634,8 +637,11 @@ class ImportlibMigrationTests(unittest.TestCase): mod = type(sys)(name) mod.__spec__ = None with CleanImport(name): - sys.modules[name] = mod - loader = pkgutil.get_loader(name) + try: + sys.modules[name] = mod + loader = pkgutil.get_loader(name) + finally: + sys.modules.pop(name, None) self.assertIsNone(loader) @ignore_warnings(category=DeprecationWarning)