Skip to content

Commit 5c6937a

Browse files
authored
gh-133656: Remove deprecated zipimport.zipimporter.load_module (GH-133662)
Remove deprecated `zipimport.zipimporter.load_module`.
1 parent e05182f commit 5c6937a

File tree

7 files changed

+18
-80
lines changed

7 files changed

+18
-80
lines changed

Doc/deprecations/pending-removal-in-3.15.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ Pending removal in Python 3.15
107107

108108
* :mod:`zipimport`:
109109

110-
* :meth:`~zipimport.zipimporter.load_module` has been deprecated since
110+
* :meth:`!zipimport.zipimporter.load_module` has been deprecated since
111111
Python 3.10. Use :meth:`~zipimport.zipimporter.exec_module` instead.
112-
(Contributed by Jiahao Li in :gh:`125746`.)
112+
(:gh:`125746`.)

Doc/library/zipimport.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,6 @@ zipimporter Objects
142142
:exc:`ZipImportError` if the module couldn't be found.
143143

144144

145-
.. method:: load_module(fullname)
146-
147-
Load the module specified by *fullname*. *fullname* must be the fully
148-
qualified (dotted) module name. Returns the imported module on success,
149-
raises :exc:`ZipImportError` on failure.
150-
151-
.. deprecated-removed:: 3.10 3.15
152-
153-
Use :meth:`exec_module` instead.
154-
155-
156145
.. method:: invalidate_caches()
157146

158147
Clear out the internal cache of information about files found within

Doc/whatsnew/3.15.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ wave
611611
(Contributed by Bénédikt Tran in :gh:`133873`.)
612612

613613

614+
zipimport
615+
---------
616+
617+
* Remove deprecated :meth:`!zipimport.zipimporter.load_module`.
618+
Use :meth:`zipimport.zipimporter.exec_module` instead.
619+
(Contributed by Jiahao Li in :gh:`133656`.)
620+
621+
614622
Porting to Python 3.15
615623
======================
616624

Lib/test/test_zipimport.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import time
1010
import unittest
1111
import unittest.mock
12-
import warnings
1312

1413
from test import support
1514
from test.support import import_helper
@@ -556,13 +555,6 @@ def testZipImporterMethods(self):
556555
self.assertEqual(zi.archive, TEMP_ZIP)
557556
self.assertTrue(zi.is_package(TESTPACK))
558557

559-
# PEP 302
560-
with warnings.catch_warnings():
561-
warnings.simplefilter("ignore", DeprecationWarning)
562-
563-
mod = zi.load_module(TESTPACK)
564-
self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
565-
566558
# PEP 451
567559
spec = zi.find_spec('spam')
568560
self.assertIsNotNone(spec)
@@ -577,6 +569,8 @@ def testZipImporterMethods(self):
577569
spec.loader.exec_module(mod)
578570
self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
579571

572+
sys.path.insert(0, TEMP_ZIP)
573+
580574
existing_pack_path = importlib.import_module(TESTPACK).__path__[0]
581575
expected_path_path = os.path.join(TEMP_ZIP, TESTPACK)
582576
self.assertEqual(existing_pack_path, expected_path_path)
@@ -675,11 +669,6 @@ def testZipImporterMethodsInSubDirectory(self):
675669
self.assertEqual(zi.archive, TEMP_ZIP)
676670
self.assertEqual(zi.prefix, packdir)
677671
self.assertTrue(zi.is_package(TESTPACK2))
678-
# PEP 302
679-
with warnings.catch_warnings():
680-
warnings.simplefilter("ignore", DeprecationWarning)
681-
mod = zi.load_module(TESTPACK2)
682-
self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
683672
# PEP 451
684673
spec = zi.find_spec(TESTPACK2)
685674
mod = importlib.util.module_from_spec(spec)
@@ -702,9 +691,12 @@ def testZipImporterMethodsInSubDirectory(self):
702691
self.assertEqual(
703692
spec.loader.get_filename(TESTMOD), load_mod.__file__)
704693

