diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-10-13 20:47:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-13 20:47:49 (GMT) |
commit | a505ecdc5013cd8f930aacc1ec4fb2afa62d3853 (patch) | |
tree | a8b81bec7e3ece4d474b252d1479acf94ea53f12 /Lib | |
parent | a99121526a14e7710843aa5dd6ac82a779542dfb (diff) | |
download | cpython-a505ecdc5013cd8f930aacc1ec4fb2afa62d3853.zip cpython-a505ecdc5013cd8f930aacc1ec4fb2afa62d3853.tar.gz cpython-a505ecdc5013cd8f930aacc1ec4fb2afa62d3853.tar.bz2 |
bpo-31676: Fix test_imp.test_load_source() side effect (#3871)
test_load_source() now replaces the current __name__ module with a
temporary module to prevent side effects.
Diffstat (limited to 'Lib')
-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 51bec50..1ef0acc 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -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): |