Kaydet (Commit) d720b362 authored tarafından Brett Cannon's avatar Brett Cannon

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

üst bcb26c53
...@@ -3,10 +3,6 @@ to do ...@@ -3,10 +3,6 @@ to do
* Reorganize support code. * 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. + Add a file loader mock that returns monotonically increasing mtime.
- Use in source/test_reload. - Use in source/test_reload.
- Use in source/test_load_module_mixed. - Use in source/test_load_module_mixed.
......
...@@ -6,6 +6,7 @@ of using the typical __path__/__name__ test). ...@@ -6,6 +6,7 @@ of using the typical __path__/__name__ test).
""" """
import unittest import unittest
from .. import util from .. import util
from . import util as import_util
class Using__package__(unittest.TestCase): class Using__package__(unittest.TestCase):
...@@ -36,8 +37,8 @@ class Using__package__(unittest.TestCase): ...@@ -36,8 +37,8 @@ class Using__package__(unittest.TestCase):
# [__package__] # [__package__]
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer: with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
with util.import_state(meta_path=[importer]): with util.import_state(meta_path=[importer]):
util.import_('pkg.fake') import_util.import_('pkg.fake')
module = util.import_('', globals={'__package__': 'pkg.fake'}, module = import_util.import_('', globals={'__package__': 'pkg.fake'},
fromlist=['attr'], level=2) fromlist=['attr'], level=2)
self.assertEquals(module.__name__, 'pkg') self.assertEquals(module.__name__, 'pkg')
...@@ -45,8 +46,8 @@ class Using__package__(unittest.TestCase): ...@@ -45,8 +46,8 @@ class Using__package__(unittest.TestCase):
# [__name__] # [__name__]
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer: with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
with util.import_state(meta_path=[importer]): with util.import_state(meta_path=[importer]):
util.import_('pkg.fake') import_util.import_('pkg.fake')
module = util.import_('', module = import_util.import_('',
globals={'__name__': 'pkg.fake', globals={'__name__': 'pkg.fake',
'__path__': []}, '__path__': []},
fromlist=['attr'], level=2) fromlist=['attr'], level=2)
...@@ -54,12 +55,12 @@ class Using__package__(unittest.TestCase): ...@@ -54,12 +55,12 @@ class Using__package__(unittest.TestCase):
def test_bad__package__(self): def test_bad__package__(self):
globals = {'__package__': '<not real>'} globals = {'__package__': '<not real>'}
self.assertRaises(SystemError, util.import_,'', globals, {}, self.assertRaises(SystemError, import_util.import_,'', globals, {},
['relimport'], 1) ['relimport'], 1)
def test_bunk__package__(self): def test_bunk__package__(self):
globals = {'__package__': 42} globals = {'__package__': 42}
self.assertRaises(ValueError, util.import_, '', globals, {}, self.assertRaises(ValueError, import_util.import_, '', globals, {},
['relimport'], 1) ['relimport'], 1)
...@@ -80,7 +81,7 @@ class Setting__package__(unittest.TestCase): ...@@ -80,7 +81,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('top_level') as mock: with util.mock_modules('top_level') as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
del mock['top_level'].__package__ del mock['top_level'].__package__
module = util.import_('top_level') module = import_util.import_('top_level')
self.assert_(module.__package__ is None) self.assert_(module.__package__ is None)
# [package] # [package]
...@@ -88,7 +89,7 @@ class Setting__package__(unittest.TestCase): ...@@ -88,7 +89,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('pkg.__init__') as mock: with util.mock_modules('pkg.__init__') as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
del mock['pkg'].__package__ del mock['pkg'].__package__
module = util.import_('pkg') module = import_util.import_('pkg')
self.assertEqual(module.__package__, 'pkg') self.assertEqual(module.__package__, 'pkg')
# [submodule] # [submodule]
...@@ -96,7 +97,7 @@ class Setting__package__(unittest.TestCase): ...@@ -96,7 +97,7 @@ class Setting__package__(unittest.TestCase):
with util.mock_modules('pkg.__init__', 'pkg.mod') as mock: with util.mock_modules('pkg.__init__', 'pkg.mod') as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
del mock['pkg.mod'].__package__ del mock['pkg.mod'].__package__
pkg = util.import_('pkg.mod') pkg = import_util.import_('pkg.mod')
module = getattr(pkg, 'mod') module = getattr(pkg, 'mod')
self.assertEqual(module.__package__, 'pkg') self.assertEqual(module.__package__, 'pkg')
......
"""Test that sys.modules is used properly by import.""" """Test that sys.modules is used properly by import."""
from .. import util from .. import util
from . import util as import_util
import sys import sys
from types import MethodType from types import MethodType
import unittest import unittest
...@@ -23,7 +24,7 @@ class UseCache(unittest.TestCase): ...@@ -23,7 +24,7 @@ class UseCache(unittest.TestCase):
# [use cache] # [use cache]
module_to_use = "some module found!" module_to_use = "some module found!"
sys.modules['some_module'] = module_to_use 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)) self.assertEqual(id(module_to_use), id(module))
def create_mock(self, *names, return_=None): def create_mock(self, *names, return_=None):
...@@ -37,31 +38,31 @@ class UseCache(unittest.TestCase): ...@@ -37,31 +38,31 @@ class UseCache(unittest.TestCase):
# __import__ inconsistent between loaders and built-in import when it comes # __import__ inconsistent between loaders and built-in import when it comes
# to when to use the module in sys.modules and when not to. # 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): def test_using_cache_after_loader(self):
# [from cache on return] # [from cache on return]
with self.create_mock('module') as mock: with self.create_mock('module') as mock:
with util.import_state(meta_path=[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'])) self.assertEquals(id(module), id(sys.modules['module']))
# See test_using_cache_after_loader() for reasoning. # See test_using_cache_after_loader() for reasoning.
@util.importlib_only @import_util.importlib_only
def test_using_cache_for_assigning_to_attribute(self): def test_using_cache_for_assigning_to_attribute(self):
# [from cache to attribute] # [from cache to attribute]
with self.create_mock('pkg.__init__', 'pkg.module') as importer: with self.create_mock('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[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_(hasattr(module, 'module'))
self.assert_(id(module.module), id(sys.modules['pkg.module'])) self.assert_(id(module.module), id(sys.modules['pkg.module']))
# See test_using_cache_after_loader() for reasoning. # See test_using_cache_after_loader() for reasoning.
@util.importlib_only @import_util.importlib_only
def test_using_cache_for_fromlist(self): def test_using_cache_for_fromlist(self):
# [from cache for fromlist] # [from cache for fromlist]
with self.create_mock('pkg.__init__', 'pkg.module') as importer: with self.create_mock('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[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.assert_(hasattr(module, 'module'))
self.assertEquals(id(module.module), id(sys.modules['pkg.module'])) self.assertEquals(id(module.module), id(sys.modules['pkg.module']))
......
"""Test that the semantics relating to the 'fromlist' argument are correct.""" """Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util from .. import util
from . import util as import_util
import unittest import unittest
class ReturnValue(unittest.TestCase): class ReturnValue(unittest.TestCase):
...@@ -17,14 +18,14 @@ class ReturnValue(unittest.TestCase): ...@@ -17,14 +18,14 @@ class ReturnValue(unittest.TestCase):
# [import return] # [import return]
with util.mock_modules('pkg.__init__', 'pkg.module') as importer: with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[importer]): with util.import_state(meta_path=[importer]):
module = util.import_('pkg.module') module = import_util.import_('pkg.module')
self.assertEquals(module.__name__, 'pkg') self.assertEquals(module.__name__, 'pkg')
def test_return_from_from_import(self): def test_return_from_from_import(self):
# [from return] # [from return]
with util.mock_modules('pkg.__init__', 'pkg.module')as importer: with util.mock_modules('pkg.__init__', 'pkg.module')as importer:
with util.import_state(meta_path=[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') self.assertEquals(module.__name__, 'pkg.module')
...@@ -49,14 +50,14 @@ class HandlingFromlist(unittest.TestCase): ...@@ -49,14 +50,14 @@ class HandlingFromlist(unittest.TestCase):
# [object case] # [object case]
with util.mock_modules('module') as importer: with util.mock_modules('module') as importer:
with util.import_state(meta_path=[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') self.assertEquals(module.__name__, 'module')
def test_unexistent_object(self): def test_unexistent_object(self):
# [bad object] # [bad object]
with util.mock_modules('module') as importer: with util.mock_modules('module') as importer:
with util.import_state(meta_path=[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.assertEquals(module.__name__, 'module')
self.assert_(not hasattr(module, 'non_existent')) self.assert_(not hasattr(module, 'non_existent'))
...@@ -64,7 +65,7 @@ class HandlingFromlist(unittest.TestCase): ...@@ -64,7 +65,7 @@ class HandlingFromlist(unittest.TestCase):
# [module] # [module]
with util.mock_modules('pkg.__init__', 'pkg.module') as importer: with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
with util.import_state(meta_path=[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.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module')) self.assert_(hasattr(module, 'module'))
self.assertEquals(module.module.__name__, 'pkg.module') self.assertEquals(module.module.__name__, 'pkg.module')
...@@ -73,14 +74,14 @@ class HandlingFromlist(unittest.TestCase): ...@@ -73,14 +74,14 @@ class HandlingFromlist(unittest.TestCase):
# [no module] # [no module]
with util.mock_modules('pkg.__init__') as importer: with util.mock_modules('pkg.__init__') as importer:
with util.import_state(meta_path=[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.assertEquals(module.__name__, 'pkg')
self.assert_(not hasattr(module, 'non_existent')) self.assert_(not hasattr(module, 'non_existent'))
def test_empty_string(self): def test_empty_string(self):
with util.mock_modules('pkg.__init__', 'pkg.mod') as importer: with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
with util.import_state(meta_path=[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') self.assertEquals(module.__name__, 'pkg.mod')
def test_using_star(self): def test_using_star(self):
...@@ -88,7 +89,7 @@ class HandlingFromlist(unittest.TestCase): ...@@ -88,7 +89,7 @@ class HandlingFromlist(unittest.TestCase):
with util.mock_modules('pkg.__init__', 'pkg.module') as mock: with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
mock['pkg'].__all__ = ['module'] mock['pkg'].__all__ = ['module']
module = util.import_('pkg', fromlist=['*']) module = import_util.import_('pkg', fromlist=['*'])
self.assertEquals(module.__name__, 'pkg') self.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module')) self.assert_(hasattr(module, 'module'))
self.assertEqual(module.module.__name__, 'pkg.module') self.assertEqual(module.module.__name__, 'pkg.module')
...@@ -99,7 +100,7 @@ class HandlingFromlist(unittest.TestCase): ...@@ -99,7 +100,7 @@ class HandlingFromlist(unittest.TestCase):
with context as mock: with context as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
mock['pkg'].__all__ = ['module1'] mock['pkg'].__all__ = ['module1']
module = util.import_('pkg', fromlist=['module2', '*']) module = import_util.import_('pkg', fromlist=['module2', '*'])
self.assertEquals(module.__name__, 'pkg') self.assertEquals(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module1')) self.assert_(hasattr(module, 'module1'))
self.assert_(hasattr(module, 'module2')) self.assert_(hasattr(module, 'module2'))
......
from .. import util from .. import util
from . import util as import_util
from contextlib import nested from contextlib import nested
from types import MethodType from types import MethodType
import unittest import unittest
...@@ -22,7 +23,7 @@ class CallingOrder(unittest.TestCase): ...@@ -22,7 +23,7 @@ class CallingOrder(unittest.TestCase):
first.modules[mod] = 42 first.modules[mod] = 42
second.modules[mod] = -13 second.modules[mod] = -13
with util.import_state(meta_path=[first, second]): 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): def test_continuing(self):
# [continuing] # [continuing]
...@@ -33,7 +34,7 @@ class CallingOrder(unittest.TestCase): ...@@ -33,7 +34,7 @@ class CallingOrder(unittest.TestCase):
first.find_module = lambda self, fullname, path=None: None first.find_module = lambda self, fullname, path=None: None
second.modules[mod_name] = 42 second.modules[mod_name] = 42
with util.import_state(meta_path=[first, second]): 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): class CallSignature(unittest.TestCase):
...@@ -58,7 +59,7 @@ class CallSignature(unittest.TestCase): ...@@ -58,7 +59,7 @@ class CallSignature(unittest.TestCase):
log, wrapped_call = self.log(importer.find_module) log, wrapped_call = self.log(importer.find_module)
importer.find_module = MethodType(wrapped_call, importer) importer.find_module = MethodType(wrapped_call, importer)
with util.import_state(meta_path=[importer]): with util.import_state(meta_path=[importer]):
util.import_(mod_name) import_util.import_(mod_name)
assert len(log) == 1 assert len(log) == 1
args = log[0][0] args = log[0][0]
kwargs = log[0][1] kwargs = log[0][1]
...@@ -79,7 +80,7 @@ class CallSignature(unittest.TestCase): ...@@ -79,7 +80,7 @@ class CallSignature(unittest.TestCase):
log, wrapped_call = self.log(importer.find_module) log, wrapped_call = self.log(importer.find_module)
importer.find_module = MethodType(wrapped_call, importer) importer.find_module = MethodType(wrapped_call, importer)
with util.import_state(meta_path=[importer]): with util.import_state(meta_path=[importer]):
util.import_(mod_name) import_util.import_(mod_name)
assert len(log) == 2 assert len(log) == 2
args = log[1][0] args = log[1][0]
kwargs = log[1][1] kwargs = log[1][1]
......
from .. import util from .. import util
from . import util as import_util
import sys import sys
import unittest import unittest
import importlib import importlib
...@@ -11,13 +12,14 @@ class ParentModuleTests(unittest.TestCase): ...@@ -11,13 +12,14 @@ class ParentModuleTests(unittest.TestCase):
def test_import_parent(self): def test_import_parent(self):
with util.mock_modules('pkg.__init__', 'pkg.module') as mock: with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
with util.import_state(meta_path=[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) self.assert_('pkg' in sys.modules)
def test_bad_parent(self): def test_bad_parent(self):
with util.mock_modules('pkg.module') as mock: with util.mock_modules('pkg.module') as mock:
with util.import_state(meta_path=[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(): def test_main():
......
from .. import util from .. import util
from . import util as import_util
from contextlib import nested from contextlib import nested
from imp import new_module from imp import new_module
import sys import sys
...@@ -41,11 +42,11 @@ class BaseTests(unittest.TestCase): ...@@ -41,11 +42,11 @@ class BaseTests(unittest.TestCase):
with nested(misser, hitter): with nested(misser, hitter):
cache = dict(zip(search_path, (misser, hitter))) cache = dict(zip(search_path, (misser, hitter)))
with util.import_state(path=path, path_importer_cache=cache): 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[0], misser)
self.assertEquals(log[1], hitter) 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=[]): def cache_use_test(self, to_import, entry, path=[]):
# [cache check], [cache use] # [cache check], [cache use]
log = [] log = []
...@@ -58,7 +59,7 @@ class BaseTests(unittest.TestCase): ...@@ -58,7 +59,7 @@ class BaseTests(unittest.TestCase):
cache = LoggingDict() cache = LoggingDict()
cache[entry] = importer cache[entry] = importer
with util.import_state(path=[entry], path_importer_cache=cache): 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.assert_(module is importer[to_import])
self.assertEquals(len(cache), 1) self.assertEquals(len(cache), 1)
self.assertEquals([entry], log) self.assertEquals([entry], log)
...@@ -70,10 +71,10 @@ class BaseTests(unittest.TestCase): ...@@ -70,10 +71,10 @@ class BaseTests(unittest.TestCase):
log.append(entry) log.append(entry)
raise ImportError raise ImportError
with util.mock_modules(to_import) as importer: 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] path_hooks = [logging_hook, logging_hook, hitter]
with util.import_state(path_hooks=path_hooks, path=path): 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(sys.path_importer_cache[entry], importer)
self.assertEquals(len(log), 2) self.assertEquals(len(log), 2)
...@@ -88,7 +89,7 @@ class BaseTests(unittest.TestCase): ...@@ -88,7 +89,7 @@ class BaseTests(unittest.TestCase):
raise ImportError raise ImportError
try: try:
util.import_(to_import) import_util.import_(to_import)
except ImportError: except ImportError:
pass pass
...@@ -126,7 +127,7 @@ class __path__Tests(BaseTests): ...@@ -126,7 +127,7 @@ class __path__Tests(BaseTests):
test('pkg.hit', entry, *args) 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): def test_order(self):
self.run_test(self.order_test, 'second', ('first', 'second'), ['first', self.run_test(self.order_test, 'second', ('first', 'second'), ['first',
'second']) 'second'])
......
"""Test relative imports (PEP 328).""" """Test relative imports (PEP 328)."""
from .. import util from .. import util
from . import util as import_util
import sys import sys
import unittest import unittest
...@@ -75,8 +76,8 @@ class RelativeImports(unittest.TestCase): ...@@ -75,8 +76,8 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.mod2' create = 'pkg.__init__', 'pkg.mod2'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'} globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
def callback(global_): def callback(global_):
util.import_('pkg') # For __import__(). import_util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['mod2'], level=1) module = import_util.import_('', global_, fromlist=['mod2'], level=1)
self.assertEqual(module.__name__, 'pkg') self.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'mod2')) self.assert_(hasattr(module, 'mod2'))
self.assertEqual(module.mod2.attr, 'pkg.mod2') self.assertEqual(module.mod2.attr, 'pkg.mod2')
...@@ -87,8 +88,9 @@ class RelativeImports(unittest.TestCase): ...@@ -87,8 +88,9 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.mod2' create = 'pkg.__init__', 'pkg.mod2'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'} globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
def callback(global_): def callback(global_):
util.import_('pkg') # For __import__(). import_util.import_('pkg') # For __import__().
module = util.import_('mod2', global_, fromlist=['attr'], level=1) module = import_util.import_('mod2', global_, fromlist=['attr'],
level=1)
self.assertEqual(module.__name__, 'pkg.mod2') self.assertEqual(module.__name__, 'pkg.mod2')
self.assertEqual(module.attr, 'pkg.mod2') self.assertEqual(module.attr, 'pkg.mod2')
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
...@@ -99,8 +101,8 @@ class RelativeImports(unittest.TestCase): ...@@ -99,8 +101,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg'}, globals_ = ({'__package__': 'pkg'},
{'__name__': 'pkg', '__path__': ['blah']}) {'__name__': 'pkg', '__path__': ['blah']})
def callback(global_): def callback(global_):
util.import_('pkg') # For __import__(). import_util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['module'], module = import_util.import_('', global_, fromlist=['module'],
level=1) level=1)
self.assertEqual(module.__name__, 'pkg') self.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'module')) self.assert_(hasattr(module, 'module'))
...@@ -112,8 +114,8 @@ class RelativeImports(unittest.TestCase): ...@@ -112,8 +114,8 @@ class RelativeImports(unittest.TestCase):
create = 'pkg.__init__', 'pkg.module' create = 'pkg.__init__', 'pkg.module'
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
def callback(global_): def callback(global_):
util.import_('pkg') # For __import__(). import_util.import_('pkg') # For __import__().
module = util.import_('', global_, fromlist=['attr'], level=1) module = import_util.import_('', global_, fromlist=['attr'], level=1)
self.assertEqual(module.__name__, 'pkg') self.assertEqual(module.__name__, 'pkg')
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
...@@ -124,7 +126,8 @@ class RelativeImports(unittest.TestCase): ...@@ -124,7 +126,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg.subpkg1'}, globals_ = ({'__package__': 'pkg.subpkg1'},
{'__name__': 'pkg.subpkg1', '__path__': ['blah']}) {'__name__': 'pkg.subpkg1', '__path__': ['blah']})
def callback(global_): 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.assertEqual(module.__name__, 'pkg')
self.assert_(hasattr(module, 'subpkg2')) self.assert_(hasattr(module, 'subpkg2'))
self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__') self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')
...@@ -139,8 +142,8 @@ class RelativeImports(unittest.TestCase): ...@@ -139,8 +142,8 @@ class RelativeImports(unittest.TestCase):
{'__name__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5', {'__name__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5',
'__path__': ['blah']}) '__path__': ['blah']})
def callback(global_): def callback(global_):
util.import_(globals_[0]['__package__']) import_util.import_(globals_[0]['__package__'])
module = util.import_('', global_, fromlist=['attr'], level=6) module = import_util.import_('', global_, fromlist=['attr'], level=6)
self.assertEqual(module.__name__, 'pkg') self.assertEqual(module.__name__, 'pkg')
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
...@@ -150,8 +153,8 @@ class RelativeImports(unittest.TestCase): ...@@ -150,8 +153,8 @@ class RelativeImports(unittest.TestCase):
globals_ = ({'__package__': 'pkg'}, globals_ = ({'__package__': 'pkg'},
{'__name__': 'pkg', '__path__': ['blah']}) {'__name__': 'pkg', '__path__': ['blah']})
def callback(global_): def callback(global_):
util.import_('pkg') import_util.import_('pkg')
self.assertRaises(ValueError, util.import_, '', global_, self.assertRaises(ValueError, import_util.import_, '', global_,
fromlist=['top_level'], level=2) fromlist=['top_level'], level=2)
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
...@@ -160,14 +163,14 @@ class RelativeImports(unittest.TestCase): ...@@ -160,14 +163,14 @@ class RelativeImports(unittest.TestCase):
create = ['top_level', 'pkg.__init__', 'pkg.module'] create = ['top_level', 'pkg.__init__', 'pkg.module']
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
def callback(global_): def callback(global_):
util.import_('pkg') import_util.import_('pkg')
self.assertRaises(ValueError, util.import_, '', global_, self.assertRaises(ValueError, import_util.import_, '', global_,
fromlist=['top_level'], level=2) fromlist=['top_level'], level=2)
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
def test_empty_name_w_level_0(self): def test_empty_name_w_level_0(self):
# [empty name] # [empty name]
self.assertRaises(ValueError, util.import_, '') self.assertRaises(ValueError, import_util.import_, '')
def test_import_from_different_package(self): def test_import_from_different_package(self):
# Test importing from a different package than the caller. # Test importing from a different package than the caller.
...@@ -181,8 +184,8 @@ class RelativeImports(unittest.TestCase): ...@@ -181,8 +184,8 @@ class RelativeImports(unittest.TestCase):
'__runpy_pkg__.uncle.cousin.nephew'] '__runpy_pkg__.uncle.cousin.nephew']
globals_ = {'__package__': '__runpy_pkg__.__runpy_pkg__'} globals_ = {'__package__': '__runpy_pkg__.__runpy_pkg__'}
def callback(global_): def callback(global_):
util.import_('__runpy_pkg__.__runpy_pkg__') import_util.import_('__runpy_pkg__.__runpy_pkg__')
module = util.import_('uncle.cousin', globals_, {}, module = import_util.import_('uncle.cousin', globals_, {},
fromlist=['nephew'], fromlist=['nephew'],
level=2) level=2)
self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin') self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin')
......
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
from importlib import Import
from contextlib import contextmanager from contextlib import contextmanager
from functools import update_wrapper
import imp import imp
import os.path import os.path
from test.support import unlink from test.support import unlink
import sys 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_): def case_insensitive_tests(class_):
"""Class decorator that nullifies tests that require a case-insensitive """Class decorator that nullifies tests that require a case-insensitive
file system.""" file system."""
...@@ -142,12 +119,3 @@ class mock_modules: ...@@ -142,12 +119,3 @@ class mock_modules:
def __exit__(self, *exc_info): def __exit__(self, *exc_info):
self._uncache.__exit__(None, None, None) 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment