Make sure sys.modules is the same object and has the same content when exiting the context as when entering. Similar to importlib.test.util.uncache, but doesn't require explicit names.
()
| 19 | |
| 20 | @contextlib.contextmanager |
| 21 | def sys_modules_context(): |
| 22 | """ |
| 23 | Make sure sys.modules is the same object and has the same content |
| 24 | when exiting the context as when entering. |
| 25 | |
| 26 | Similar to importlib.test.util.uncache, but doesn't require explicit |
| 27 | names. |
| 28 | """ |
| 29 | sys_modules_saved = sys.modules |
| 30 | sys_modules_copy = sys.modules.copy() |
| 31 | try: |
| 32 | yield |
| 33 | finally: |
| 34 | sys.modules = sys_modules_saved |
| 35 | sys.modules.clear() |
| 36 | sys.modules.update(sys_modules_copy) |
| 37 | |
| 38 | |
| 39 | @contextlib.contextmanager |
no test coverage detected
searching dependent graphs…