Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.

This commit is contained in:
Collin Winter 2010-03-17 03:14:31 +00:00
parent 88e333d141
commit 6498cff19f
1 changed files with 24 additions and 2 deletions

View 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.