Skip to content

gh-133656: Remove deprecated zipimport.zipimporter.load_module #133662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/deprecations/pending-removal-in-3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ Pending removal in Python 3.15

* :mod:`zipimport`:

* :meth:`~zipimport.zipimporter.load_module` has been deprecated since
* :meth:`!zipimport.zipimporter.load_module` has been deprecated since
Python 3.10. Use :meth:`~zipimport.zipimporter.exec_module` instead.
(Contributed by Jiahao Li in :gh:`125746`.)
(:gh:`125746`.)
11 changes: 0 additions & 11 deletions Doc/library/zipimport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,6 @@ zipimporter Objects
:exc:`ZipImportError` if the module couldn't be found.


.. method:: load_module(fullname)

Load the module specified by *fullname*. *fullname* must be the fully
qualified (dotted) module name. Returns the imported module on success,
raises :exc:`ZipImportError` on failure.

.. deprecated-removed:: 3.10 3.15

Use :meth:`exec_module` instead.


.. method:: invalidate_caches()

Clear out the internal cache of information about files found within
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ wave
(Contributed by Bénédikt Tran in :gh:`133873`.)


zipimport
---------

* Remove deprecated :meth:`!zipimport.zipimporter.load_module`.
Use :meth:`zipimport.zipimporter.exec_module` instead.
(Contributed by Jiahao Li in :gh:`133656`.)


Porting to Python 3.15
======================

Expand Down
24 changes: 4 additions & 20 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
import unittest
import unittest.mock
import warnings

from test import support
from test.support import import_helper
Expand Down Expand Up @@ -552,17 +551,12 @@ def testZipImporterMethods(self):
"spam" + pyc_ext: test_pyc}
self.makeZip(files, file_comment=b"spam")

sys.path.insert(0, TEMP_ZIP)

zi = zipimport.zipimporter(TEMP_ZIP)
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)
Expand Down Expand Up @@ -671,15 +665,12 @@ def testZipImporterMethodsInSubDirectory(self):
packdir2 + TESTMOD + pyc_ext: test_pyc}
self.makeZip(files, file_comment=b"eggs")

sys.path.insert(0, TEMP_ZIP + os.sep + TESTPACK)

zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
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)
Expand Down Expand Up @@ -1069,9 +1060,6 @@ def _testBogusZipFile(self):
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)
Expand All @@ -1082,10 +1070,6 @@ def _testBogusZipFile(self):

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')
Expand Down
46 changes: 0 additions & 46 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,52 +210,6 @@ def is_package(self, fullname):
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
Expand Down
2 changes: 1 addition & 1 deletion Misc/NEWS.d/3.14.0a6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ Patch by Semyon Moroz.
.. nonce: wDLTay
.. section: Library

Delay deprecated :meth:`zipimport.zipimporter.load_module` removal time to
Delay deprecated :meth:`!zipimport.zipimporter.load_module` removal time to
3.15. Use :meth:`zipimport.zipimporter.exec_module` instead.

..
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove deprecated :meth:`!zipimport.zipimporter.load_module`. Use
:meth:`zipimport.zipimporter.exec_module` instead.
Loading