[Python-Dev] isolating import state during tests (original) (raw)
Eric Snow ericsnowcurrently at gmail.com
Sun Apr 22 03:02:17 CEST 2012
- Previous message: [Python-Dev] Expose dictproxy through collections rather than the types module?
- Next message: [Python-Dev] isolating import state during tests
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
It looks like the test suite accommodates a stable import state to some extent, but would it be worth having a PEP-405-esque context manager to help with this? For example, something along these lines:
class ImportState: # sys.modules is part of the interpreter state, so # repopulate (don't replace) def enter(self): self.path = sys.path[:] self.modules = sys.modules.copy() self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] self.path_importer_cache = sys.path_importer_cache.copy()
sys.path = site.getsitepackages()
sys.modules.clear()
sys.meta_path = []
sys.path_hooks = []
sys.path_importer_cache = {}
def __exit__(self, *args, **kwargs):
sys.path = self.path
sys.modules.clear()
sys.modules.update(self.modules)
sys.meta_path = self.meta_path
sys.path_hooks = self.path_hooks
sys.path_importer_cache = self.path_importer_cache
in some unit test:
with ImportState(): ... # tests
-eric
- Previous message: [Python-Dev] Expose dictproxy through collections rather than the types module?
- Next message: [Python-Dev] isolating import state during tests
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]