Skip to content
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
2 changes: 1 addition & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def test_zipfile_run_filter_syntax_warnings_by_module(self):
'-Werror:::test_pkg.__main__',
os.path.join(zip_name, 'test_pkg')
)
self.assertEqual(err.count(b': SyntaxWarning: '), 12)
self.assertEqual(err.count(b': SyntaxWarning: '), 6)


def tearDownModule():
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
import unittest
import unittest.mock
import warnings

from test import support
from test.support import import_helper
Expand Down Expand Up @@ -229,6 +230,23 @@ def testPy(self):
files = {TESTMOD + ".py": test_src}
self.doTest(".py", files, TESTMOD)

def test_syntax_warning(self):
files = {TESTMOD + ".py": "x = 1 is 1\n"}
self.makeZip(files)
zi = zipimport.zipimporter(TEMP_ZIP)

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always", SyntaxWarning)
spec = zi.find_spec(TESTMOD)
self.assertIsNotNone(spec)
self.assertEqual(caught, [])

mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

self.assertEqual(len(caught), 1)
self.assertIsInstance(caught[0].message, SyntaxWarning)

def testPyc(self):
files = {TESTMOD + pyc_ext: test_pyc}
self.doTest(pyc_ext, files, TESTMOD)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_zipimport_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_import_filter_syntax_warnings_by_module(self):
warnings.filterwarnings('error', module='test_mod')
import test_pkg.test_mod
self.assertEqual(sorted(wm.lineno for wm in wlog),
sorted([4, 7, 10, 13, 14, 21]*2))
[4, 7, 10, 13, 14, 21])
filename = test_pkg.test_mod.__file__
for wm in wlog:
self.assertEqual(wm.filename, filename)
Expand Down
11 changes: 7 additions & 4 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def get_filename(self, fullname):
"""
# Deciding the filename requires working out where the code
# would come from if the module was actually loaded
code, ispackage, modpath = _get_module_code(self, fullname)
_, _, modpath = _get_module_code(
self, fullname, compile_source=False)
return modpath


Expand Down Expand Up @@ -793,9 +794,9 @@ def _get_pyc_source(self, path):
return _get_data(self.archive, toc_entry)


# Get the code object associated with the module specified by
# 'fullname'.
def _get_module_code(self, fullname):
# Get the code object associated with the module specified by 'fullname'.
# If compile_source is false, return None for source code without compiling it.
def _get_module_code(self, fullname, *, compile_source=True):
path = _get_module_path(self, fullname)
import_error = None
for suffix, isbytecode, ispackage in _zip_searchorder:
Expand All @@ -815,6 +816,8 @@ def _get_module_code(self, fullname):
except ImportError as exc:
import_error = exc
else:
if not compile_source:
return None, ispackage, modpath
Comment on lines +819 to +820

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed with the suggestion above

Suggested change
if not compile_source:
return None, ispackage, modpath

code = _compile_source(modpath, data, fullname)
if code is None:
# bad magic number or non-matching mtime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix duplicate :exc:`SyntaxWarning` messages when importing source modules
from ZIP archives.
Comment on lines +1 to +2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some prose you could use here:

zipimporter.get_filename() and find_spec() no longer compile source modules; a SyntaxError in a zipped source module is now raised when the module is executed, matching the behavior of filesystem-based loaders.

Loading