2009-01-17 20:24:28 -04:00
|
|
|
from contextlib import contextmanager
|
2014-01-25 18:32:46 -04:00
|
|
|
from importlib import util, invalidate_caches
|
2009-01-17 20:24:28 -04:00
|
|
|
import os.path
|
2009-04-02 02:17:54 -03:00
|
|
|
from test import support
|
2009-02-06 22:06:43 -04:00
|
|
|
import unittest
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
2013-06-15 19:39:21 -03:00
|
|
|
import types
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
2013-10-04 15:47:14 -03:00
|
|
|
def import_importlib(module_name):
|
|
|
|
"""Import a module from importlib both w/ and w/o _frozen_importlib."""
|
|
|
|
fresh = ('importlib',) if '.' in module_name else ()
|
|
|
|
frozen = support.import_fresh_module(module_name)
|
|
|
|
source = support.import_fresh_module(module_name, fresh=fresh,
|
|
|
|
blocked=('_frozen_importlib',))
|
|
|
|
return frozen, source
|
|
|
|
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
def test_both(test_class, **kwargs):
|
|
|
|
frozen_tests = types.new_class('Frozen_'+test_class.__name__,
|
|
|
|
(test_class, unittest.TestCase))
|
|
|
|
source_tests = types.new_class('Source_'+test_class.__name__,
|
|
|
|
(test_class, unittest.TestCase))
|
2013-11-22 15:47:09 -04:00
|
|
|
frozen_tests.__module__ = source_tests.__module__ = test_class.__module__
|
2013-10-18 16:40:11 -03:00
|
|
|
for attr, (frozen_value, source_value) in kwargs.items():
|
|
|
|
setattr(frozen_tests, attr, frozen_value)
|
|
|
|
setattr(source_tests, attr, source_value)
|
|
|
|
return frozen_tests, source_tests
|
|
|
|
|
|
|
|
|
2009-07-19 22:05:40 -03:00
|
|
|
CASE_INSENSITIVE_FS = True
|
|
|
|
# Windows is the only OS that is *always* case-insensitive
|
|
|
|
# (OS X *can* be case-sensitive).
|
|
|
|
if sys.platform not in ('win32', 'cygwin'):
|
|
|
|
changed_name = __file__.upper()
|
|
|
|
if changed_name == __file__:
|
|
|
|
changed_name = __file__.lower()
|
|
|
|
if not os.path.exists(changed_name):
|
|
|
|
CASE_INSENSITIVE_FS = False
|
|
|
|
|
|
|
|
|
|
|
|
def case_insensitive_tests(test):
|
2009-05-10 22:47:11 -03:00
|
|
|
"""Class decorator that nullifies tests requiring a case-insensitive
|
2009-01-18 02:55:05 -04:00
|
|
|
file system."""
|
2009-07-19 22:05:40 -03:00
|
|
|
return unittest.skipIf(not CASE_INSENSITIVE_FS,
|
|
|
|
"requires a case-insensitive filesystem")(test)
|
2009-01-18 02:55:05 -04:00
|
|
|
|
|
|
|
|
2014-01-25 18:32:46 -04:00
|
|
|
def submodule(parent, name, pkg_dir, content=''):
|
|
|
|
path = os.path.join(pkg_dir, name + '.py')
|
|
|
|
with open(path, 'w') as subfile:
|
|
|
|
subfile.write(content)
|
|
|
|
return '{}.{}'.format(parent, name), path
|
|
|
|
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
@contextmanager
|
|
|
|
def uncache(*names):
|
|
|
|
"""Uncache a module from sys.modules.
|
|
|
|
|
|
|
|
A basic sanity check is performed to prevent uncaching modules that either
|
|
|
|
cannot/shouldn't be uncached.
|
|
|
|
|
|
|
|
"""
|
|
|
|
for name in names:
|
|
|
|
if name in ('sys', 'marshal', 'imp'):
|
|
|
|
raise ValueError(
|
2012-04-14 15:10:13 -03:00
|
|
|
"cannot uncache {0}".format(name))
|
2009-01-17 20:24:28 -04:00
|
|
|
try:
|
|
|
|
del sys.modules[name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
del sys.modules[name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2014-01-25 18:32:46 -04:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def temp_module(name, content='', *, pkg=False):
|
|
|
|
conflicts = [n for n in sys.modules if n.partition('.')[0] == name]
|
|
|
|
with support.temp_cwd(None) as cwd:
|
|
|
|
with uncache(name, *conflicts):
|
|
|
|
with support.DirsOnSysPath(cwd):
|
|
|
|
invalidate_caches()
|
|
|
|
|
|
|
|
location = os.path.join(cwd, name)
|
|
|
|
if pkg:
|
|
|
|
modpath = os.path.join(location, '__init__.py')
|
|
|
|
os.mkdir(name)
|
|
|
|
else:
|
|
|
|
modpath = location + '.py'
|
|
|
|
if content is None:
|
|
|
|
# Make sure the module file gets created.
|
|
|
|
content = ''
|
|
|
|
if content is not None:
|
|
|
|
# not a namespace package
|
|
|
|
with open(modpath, 'w') as modfile:
|
|
|
|
modfile.write(content)
|
|
|
|
yield location
|
|
|
|
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
@contextmanager
|
|
|
|
def import_state(**kwargs):
|
|
|
|
"""Context manager to manage the various importers and stored state in the
|
|
|
|
sys module.
|
|
|
|
|
|
|
|
The 'modules' attribute is not supported as the interpreter state stores a
|
|
|
|
pointer to the dict that the interpreter uses internally;
|
|
|
|
reassigning to sys.modules does not have the desired effect.
|
|
|
|
|
|
|
|
"""
|
|
|
|
originals = {}
|
|
|
|
try:
|
|
|
|
for attr, default in (('meta_path', []), ('path', []),
|
|
|
|
('path_hooks', []),
|
|
|
|
('path_importer_cache', {})):
|
|
|
|
originals[attr] = getattr(sys, attr)
|
|
|
|
if attr in kwargs:
|
|
|
|
new_value = kwargs[attr]
|
|
|
|
del kwargs[attr]
|
|
|
|
else:
|
|
|
|
new_value = default
|
|
|
|
setattr(sys, attr, new_value)
|
|
|
|
if len(kwargs):
|
|
|
|
raise ValueError(
|
|
|
|
'unrecognized arguments: {0}'.format(kwargs.keys()))
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for attr, value in originals.items():
|
|
|
|
setattr(sys, attr, value)
|
|
|
|
|
|
|
|
|
2013-12-06 13:07:25 -04:00
|
|
|
class _ImporterMock:
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-12-06 13:07:25 -04:00
|
|
|
"""Base class to help with creating importer mocks."""
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2011-12-15 00:23:46 -04:00
|
|
|
def __init__(self, *names, module_code={}):
|
2009-01-17 20:24:28 -04:00
|
|
|
self.modules = {}
|
2011-12-15 00:23:46 -04:00
|
|
|
self.module_code = {}
|
2009-01-17 20:24:28 -04:00
|
|
|
for name in names:
|
|
|
|
if not name.endswith('.__init__'):
|
|
|
|
import_name = name
|
|
|
|
else:
|
|
|
|
import_name = name[:-len('.__init__')]
|
|
|
|
if '.' not in name:
|
|
|
|
package = None
|
|
|
|
elif import_name == name:
|
|
|
|
package = name.rsplit('.', 1)[0]
|
|
|
|
else:
|
|
|
|
package = import_name
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType(import_name)
|
2009-01-17 20:24:28 -04:00
|
|
|
module.__loader__ = self
|
|
|
|
module.__file__ = '<mock __file__>'
|
|
|
|
module.__package__ = package
|
|
|
|
module.attr = name
|
|
|
|
if import_name != name:
|
|
|
|
module.__path__ = ['<mock __path__>']
|
|
|
|
self.modules[import_name] = module
|
2011-12-15 00:23:46 -04:00
|
|
|
if import_name in module_code:
|
|
|
|
self.module_code[import_name] = module_code[import_name]
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.modules[name]
|
|
|
|
|
2013-12-06 13:07:25 -04:00
|
|
|
def __enter__(self):
|
|
|
|
self._uncache = uncache(*self.modules.keys())
|
|
|
|
self._uncache.__enter__()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *exc_info):
|
|
|
|
self._uncache.__exit__(None, None, None)
|
|
|
|
|
|
|
|
|
|
|
|
class mock_modules(_ImporterMock):
|
|
|
|
|
|
|
|
"""Importer mock using PEP 302 APIs."""
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
def find_module(self, fullname, path=None):
|
|
|
|
if fullname not in self.modules:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self
|
|
|
|
|
|
|
|
def load_module(self, fullname):
|
|
|
|
if fullname not in self.modules:
|
|
|
|
raise ImportError
|
|
|
|
else:
|
|
|
|
sys.modules[fullname] = self.modules[fullname]
|
2011-12-15 00:23:46 -04:00
|
|
|
if fullname in self.module_code:
|
2012-05-07 16:41:59 -03:00
|
|
|
try:
|
|
|
|
self.module_code[fullname]()
|
|
|
|
except Exception:
|
|
|
|
del sys.modules[fullname]
|
|
|
|
raise
|
2009-01-17 20:24:28 -04:00
|
|
|
return self.modules[fullname]
|
|
|
|
|
2013-12-06 13:07:25 -04:00
|
|
|
class mock_spec(_ImporterMock):
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-12-06 13:07:25 -04:00
|
|
|
"""Importer mock using PEP 451 APIs."""
|
|
|
|
|
|
|
|
def find_spec(self, fullname, path=None, parent=None):
|
|
|
|
try:
|
|
|
|
module = self.modules[fullname]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
is_package = hasattr(module, '__path__')
|
|
|
|
spec = util.spec_from_file_location(
|
|
|
|
fullname, module.__file__, loader=self,
|
|
|
|
submodule_search_locations=getattr(module, '__path__', None))
|
|
|
|
return spec
|
|
|
|
|
|
|
|
def create_module(self, spec):
|
|
|
|
if spec.name not in self.modules:
|
|
|
|
raise ImportError
|
|
|
|
return self.modules[spec.name]
|
|
|
|
|
|
|
|
def exec_module(self, module):
|
|
|
|
try:
|
|
|
|
self.module_code[module.__spec__.name]()
|
|
|
|
except KeyError:
|
|
|
|
pass
|