694+
sys.path.insert(0, TEMP_ZIP + os.sep + TESTPACK)
695+
705696
mod_path = TESTPACK2 + os.sep + TESTMOD
706697
mod_name = module_path_to_dotted_name(mod_path)
707698
mod = importlib.import_module(mod_name)
699+
708700
self.assertTrue(mod_name in sys.modules)
709701
self.assertIsNone(zi.get_source(TESTPACK2))
710702
self.assertIsNone(zi.get_source(mod_path))
@@ -1069,9 +1061,6 @@ def _testBogusZipFile(self):
10691061
z = zipimport.zipimporter(TESTMOD)
10701062

10711063
try:
1072-
with warnings.catch_warnings():
1073-
warnings.simplefilter("ignore", DeprecationWarning)
1074-
self.assertRaises(TypeError, z.load_module, None)
10751064
self.assertRaises(TypeError, z.find_module, None)
10761065
self.assertRaises(TypeError, z.find_spec, None)
10771066
self.assertRaises(TypeError, z.exec_module, None)
@@ -1082,10 +1071,6 @@ def _testBogusZipFile(self):
10821071

10831072
error = zipimport.ZipImportError
10841073
self.assertIsNone(z.find_spec('abc'))
1085-
1086-
with warnings.catch_warnings():
1087-
warnings.simplefilter("ignore", DeprecationWarning)
1088-
self.assertRaises(error, z.load_module, 'abc')
10891074
self.assertRaises(error, z.get_code, 'abc')
10901075
self.assertRaises(OSError, z.get_data, 'abc')
10911076
self.assertRaises(error, z.get_source, 'abc')

Lib/zipimport.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -210,52 +210,6 @@ def is_package(self, fullname):
210210
return mi
211211

212212

213-
# Load and return the module named by 'fullname'.
214-
def load_module(self, fullname):
215-
"""load_module(fullname) -> module.
216-
217-
Load the module specified by 'fullname'. 'fullname' must be the
218-
fully qualified (dotted) module name. It returns the imported
219-
module, or raises ZipImportError if it could not be imported.
220-
221-
Deprecated since Python 3.10. Use exec_module() instead.
222-
"""
223-
import warnings
224-
warnings._deprecated("zipimport.zipimporter.load_module",
225-
f"{warnings._DEPRECATED_MSG}; "
226-
"use zipimport.zipimporter.exec_module() instead",
227-
remove=(3, 15))
228-
code, ispackage, modpath = _get_module_code(self, fullname)
229-
mod = sys.modules.get(fullname)
230-
if mod is None or not isinstance(mod, _module_type):
231-
mod = _module_type(fullname)
232-
sys.modules[fullname] = mod
233-
mod.__loader__ = self
234-
235-
try:
236-
if ispackage:
237-
# add __path__ to the module *before* the code gets
238-
# executed
239-
path = _get_module_path(self, fullname)
240-
fullpath = _bootstrap_external._path_join(self.archive, path)
241-
mod.__path__ = [fullpath]
242-
243-
if not hasattr(mod, '__builtins__'):
244-
mod.__builtins__ = __builtins__
245-
_bootstrap_external._fix_up_module(mod.__dict__, fullname, modpath)
246-
exec(code, mod.__dict__)
247-
except:
248-
del sys.modules[fullname]
249-
raise
250-
251-
try:
252-
mod = sys.modules[fullname]
253-
except KeyError:
254-
raise ImportError(f'Loaded module {fullname!r} not found in sys.modules')
255-
_bootstrap._verbose_message('import {} # loaded from Zip {}', fullname, modpath)
256-
return mod
257-
258-
259213
def get_resource_reader(self, fullname):
260214
"""Return the ResourceReader for a module in a zip file."""
261215
from importlib.readers import ZipReader

Misc/NEWS.d/3.14.0a6.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ Patch by Semyon Moroz.
758758
.. nonce: wDLTay
759759
.. section: Library
760760
761-
Delay deprecated :meth:`zipimport.zipimporter.load_module` removal time to
761+
Delay deprecated :meth:`!zipimport.zipimporter.load_module` removal time to
762762
3.15. Use :meth:`zipimport.zipimporter.exec_module` instead.
763763

764764
..
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove deprecated :meth:`!zipimport.zipimporter.load_module`. Use
2+
:meth:`zipimport.zipimporter.exec_module` instead.

0 commit comments

Comments
 (0)