[Python-checkins] cpython (2.7): Refactor the new test for issue19081 to exec import statements into a

gregory.p.smith python-checkins at python.org
Tue Jan 28 07:07:19 CET 2014


http://hg.python.org/cpython/rev/d140e3f41796
changeset: 88789:d140e3f41796
branch: 2.7
parent: 88779:6b314f5c9404
user: Gregory P. Smith <greg at krypto.org>
date: Mon Jan 27 22:06:51 2014 -0800
summary:
 Refactor the new test for issue19081 to exec import statements into a
test_ns dict instead of the actual globals() and locals(). Suggested
after review by Thomas Wouters.
files:
 Lib/test/test_zipimport.py | 24 ++++++++++++++----------
 1 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -469,7 +469,7 @@
 with self.assertRaises(ImportError):
 from ziptest_a import test_value
 with self.assertRaises(ImportError):
- exec("from {} import {}".format(TESTPACK, TESTMOD))
+ exec("from {} import {}".format(TESTPACK, TESTMOD), {})
 
 # Alters all of the offsets within the file
 self.restoreZipFileWithDifferentHeaderOffsets()
@@ -479,10 +479,12 @@
 # new file's end of file central index and be able to import again.
 
 # Importing a submodule triggers a different import code path.
- exec("import " + self.testpack_testmod)
- self.assertEqual(getattr(locals()[TESTPACK], TESTMOD).test_value, 38)
- exec("from {} import {}".format(TESTPACK, TESTMOD))
- self.assertEqual(locals()[TESTMOD].test_value, 38)
+ test_ns = {}
+ exec("import " + self.testpack_testmod, test_ns)
+ self.assertEqual(getattr(test_ns[TESTPACK], TESTMOD).test_value, 38)
+ test_ns = {}
+ exec("from {} import {}".format(TESTPACK, TESTMOD), test_ns)
+ self.assertEqual(test_ns[TESTMOD].test_value, 38)
 
 ziptest_a = __import__("ziptest_a", globals(), locals(), ["test_value"])
 self.assertEqual(ziptest_a.test_value, 23)
@@ -498,8 +500,9 @@
 testmod = __import__(TESTMOD, globals(), locals(), ["test_value"])
 self.assertEqual(testmod.test_value, 38)
 del sys.modules[TESTMOD]
- exec("from {} import test_value".format(TESTMOD))
- self.assertEqual(test_value, 38)
+ test_ns = {}
+ exec("from {} import test_value".format(TESTMOD), test_ns)
+ self.assertEqual(test_ns["test_value"], 38)
 del sys.modules[TESTMOD]
 
 # Confirm that imports from the top level of the zip file
@@ -516,7 +519,7 @@
 with self.assertRaises(ImportError):
 testmod = __import__(TESTMOD, globals(), locals(), ["test_value"])
 with self.assertRaises(ImportError):
- exec("from {} import test_value".format(TESTMOD))
+ exec("from {} import test_value".format(TESTMOD), {})
 with self.assertRaises(ImportError):
 import ziptest_a
 
@@ -525,8 +528,9 @@
 testmod = __import__(TESTMOD, globals(), locals(), ["test_value"])
 self.assertEqual(testmod.test_value, 38)
 del sys.modules[TESTMOD]
- exec("from {} import test_value".format(TESTMOD))
- self.assertEqual(test_value, 38)
+ test_ns = {}
+ exec("from {} import test_value".format(TESTMOD), test_ns)
+ self.assertEqual(test_ns['test_value'], 38)
 
 ziptest_a = __import__("ziptest_a", globals(), locals(), ["test_value"])
 self.assertEqual(ziptest_a.test_value, 23)
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /