summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2010-03-17 03:14:31 (GMT)
committerCollin Winter <collinw@gmail.com>2010-03-17 03:14:31 (GMT)
commit6498cff19f9d403b82a6be626f085ae89f6bbab9 (patch)
tree4a5bcc741126c2efecb8bd29018c582ed563f83f
parent88e333d141a9c7839d295564d8fae33d39206165 (diff)
downloadcpython-6498cff19f9d403b82a6be626f085ae89f6bbab9.zip
cpython-6498cff19f9d403b82a6be626f085ae89f6bbab9.tar.gz
cpython-6498cff19f9d403b82a6be626f085ae89f6bbab9.tar.bz2
Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
-rw-r--r--Lib/test/test_import.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index cf15239..9922407 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -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.