1
from contextlib import contextmanager
4
from test.support import unlink
9
def case_insensitive_tests(class_):
10
"""Class decorator that nullifies tests that require a case-insensitive
12
if sys.platform not in ('win32', 'darwin', 'cygwin'):
13
return unittest.TestCase
20
"""Uncache a module from sys.modules.
22
A basic sanity check is performed to prevent uncaching modules that either
23
cannot/shouldn't be uncached.
27
if name in ('sys', 'marshal', 'imp'):
29
"cannot uncache {0} as it will break _importlib".format(name))
44
def import_state(**kwargs):
45
"""Context manager to manage the various importers and stored state in the
48
The 'modules' attribute is not supported as the interpreter state stores a
49
pointer to the dict that the interpreter uses internally;
50
reassigning to sys.modules does not have the desired effect.
55
for attr, default in (('meta_path', []), ('path', []),
57
('path_importer_cache', {})):
58
originals[attr] = getattr(sys, attr)
60
new_value = kwargs[attr]
64
setattr(sys, attr, new_value)
67
'unrecognized arguments: {0}'.format(kwargs.keys()))
70
for attr, value in originals.items():
71
setattr(sys, attr, value)
76
"""A mock importer/loader."""
78
def __init__(self, *names):
81
if not name.endswith('.__init__'):
84
import_name = name[:-len('.__init__')]
87
elif import_name == name:
88
package = name.rsplit('.', 1)[0]
91
module = imp.new_module(import_name)
92
module.__loader__ = self
93
module.__file__ = '<mock __file__>'
94
module.__package__ = package
96
if import_name != name:
97
module.__path__ = ['<mock __path__>']
98
self.modules[import_name] = module
100
def __getitem__(self, name):
101
return self.modules[name]
103
def find_module(self, fullname, path=None):
104
if fullname not in self.modules:
109
def load_module(self, fullname):
110
if fullname not in self.modules:
113
sys.modules[fullname] = self.modules[fullname]
114
return self.modules[fullname]
117
self._uncache = uncache(*self.modules.keys())
118
self._uncache.__enter__()
121
def __exit__(self, *exc_info):
122
self._uncache.__exit__(None, None, None)