]> granicus.if.org Git - python/commitdiff
bpo-31676: Fix test_imp.test_load_source() side effect (GH-3871) (GH-3988)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 18 Oct 2017 01:47:10 +0000 (18:47 -0700)
committerMariatta <Mariatta@users.noreply.github.com>
Wed, 18 Oct 2017 01:47:10 +0000 (18:47 -0700)
test_load_source() now replaces the current __name__ module with a
temporary module to prevent side effects.
(cherry picked from commit a505ecdc5013cd8f930aacc1ec4fb2afa62d3853)

Lib/test/test_imp.py

index d513eedc7bb4aa5288d7fc7a625d6c1949b7b176..1c7605c77bb14fda0b6db30e007c0124d98f87ef 100644 (file)
@@ -315,8 +315,13 @@ class ImportTests(unittest.TestCase):
         loader.get_data(imp.__file__)  # Will need to create a newly opened file
 
     def test_load_source(self):
-        with self.assertRaisesRegex(ValueError, 'embedded null'):
-            imp.load_source(__name__, __file__ + "\0")
+        # Create a temporary module since load_source(name) modifies
+        # sys.modules[name] attributes like __loader___
+        modname = f"tmp{__name__}"
+        mod = type(sys.modules[__name__])(modname)
+        with support.swap_item(sys.modules, modname, mod):
+            with self.assertRaisesRegex(ValueError, 'embedded null'):
+                imp.load_source(modname, __file__ + "\0")
 
     @support.cpython_only
     def test_issue31315(self):