From: Gregory P. Smith Date: Tue, 28 Jan 2014 06:06:51 +0000 (-0800) Subject: Refactor the new test for issue19081 to exec import statements into a X-Git-Tag: v2.7.8~76 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a4795525511154621cca5f083b93c953327c7b6;p=python 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. --- diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index b435b32ca1..a8180cc0be 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -469,7 +469,7 @@ class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase): 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 @@ class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase): # 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 @@ class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase): 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 @@ class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase): 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 @@ class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase): 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)