[Python-checkins] bpo-31676: Fix test_imp.test_load_source() side effect (#3871)
Victor Stinner
webhook-mailer at python.org
Fri Oct 13 16:47:51 EDT 2017
https://github.com/python/cpython/commit/a505ecdc5013cd8f930aacc1ec4fb2afa62d3853
commit: a505ecdc5013cd8f930aacc1ec4fb2afa62d3853
branch: master
author: Victor Stinner <victor.stinner at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-10-13T13:47:49-07:00
summary:
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.
files:
M Lib/test/test_imp.py
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index 51bec50cc2f..1ef0accf0f3 100644
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -310,8 +310,13 @@ def test_multiple_calls_to_get_data(self):
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):
More information about the Python-checkins
mailing list