isolating import state during tests

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

isolating import state during tests

Eric Snow-2
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
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%2B1324100855712-1801473%40n6.nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: isolating import state during tests

Brett Cannon-2


On Sat, Apr 21, 2012 at 21:02, Eric Snow <[hidden email]> wrote:
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

That practically all done for you with a combination of importlib.test.util.uncache and importlib.test.util.import_state. 

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%2B1324100855712-1801473%40n6.nabble.com