]> granicus.if.org Git - python/commitdiff
Add tests for overriding and shadowing __import__; these are a useful tripwire for...
authorCollin Winter <collinw@gmail.com>
Wed, 17 Mar 2010 03:14:31 +0000 (03:14 +0000)
committerCollin Winter <collinw@gmail.com>
Wed, 17 Mar 2010 03:14:31 +0000 (03:14 +0000)
Lib/test/test_import.py

index cf152394f5770c284fa02a2f3c2c6f3e9627e0a5..992240746f87ae95a84c1d07ba77a371a396ebae 100644 (file)
@@ -1,3 +1,4 @@
+import builtins
 import imp
 import marshal
 import os
@@ -9,7 +10,7 @@ import sys
 import unittest
 import warnings
 from test.support import (unlink, TESTFN, unload, run_unittest,
-                          TestFailed, EnvironmentVarGuard)
+                          TestFailed, EnvironmentVarGuard, swap_attr, swap_item)
 
 
 def remove_files(name):
@@ -446,8 +447,29 @@ class RelativeImportTests(unittest.TestCase):
         self.assertRaises(ValueError, check_relative)
 
 
+class OverridingImportBuiltinTests(unittest.TestCase):
+    def test_override_builtin(self):
+        # Test that overriding builtins.__import__ can bypass sys.modules.
+        import os
+
+        def foo():
+            import os
+            return os
+        self.assertEqual(foo(), os)  # Quick sanity check.
+
+        with swap_attr(builtins, "__import__", lambda *x: 5):
+            self.assertEqual(foo(), 5)
+
+        # Test what happens when we shadow __import__ in globals(); this
+        # currently does not impact the import process, but if this changes,
+        # other code will need to change, so keep this test as a tripwire.
+        with swap_item(globals(), "__import__", lambda *x: 5):
+            self.assertEqual(foo(), os)
+
+
 def test_main(verbose=None):
-    run_unittest(ImportTests, PycRewritingTests, PathsTests, RelativeImportTests)
+    run_unittest(ImportTests, PycRewritingTests, PathsTests, RelativeImportTests,
+                 OverridingImportBuiltinTests)
 
 if __name__ == '__main__':
     # Test needs to be a package, so we can do relative imports.