Move import semantic util code to importlib.test.import_.util.

This commit is contained in:
Brett Cannon 2009-02-01 04:28:04 +00:00
parent bcb26c53c0
commit d720b36248
10 changed files with 99 additions and 92 deletions

View File

@ -3,10 +3,6 @@ to do
* Reorganize support code.
+ Separate out support code for extensions out of test_support_hook.
+ Move util.import_ and utill.mock_modules to import_, importlib_only,
mock_path_hook?
+ Add a file loader mock that returns monotonically increasing mtime.
- Use in source/test_reload.
- Use in source/test_load_module_mixed.

View File

@ -6,6 +6,7 @@ of using the typical __path__/__name__ test).
"""
import unittest
from .. import util
from . import util as import_util
class Using__package__(unittest.TestCase):
@ -36,8 +37,8 @@ class Using__package__(unittest.TestCase):
# [__package__]
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
with util.import_state(meta_path=[importer]):
util.import_('pkg.fake')
module = util.import_('', globals={'__package__': 'pkg.fake'},
import_util.import_('pkg.fake')
module = import_util.import_('', globals={'__package__': 'pkg.fake'},
fromlist=['attr'], level=2)
self.assertEquals(module.__name__, 'pkg')
@ -45,8 +46,8 @@ class Using__package__(unittest.TestCase):
# [__name__]
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
with util.import_state(meta_path=[importer]):
util.import_('pkg.fake')
module = util.import_('',
import_util.import_('pkg.fake')
module = import_util.import_('',
globals={'__name__': 'pkg.fake',
'__path__': []},
fromlist=['attr'], level=2)
@ -54,12 +55,12 @@ class Using__package__(unittest.TestCase):
def test_bad__package__(self):
globals = {'__package__': '<not real>'}
self.assertRaises(SystemError, util.import_,'', globals, {},
self.assertRaises(SystemError, import_util.import_,'', globals, {},
['relimport'], 1)
def test_bunk__package__(self):
globals = {'__package__': 42}
self.assertRaises(ValueError, util.import_, '', globals, {},
self.assertRaises(ValueError, import_util.import_, '', globals, {},
['relimport'], 1)
@ -80,7 +81,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('top_level') as mock:
with util.import_state(meta_path=[mock]):
del mock['top_level'].__package__
module = util.import_('top_level')
module = import_util.import_('top_level')
self.assert_(module.__package__ is None)
# [package]
@ -88,7 +89,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('pkg.__init__') as mock:
with util.import_state(meta_path=[mock]):
del mock['pkg'].__package__
module = util.import_('pkg')
module = import_util.import_('pkg')
self.assertEqual(module.__package__, 'pkg')
# [submodule]
@ -96,7 +97,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('pkg.__init__', 'pkg.mod') as mock:
with util.import_state(meta_path=[mock]):
del mock['pkg.mod'].__package__
pkg = util.import_('pkg.mod')
pkg = import_util.import_('pkg.mod')
module = getattr(pkg, 'mod')
self.assertEqual(module.__package__, 'pkg')

View File

@ -1,5 +1,6 @@
"""Test that sys.modules is used properly by import."""
from .. import util
from . import util as import_util
import sys
from types import MethodType
import unittest
@ -23,7 +24,7 @@ class UseCache(unittest.TestCase):
# [use cache]
module_to_use = "some module found!"
sys.modules['some_module'] = module_to_use
module = util.import_('some_module')
module = import_util.import_('some_module')
self.assertEqual(id(module_to_use), id(module))
def create_mock(self, *names, return_=None):
@ -37,31 +38,31 @@ class UseCache(unittest.TestCase):
# __import__ inconsistent between loaders and built-in import when it comes
# to when to use the module in sys.modules and when not to.
@util.importlib_only
@import_util.importlib_only
def test_using_cache_after_loader(self):
# [from cache on return]
with self.create_mock('module') as mock:
with util.import_state(meta_path=[mock]):
module = util.import_('module')
module = import_util.import_('module')
self.assertEquals(id(module), id(sys.modules['module']))
# See test_using_cache_after_loader() for reasoning.
@util.importlib_only
@import_util.importlib_only
def test_using_cache_for_assigning_to_attribute(self):
# [from cache to attribute]
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg.module')
module = import_util.import_('pkg.module')
self.assert_(hasattr(module, 'module'))
self.assert_(id(module.module), id(sys.modules['pkg.module']))
# See test_using_cache_after_loader() for reasoning.
@util.importlib_only
@import_util.importlib_only
def test_using_cache_for_fromlist(self):
# [from cache for fromlist]
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg', fromlist=['module'])
module = import_util.import_('pkg', fromlist=['module'])
self.assert_(hasattr(module, 'module'))
self.assertEquals(id(module.module), id(sys.modules['pkg.module']))

View File

@ -1,5 +1,6 @@
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util
from . import util as import_util
import unittest
class ReturnValue(unittest.TestCase):
@ -17,14 +18,14 @@ class ReturnValue(unittest.TestCase):
# [import return]
with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg.module')
module = import_util.import_('pkg.module')
self.assertEquals(module.__name__, 'pkg')
def test_return_from_from_import(self):
# [from return]
with util.mock_modules('pkg.__init__', 'pkg.module')as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg.module', fromlist=['attr'])
module = import_util.import_('pkg.module', fromlist=['attr'])
self.assertEquals(module.__name__, 'pkg.module')
@ -49,14 +50,14 @@ class HandlingFromlist(unittest.TestCase):
# [object case]
with util.mock_modules('module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('module', fromlist=['attr'])
module = import_util.import_('module', fromlist=['attr'])
self.assertEquals(module.__name__, 'module')
def test_unexistent_object(self):
# [bad object]
with util.mock_modules('module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('module', fromlist=['non_existent'])
module = import_util.import_('module', fromlist=['non_existent'])
self.assertEquals(module.__name__, 'module')
self.assert_(not hasattr(module, 'non_existent'))
@ -64,7 +65,7 @@ class HandlingFromlist(unittest.TestCase):
# [module]
with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg', fromlist=['module'])
module = import_util.import_('pkg', fromlist=['module'])
self.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module'))
self.assertEquals(module.module.__name__, 'pkg.module')
@ -73,14 +74,14 @@ class HandlingFromlist(unittest.TestCase):
# [no module]
with util.mock_modules('pkg.__init__') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg', fromlist='non_existent')
module = import_util.import_('pkg', fromlist='non_existent')
self.assertEquals(module.__name__, 'pkg')
self.assert_(not hasattr(module, 'non_existent'))
def test_empty_string(self):
with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
with util.import_state(meta_path=[importer]):
module = util.import_('pkg.mod', fromlist=[''])
module = import_util.import_('pkg.mod', fromlist=[''])
self.assertEquals(module.__name__, 'pkg.mod')
def test_using_star(self):
@ -88,7 +89,7 @@ class HandlingFromlist(unittest.TestCase):
with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
with util.import_state(meta_path=[mock]):
mock['pkg'].__all__ = ['module']
module = util.import_('pkg', fromlist=['*'])
module = import_util.import_('pkg', fromlist=['*'])
self.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module'))
self.assertEqual(module.module.__name__, 'pkg.module')
@ -99,7 +100,7 @@ class HandlingFromlist(unittest.TestCase):
with context as mock:
with util.import_state(meta_path=[mock]):
mock['pkg'].__all__ = ['module1']
module = util.import_('pkg', fromlist=['module2', '*'])
module = import_util.import_('pkg', fromlist=['module2', '*'])
self.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module1'))
self.assert_(hasattr(module, 'module2'))

View File

@ -1,4 +1,5 @@
from .. import util
from . import util as import_util
from contextlib import nested
from types import MethodType
import unittest
@ -22,7 +23,7 @@ class CallingOrder(unittest.TestCase):
first.modules[mod] = 42
second.modules[mod] = -13
with util.import_state(meta_path=[first, second]):
self.assertEquals(util.import_(mod), 42)
self.assertEquals(import_util.import_(mod), 42)
def test_continuing(self):
# [continuing]
@ -33,7 +34,7 @@ class CallingOrder(unittest.TestCase):
first.find_module = lambda self, fullname, path=None: None
second.modules[mod_name] = 42
with util.import_state(meta_path=[first, second]):
self.assertEquals(util.import_(mod_name), 42)
self.assertEquals(import_util.import_(mod_name), 42)
class CallSignature(unittest.TestCase):
@ -58,7 +59,7 @@ class CallSignature(unittest.TestCase):
log, wrapped_call = self.log(importer.find_module)
importer.find_module = MethodType(wrapped_call, importer)
with util.import_state(meta_path=[importer]):
util.import_(mod_name)
import_util.import_(mod_name)
assert len(log) == 1
args = log[0][0]
kwargs = log[0][1]
@ -79,7 +80,7 @@ class CallSignature(unittest.TestCase):
log, wrapped_call = self.log(importer.find_module)
importer.find_module = MethodType(wrapped_call, importer)
with util.import_state(meta_path=[importer]):
util.import_(mod_name)
import_util.import_(mod_name)
assert len(log) == 2
args = log[1][0]
kwargs = log[1][1]

View File

@ -1,4 +1,5 @@
from .. import util
from . import util as import_util
import sys
import unittest
import importlib
@ -11,13 +12,14 @@ class ParentModuleTests(unittest.TestCase):
def test_import_parent(self):
with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
with util.import_state(meta_path=[mock]):
module = util.import_('pkg.module')
module = import_util.import_('pkg.module')
self.assert_('pkg' in sys.modules)
def test_bad_parent(self):
with util.mock_modules('pkg.module') as mock:
with util.import_state(meta_path=[mock]):
self.assertRaises(ImportError, util.import_, 'pkg.module')
self.assertRaises(ImportError,
import_util.import_, 'pkg.module')
def test_main():

View File

@ -1,4 +1,5 @@
from .. import util
from . import util as import_util
from contextlib import nested
from imp import new_module
import sys
@ -41,11 +42,11 @@ class BaseTests(unittest.TestCase):
with nested(misser, hitter):
cache = dict(zip(search_path, (misser, hitter)))
with util.import_state(path=path, path_importer_cache=cache):
util.import_(to_import)
import_util.import_(to_import)
self.assertEquals(log[0], misser)
self.assertEquals(log[1], hitter)
@util.importlib_only # __import__ uses PyDict_GetItem(), bypassing log.
@import_util.importlib_only # __import__ uses PyDict_GetItem(), bypassing log.
def cache_use_test(self, to_import, entry, path=[]):
# [cache check], [cache use]
log = []
@ -58,7 +59,7 @@ class BaseTests(unittest.TestCase):
cache = LoggingDict()
cache[entry] = importer
with util.import_state(path=[entry], path_importer_cache=cache):
module = util.import_(to_import, fromlist=['a'])
module = import_util.import_(to_import, fromlist=['a'])
self.assert_(module is importer[to_import])
self.assertEquals(len(cache), 1)
self.assertEquals([entry], log)
@ -70,10 +71,10 @@ class BaseTests(unittest.TestCase):
log.append(entry)
raise ImportError
with util.mock_modules(to_import) as importer:
hitter = util.mock_path_hook(entry, importer=importer)
hitter = import_util.mock_path_hook(entry, importer=importer)
path_hooks = [logging_hook, logging_hook, hitter]
with util.import_state(path_hooks=path_hooks, path=path):
util.import_(to_import)
import_util.import_(to_import)
self.assertEquals(sys.path_importer_cache[entry], importer)
self.assertEquals(len(log), 2)
@ -88,7 +89,7 @@ class BaseTests(unittest.TestCase):
raise ImportError
try:
util.import_(to_import)
import_util.import_(to_import)
except ImportError:
pass
@ -126,7 +127,7 @@ class __path__Tests(BaseTests):
test('pkg.hit', entry, *args)
@util.importlib_only # XXX Unknown reason why this fails.
@import_util.importlib_only # XXX Unknown reason why this fails.
def test_order(self):
self.run_test(self.order_test, 'second', ('first', 'second'), ['first',
'second'])

View File

@ -1,5 +1,6 @@
"""Test relative imports (PEP 328)."""
from .. import util
from . import util as import_util
import sys
import unittest
@ -75,8 +76,8 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.mod2'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
def callback(global_):
util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['mod2'], level=1)
import_util.import_('pkg') # For __import__().
module = import_util.import_('', global_, fromlist=['mod2'], level=1)
self.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'mod2'))
self.assertEqual(module.mod2.attr, 'pkg.mod2')
@ -87,8 +88,9 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.mod2'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
def callback(global_):
util.import_('pkg') # For __import__().
module = util.import_('mod2', global_, fromlist=['attr'], level=1)
import_util.import_('pkg') # For __import__().
module = import_util.import_('mod2', global_, fromlist=['attr'],
level=1)
self.assertEqual(module.__name__, 'pkg.mod2')
self.assertEqual(module.attr, 'pkg.mod2')
self.relative_import_test(create, globals_, callback)
@ -99,8 +101,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg'},
{'__name__': 'pkg', '__path__': ['blah']})
def callback(global_):
util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['module'],
import_util.import_('pkg') # For __import__().
module = import_util.import_('', global_, fromlist=['module'],
level=1)
self.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module'))
@ -112,8 +114,8 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.module'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
def callback(global_):
util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['attr'], level=1)
import_util.import_('pkg') # For __import__().
module = import_util.import_('', global_, fromlist=['attr'], level=1)
self.assertEqual(module.__name__, 'pkg')
self.relative_import_test(create, globals_, callback)
@ -124,7 +126,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg.subpkg1'},
{'__name__': 'pkg.subpkg1', '__path__': ['blah']})
def callback(global_):
module = util.import_('', global_, fromlist=['subpkg2'], level=2)
module = import_util.import_('', global_, fromlist=['subpkg2'],
level=2)
self.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'subpkg2'))
self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')
@ -139,8 +142,8 @@ class RelativeImports(unittest.TestCase):
{'__name__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5',
'__path__': ['blah']})
def callback(global_):
util.import_(globals_[0]['__package__'])
module = util.import_('', global_, fromlist=['attr'], level=6)
import_util.import_(globals_[0]['__package__'])
module = import_util.import_('', global_, fromlist=['attr'], level=6)
self.assertEqual(module.__name__, 'pkg')
self.relative_import_test(create, globals_, callback)
@ -150,8 +153,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg'},
{'__name__': 'pkg', '__path__': ['blah']})
def callback(global_):
util.import_('pkg')
self.assertRaises(ValueError, util.import_, '', global_,
import_util.import_('pkg')
self.assertRaises(ValueError, import_util.import_, '', global_,
fromlist=['top_level'], level=2)
self.relative_import_test(create, globals_, callback)
@ -160,14 +163,14 @@ class RelativeImports(unittest.TestCase):
create = ['top_level', 'pkg.__init__', 'pkg.module']
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
def callback(global_):
util.import_('pkg')
self.assertRaises(ValueError, util.import_, '', global_,
import_util.import_('pkg')
self.assertRaises(ValueError, import_util.import_, '', global_,
fromlist=['top_level'], level=2)
self.relative_import_test(create, globals_, callback)
def test_empty_name_w_level_0(self):
# [empty name]
self.assertRaises(ValueError, util.import_, '')
self.assertRaises(ValueError, import_util.import_, '')
def test_import_from_different_package(self):
# Test importing from a different package than the caller.
@ -181,8 +184,8 @@ class RelativeImports(unittest.TestCase):
'__runpy_pkg__.uncle.cousin.nephew']
globals_ = {'__package__': '__runpy_pkg__.__runpy_pkg__'}
def callback(global_):
util.import_('__runpy_pkg__.__runpy_pkg__')
module = util.import_('uncle.cousin', globals_, {},
import_util.import_('__runpy_pkg__.__runpy_pkg__')
module = import_util.import_('uncle.cousin', globals_, {},
fromlist=['nephew'],
level=2)
self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin')

View File

@ -0,0 +1,33 @@
import functools
import importlib
using___import__ = False
def import_(*args, **kwargs):
"""Delegate to allow for injecting different implementations of import."""
if using___import__:
return __import__(*args, **kwargs)
return importlib.Import()(*args, **kwargs)
def importlib_only(fxn):
"""Decorator to mark which tests are not supported by the current
implementation of __import__()."""
def inner(*args, **kwargs):
if using___import__:
return
else:
return fxn(*args, **kwargs)
functools.update_wrapper(inner, fxn)
return inner
def mock_path_hook(*entries, importer):
"""A mock sys.path_hooks entry."""
def hook(entry):
if entry not in entries:
raise ImportError
return importer
return hook

View File

@ -1,33 +1,10 @@
from importlib import Import
from contextlib import contextmanager
from functools import update_wrapper
import imp
import os.path
from test.support import unlink
import sys
using___import__ = False
def import_(*args, **kwargs):
"""Delegate to allow for injecting different implementations of import."""
if using___import__:
return __import__(*args, **kwargs)
return Import()(*args, **kwargs)
def importlib_only(fxn):
"""Decorator to mark which tests are not supported by the current
implementation of __import__()."""
def inner(*args, **kwargs):
if using___import__:
return
else:
return fxn(*args, **kwargs)
update_wrapper(inner, fxn)
return inner
def case_insensitive_tests(class_):
"""Class decorator that nullifies tests that require a case-insensitive
file system."""
@ -142,12 +119,3 @@ class mock_modules:
def __exit__(self, *exc_info):
self._uncache.__exit__(None, None, None)
def mock_path_hook(*entries, importer):
"""A mock sys.path_hooks entry."""
def hook(entry):
if entry not in entries:
raise ImportError
return importer
return hook