@contextlib.contextmanager
def replace_modules(new_module_classes):
 old_registry = _registry[:]
 _registry[:] = []
 for cls in new_module_classes:
  register(cls)
 try:
  (yield)
 finally:
  _registry[:] = old_registry

@ contextlib . contextmanager def replace_modules ( new_module_classes ) : old_registry = _registry [ : ] _registry [ : ] = [ ] for cls in new_module_classes : register ( cls ) try : ( yield ) finally : _registry [ : ] = old_registry
context manager to temporarily replace all modules with something else .