| OLD | NEW |
| 1 from contextlib import contextmanager | 1 from contextlib import contextmanager |
| 2 import imp | 2 import imp |
| 3 import os.path | 3 import os.path |
| 4 from test import support | 4 from test import support |
| 5 import unittest | 5 import unittest |
| 6 import sys | 6 import sys |
| 7 | 7 |
| 8 | 8 |
| 9 CASE_INSENSITIVE_FS = True | 9 CASE_INSENSITIVE_FS = True |
| 10 # Windows is the only OS that is *always* case-insensitive | 10 # Windows is the only OS that is *always* case-insensitive |
| 11 # (OS X *can* be case-sensitive). | 11 # (OS X *can* be case-sensitive). |
| 12 if sys.platform not in ('win32', 'cygwin'): | 12 if sys.platform not in ('win32', 'cygwin'): |
| 13 changed_name = __file__.upper() | 13 changed_name = __file__.upper() |
| 14 if changed_name == __file__: | 14 if changed_name == __file__: |
| 15 changed_name = __file__.lower() | 15 changed_name = __file__.lower() |
| 16 if not os.path.exists(changed_name): | 16 if not os.path.exists(changed_name): |
| 17 CASE_INSENSITIVE_FS = False | 17 CASE_INSENSITIVE_FS = False |
| 18 | 18 |
| 19 | 19 |
| 20 def case_insensitive_tests(test): | 20 def case_insensitive_tests(test): |
| 21 """Class decorator that nullifies tests requiring a case-insensitive | 21 """Class decorator that nullifies tests requiring a case-insensitive |
| 22 file system.""" | 22 file system.""" |
| 23 return unittest.skipIf(not CASE_INSENSITIVE_FS, | 23 return unittest.skipIf(not CASE_INSENSITIVE_FS, |
| 24 "requires a case-insensitive filesystem")(test) | 24 "requires a case-insensitive filesystem")(test) |
| 25 |
| 26 |
| 27 def set_bootstrap(importlib, bootstrap): |
| 28 """Set importlib to use the passed bootstrap module.""" |
| 29 importlib._w_long = bootstrap._w_long |
| 30 importlib._r_long = bootstrap._r_long |
| 31 importlib._bootstrap = bootstrap |
| 32 importlib.__import__ = bootstrap.__import__ |
| 33 sys.modules['importlib._bootstrap'] = bootstrap |
| 34 |
| 35 |
| 36 @contextmanager |
| 37 def bootstrap_context(importlib, bootstrap): |
| 38 """Temporarily use an alternate bootstrap module.""" |
| 39 old_bootstrap = importlib._bootstrap |
| 40 try: |
| 41 set_bootstrap(importlib, bootstrap) |
| 42 yield |
| 43 finally: |
| 44 set_bootstrap(importlib, old_bootstrap) |
| 45 |
| 46 |
| 47 def pure_bootstrap_context(importlib): |
| 48 """Temporarily use importlib._bootstrap.""" |
| 49 # importlib._bootstrap will normally be the same as _frozen_importlib |
| 50 if not hasattr(importlib._pure_bootstrap, 'sys'): |
| 51 importlib._pure_bootstrap._setup(sys, imp) |
| 52 return bootstrap_context(importlib, importlib._pure_bootstrap) |
| 25 | 53 |
| 26 | 54 |
| 27 @contextmanager | 55 @contextmanager |
| 28 def uncache(*names): | 56 def uncache(*names): |
| 29 """Uncache a module from sys.modules. | 57 """Uncache a module from sys.modules. |
| 30 | 58 |
| 31 A basic sanity check is performed to prevent uncaching modules that either | 59 A basic sanity check is performed to prevent uncaching modules that either |
| 32 cannot/shouldn't be uncached. | 60 cannot/shouldn't be uncached. |
| 33 | 61 |
| 34 """ | 62 """ |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 self.module_code[fullname]() | 155 self.module_code[fullname]() |
| 128 return self.modules[fullname] | 156 return self.modules[fullname] |
| 129 | 157 |
| 130 def __enter__(self): | 158 def __enter__(self): |
| 131 self._uncache = uncache(*self.modules.keys()) | 159 self._uncache = uncache(*self.modules.keys()) |
| 132 self._uncache.__enter__() | 160 self._uncache.__enter__() |
| 133 return self | 161 return self |
| 134 | 162 |
| 135 def __exit__(self, *exc_info): | 163 def __exit__(self, *exc_info): |
| 136 self._uncache.__exit__(None, None, None) | 164 self._uncache.__exit__(None, None, None) |
| OLD | NEW |