diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-10-18 01:47:10 (GMT) |
---|---|---|
committer | Mariatta <Mariatta@users.noreply.github.com> | 2017-10-18 01:47:10 (GMT) |
commit | 178148025494c4058571831fb11fc8eeff8b7365 (patch) | |
tree | fc1452fe6c01a1c5dd0cd6a906585bb9b2fae000 | |
parent | 98e0f26f2e4cbf5c2ca27b39f43c1cb0114c6e3c (diff) | |
download | cpython-178148025494c4058571831fb11fc8eeff8b7365.zip cpython-178148025494c4058571831fb11fc8eeff8b7365.tar.gz cpython-178148025494c4058571831fb11fc8eeff8b7365.tar.bz2 |
bpo-31676: Fix test_imp.test_load_source() side effect (GH-3871) (GH-3988)
test_load_source() now replaces the current __name__ module with a
temporary module to prevent side effects.
(cherry picked from commit a505ecdc5013cd8f930aacc1ec4fb2afa62d3853)
-rw-r--r-- | Lib/test/test_imp.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index d513eed..1c7605c 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -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): |