Uncache a module from sys.modules. A basic sanity check is performed to prevent uncaching modules that either cannot/shouldn't be uncached.
(*names)
| 145 | |
| 146 | @contextlib.contextmanager |
| 147 | def uncache(*names): |
| 148 | """Uncache a module from sys.modules. |
| 149 | |
| 150 | A basic sanity check is performed to prevent uncaching modules that either |
| 151 | cannot/shouldn't be uncached. |
| 152 | |
| 153 | """ |
| 154 | for name in names: |
| 155 | if name in ('sys', 'marshal'): |
| 156 | raise ValueError("cannot uncache {}".format(name)) |
| 157 | try: |
| 158 | del sys.modules[name] |
| 159 | except KeyError: |
| 160 | pass |
| 161 | try: |
| 162 | yield |
| 163 | finally: |
| 164 | for name in names: |
| 165 | try: |
| 166 | del sys.modules[name] |
| 167 | except KeyError: |
| 168 | pass |
| 169 | |
| 170 | |
| 171 | @contextlib.contextmanager |