]> granicus.if.org Git - python/commitdiff
bpo-31676: Fix test_imp.test_load_source() side effect (#3871)
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 13 Oct 2017 20:47:49 +0000 (13:47 -0700)
committerGitHub <noreply@github.com>
Fri, 13 Oct 2017 20:47:49 +0000 (13:47 -0700)
test_load_source() now replaces the current __name__ module with a
temporary module to prevent side effects.

Lib/test/test_imp.py

index 51bec50cc2f264d56cc170391b2be1532128fe1c..1ef0accf0f3c911fa3a8c5ed5a7a8f44989969cd 100644 (file)
@@ -310,8 +310,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):