import time
import unittest
import unittest.mock
-import warnings
from test import support
from test.support import import_helper
self.assertEqual(zi.archive, TEMP_ZIP)
self.assertTrue(zi.is_package(TESTPACK))
- # PEP 302
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
-
- mod = zi.load_module(TESTPACK)
- self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
-
# PEP 451
spec = zi.find_spec('spam')
self.assertIsNotNone(spec)
spec.loader.exec_module(mod)
self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
+ sys.path.insert(0, TEMP_ZIP)
+
existing_pack_path = importlib.import_module(TESTPACK).__path__[0]
expected_path_path = os.path.join(TEMP_ZIP, TESTPACK)
self.assertEqual(existing_pack_path, expected_path_path)
self.assertEqual(zi.archive, TEMP_ZIP)
self.assertEqual(zi.prefix, packdir)
self.assertTrue(zi.is_package(TESTPACK2))
- # PEP 302
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- mod = zi.load_module(TESTPACK2)
- self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
# PEP 451
spec = zi.find_spec(TESTPACK2)
mod = importlib.util.module_from_spec(spec)
self.assertEqual(
spec.loader.get_filename(TESTMOD), load_mod.__file__)
+ sys.path.insert(0, TEMP_ZIP + os.sep + TESTPACK)
+
mod_path = TESTPACK2 + os.sep + TESTMOD
mod_name = module_path_to_dotted_name(mod_path)
mod = importlib.import_module(mod_name)
+
self.assertTrue(mod_name in sys.modules)
self.assertIsNone(zi.get_source(TESTPACK2))
self.assertIsNone(zi.get_source(mod_path))
z = zipimport.zipimporter(TESTMOD)
try:
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.assertRaises(TypeError, z.load_module, None)
self.assertRaises(TypeError, z.find_module, None)
self.assertRaises(TypeError, z.find_spec, None)
self.assertRaises(TypeError, z.exec_module, None)
error = zipimport.ZipImportError
self.assertIsNone(z.find_spec('abc'))
-
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.assertRaises(error, z.load_module, 'abc')
self.assertRaises(error, z.get_code, 'abc')
self.assertRaises(OSError, z.get_data, 'abc')
self.assertRaises(error, z.get_source, 'abc')
return mi
- # Load and return the module named by 'fullname'.
- def load_module(self, fullname):
- """load_module(fullname) -> module.
-
- Load the module specified by 'fullname'. 'fullname' must be the
- fully qualified (dotted) module name. It returns the imported
- module, or raises ZipImportError if it could not be imported.
-
- Deprecated since Python 3.10. Use exec_module() instead.
- """
- import warnings
- warnings._deprecated("zipimport.zipimporter.load_module",
- f"{warnings._DEPRECATED_MSG}; "
- "use zipimport.zipimporter.exec_module() instead",
- remove=(3, 15))
- code, ispackage, modpath = _get_module_code(self, fullname)
- mod = sys.modules.get(fullname)
- if mod is None or not isinstance(mod, _module_type):
- mod = _module_type(fullname)
- sys.modules[fullname] = mod
- mod.__loader__ = self
-
- try:
- if ispackage:
- # add __path__ to the module *before* the code gets
- # executed
- path = _get_module_path(self, fullname)
- fullpath = _bootstrap_external._path_join(self.archive, path)
- mod.__path__ = [fullpath]
-
- if not hasattr(mod, '__builtins__'):
- mod.__builtins__ = __builtins__
- _bootstrap_external._fix_up_module(mod.__dict__, fullname, modpath)
- exec(code, mod.__dict__)
- except:
- del sys.modules[fullname]
- raise
-
- try:
- mod = sys.modules[fullname]
- except KeyError:
- raise ImportError(f'Loaded module {fullname!r} not found in sys.modules')
- _bootstrap._verbose_message('import {} # loaded from Zip {}', fullname, modpath)
- return mod
-
-
def get_resource_reader(self, fullname):
"""Return the ResourceReader for a module in a zip file."""
from importlib.readers import ZipReader