Skip to content

Commit 0d5505d

Browse files
committed
gh-141276: Avoid compiling zipimport source in get_filename
1 parent b4662e8 commit 0d5505d

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lib/test/test_zipimport.py

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

1314
from test import support
1415
from test.support import import_helper
@@ -229,6 +230,23 @@ def testPy(self):
229230
files = {TESTMOD + ".py": test_src}
230231
self.doTest(".py", files, TESTMOD)
231232

233+
def test_syntax_warning(self):
234+
files = {TESTMOD + ".py": "x = 1 is 1\n"}
235+
self.makeZip(files)
236+
zi = zipimport.zipimporter(TEMP_ZIP)
237+
238+
with warnings.catch_warnings(record=True) as caught:
239+
warnings.simplefilter("always", SyntaxWarning)
240+
spec = zi.find_spec(TESTMOD)
241+
self.assertIsNotNone(spec)
242+
self.assertEqual(caught, [])
243+
244+
mod = importlib.util.module_from_spec(spec)
245+
spec.loader.exec_module(mod)
246+
247+
self.assertEqual(len(caught), 1)
248+
self.assertIsInstance(caught[0].message, SyntaxWarning)
249+
232250
def testPyc(self):
233251
files = {TESTMOD + pyc_ext: test_pyc}
234252
self.doTest(pyc_ext, files, TESTMOD)

Lib/zipimport.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def get_filename(self, fullname):
163163
"""
164164
# Deciding the filename requires working out where the code
165165
# would come from if the module was actually loaded
166-
code, ispackage, modpath = _get_module_code(self, fullname)
166+
_, _, modpath = _get_module_code(
167+
self, fullname, compile_source=False)
167168
return modpath
168169

169170

@@ -793,9 +794,9 @@ def _get_pyc_source(self, path):
793794
return _get_data(self.archive, toc_entry)
794795

795796

796-
# Get the code object associated with the module specified by
797-
# 'fullname'.
798-
def _get_module_code(self, fullname):
797+
# Get the code object associated with the module specified by 'fullname'.
798+
# If compile_source is false, return None for source code without compiling it.
799+
def _get_module_code(self, fullname, *, compile_source=True):
799800
path = _get_module_path(self, fullname)
800801
import_error = None
801802
for suffix, isbytecode, ispackage in _zip_searchorder:
@@ -815,6 +816,8 @@ def _get_module_code(self, fullname):
815816
except ImportError as exc:
816817
import_error = exc
817818
else:
819+
if not compile_source:
820+
return None, ispackage, modpath
818821
code = _compile_source(modpath, data, fullname)
819822
if code is None:
820823
# bad magic number or non-matching mtime
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix duplicate :exc:`SyntaxWarning` messages when importing source modules
2+
from ZIP archives.

0 commit comments

Comments
 (0)