Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 1 | from contextlib import contextmanager |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 2 | import imp |
| 3 | import os.path |
Brett Cannon | a3d056e | 2009-04-02 05:17:54 +0000 | [diff] [blame] | 4 | from test import support |
Brett Cannon | 534b2cd | 2009-02-07 02:06:43 +0000 | [diff] [blame] | 5 | import unittest |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 6 | import sys |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 7 | |
| 8 | |
Brett Cannon | 4dc3193 | 2009-07-20 01:05:40 +0000 | [diff] [blame] | 9 | CASE_INSENSITIVE_FS = True |
| 10 | # Windows is the only OS that is *always* case-insensitive |
| 11 | # (OS X *can* be case-sensitive). |
| 12 | if sys.platform not in ('win32', 'cygwin'): |
| 13 | changed_name = __file__.upper() |
| 14 | if changed_name == __file__: |
| 15 | changed_name = __file__.lower() |
| 16 | if not os.path.exists(changed_name): |
| 17 | CASE_INSENSITIVE_FS = False |
| 18 | |
| 19 | |
| 20 | def case_insensitive_tests(test): |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 21 | """Class decorator that nullifies tests requiring a case-insensitive |
Brett Cannon | 2c5c79c | 2009-01-18 06:55:05 +0000 | [diff] [blame] | 22 | file system.""" |
Brett Cannon | 4dc3193 | 2009-07-20 01:05:40 +0000 | [diff] [blame] | 23 | return unittest.skipIf(not CASE_INSENSITIVE_FS, |
| 24 | "requires a case-insensitive filesystem")(test) |
Brett Cannon | 2c5c79c | 2009-01-18 06:55:05 +0000 | [diff] [blame] | 25 | |
| 26 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 27 | @contextmanager |
| 28 | def uncache(*names): |
| 29 | """Uncache a module from sys.modules. |
| 30 | |
| 31 | A basic sanity check is performed to prevent uncaching modules that either |
| 32 | cannot/shouldn't be uncached. |
| 33 | |
| 34 | """ |
| 35 | for name in names: |
| 36 | if name in ('sys', 'marshal', 'imp'): |
| 37 | raise ValueError( |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 38 | "cannot uncache {0}".format(name)) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 39 | try: |
| 40 | del sys.modules[name] |
| 41 | except KeyError: |
| 42 | pass |
| 43 | try: |
| 44 | yield |
| 45 | finally: |
| 46 | for name in names: |
| 47 | try: |
| 48 | del sys.modules[name] |
| 49 | except KeyError: |
| 50 | pass |
| 51 | |
| 52 | @contextmanager |
| 53 | def import_state(**kwargs): |
| 54 | """Context manager to manage the various importers and stored state in the |
| 55 | sys module. |
| 56 | |
| 57 | The 'modules' attribute is not supported as the interpreter state stores a |
| 58 | pointer to the dict that the interpreter uses internally; |
| 59 | reassigning to sys.modules does not have the desired effect. |
| 60 | |
| 61 | """ |
| 62 | originals = {} |
| 63 | try: |
| 64 | for attr, default in (('meta_path', []), ('path', []), |
| 65 | ('path_hooks', []), |
| 66 | ('path_importer_cache', {})): |
| 67 | originals[attr] = getattr(sys, attr) |
| 68 | if attr in kwargs: |
| 69 | new_value = kwargs[attr] |
| 70 | del kwargs[attr] |
| 71 | else: |
| 72 | new_value = default |
| 73 | setattr(sys, attr, new_value) |
| 74 | if len(kwargs): |
| 75 | raise ValueError( |
| 76 | 'unrecognized arguments: {0}'.format(kwargs.keys())) |
| 77 | yield |
| 78 | finally: |
| 79 | for attr, value in originals.items(): |
| 80 | setattr(sys, attr, value) |
| 81 | |
| 82 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 83 | class mock_modules: |
| 84 | |
| 85 | """A mock importer/loader.""" |
| 86 | |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 87 | def __init__(self, *names, module_code={}): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 88 | self.modules = {} |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 89 | self.module_code = {} |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 90 | for name in names: |
| 91 | if not name.endswith('.__init__'): |
| 92 | import_name = name |
| 93 | else: |
| 94 | import_name = name[:-len('.__init__')] |
| 95 | if '.' not in name: |
| 96 | package = None |
| 97 | elif import_name == name: |
| 98 | package = name.rsplit('.', 1)[0] |
| 99 | else: |
| 100 | package = import_name |
| 101 | module = imp.new_module(import_name) |
| 102 | module.__loader__ = self |
| 103 | module.__file__ = '<mock __file__>' |
| 104 | module.__package__ = package |
| 105 | module.attr = name |
| 106 | if import_name != name: |
| 107 | module.__path__ = ['<mock __path__>'] |
| 108 | self.modules[import_name] = module |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 109 | if import_name in module_code: |
| 110 | self.module_code[import_name] = module_code[import_name] |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 111 | |
| 112 | def __getitem__(self, name): |
| 113 | return self.modules[name] |
| 114 | |
| 115 | def find_module(self, fullname, path=None): |
| 116 | if fullname not in self.modules: |
| 117 | return None |
| 118 | else: |
| 119 | return self |
| 120 | |
| 121 | def load_module(self, fullname): |
| 122 | if fullname not in self.modules: |
| 123 | raise ImportError |
| 124 | else: |
| 125 | sys.modules[fullname] = self.modules[fullname] |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 126 | if fullname in self.module_code: |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame^] | 127 | try: |
| 128 | self.module_code[fullname]() |
| 129 | except Exception: |
| 130 | del sys.modules[fullname] |
| 131 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 132 | return self.modules[fullname] |
| 133 | |
| 134 | def __enter__(self): |
| 135 | self._uncache = uncache(*self.modules.keys()) |
| 136 | self._uncache.__enter__() |
| 137 | return self |
| 138 | |
| 139 | def __exit__(self, *exc_info): |
| 140 | self._uncache.__exit__(None, None, None) |