import sys import unittest import marshal import imp import struct from textwrap import dedent from fs.expose.importhook import FSImportHook from fs.tempfs import TempFS from fs.zipfs import ZipFS from six import b class TestFSImportHook(unittest.TestCase): def setUp(self): pass def tearDown(self): for mph in list(sys.meta_path): if isinstance(mph,FSImportHook): sys.meta_path.remove(mph) for ph in list(sys.path_hooks): if issubclass(ph,FSImportHook): sys.path_hooks.remove(mph) for (k,v) in sys.modules.items(): if k.startswith("fsih_"): del sys.modules[k] elif hasattr(v,"__loader__"): if isinstance(v.__loader__,FSImportHook): del sys.modules[k] sys.path_importer_cache.clear() def _init_modules(self,fs): fs.setcontents("fsih_hello.py",b(dedent(""" message = 'hello world!' """))) fs.makedir("fsih_pkg") fs.setcontents("fsih_pkg/__init__.py",b(dedent(""" a = 42 """))) fs.setcontents("fsih_pkg/sub1.py",b(dedent(""" import fsih_pkg from fsih_hello import message a = fsih_pkg.a """))) fs.setcontents("fsih_pkg/sub2.pyc",self._getpyc(b(dedent(""" import fsih_pkg from fsih_hello import message a = fsih_pkg.a * 2 """)))) def _getpyc(self,src): """Get the .pyc contents to match th given .py source code.""" code = imp.get_magic() + struct.pack("