]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-123780: Make test_pkgutil clean up `spam` module (GH-123036) (GH-123782)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 9 Sep 2024 11:18:42 +0000 (13:18 +0200)
committerGitHub <noreply@github.com>
Mon, 9 Sep 2024 11:18:42 +0000 (13:18 +0200)
(cherry picked from commit eca3fe40c251d51964172dd4e6e9c7d0d85d7d4a)

Co-authored-by: Malcolm Smith <smith@chaquo.com>
Lib/test/test_pkgutil.py

index 20fba87e4ec120403a584f6f9d07ed67d7ae9933..ca6927554b053c2b8c237a3a20353f95c349cb72 100644 (file)
@@ -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)