A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2012-April/118958.html below:

[Python-Dev] isolating import state during tests

[Python-Dev] isolating import state during tests [Python-Dev] isolating import state during testsEric Snow ericsnowcurrently at gmail.com
Sun Apr 22 03:02:17 CEST 2012
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
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4