Skip to content

Commit 3610927

Browse files
Add more tests.
1 parent eb234eb commit 3610927

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,21 @@ def test_filter_syntax_warnings_by_module(self):
820820
filename)
821821
self.assertEqual(err.count(b': SyntaxWarning: '), 6)
822822

823+
def test_zipfile_run_filter_syntax_warnings_by_module(self):
824+
filename = support.findfile('test_import/data/syntax_warnings.py')
825+
with open(filename, 'rb') as f:
826+
source = f.read()
827+
with os_helper.temp_dir() as script_dir:
828+
zip_name, _ = make_zip_pkg(
829+
script_dir, 'test_zip', 'test_pkg', '__main__', source)
830+
rc, out, err = assert_python_ok(
831+
'-Werror',
832+
'-Walways:::__main__',
833+
'-Werror:::test_pkg.__main__',
834+
os.path.join(zip_name, 'test_pkg')
835+
)
836+
self.assertEqual(err.count(b': SyntaxWarning: '), 12)
837+
823838

824839
def tearDownModule():
825840
support.reap_children()

Lib/test/test_runpy.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
requires_subprocess,
2121
verbose,
2222
)
23+
from test import support
2324
from test.support.import_helper import forget, make_legacy_pyc, unload
2425
from test.support.os_helper import create_empty_file, temp_dir, FakePath
2526
from test.support.script_helper import make_script, make_zip_script
27+
from test.test_importlib.util import temporary_pycache_prefix
2628

2729

2830
import runpy
@@ -763,6 +765,47 @@ def test_encoding(self):
763765
result = run_path(filename)
764766
self.assertEqual(result['s'], "non-ASCII: h\xe9")
765767

768+
def test_run_module_filter_syntax_warnings_by_module(self):
769+
module_re = r'test\.test_import\.data\.syntax_warnings\z'
770+
with (temp_dir() as tmpdir,
771+
temporary_pycache_prefix(tmpdir),
772+
warnings.catch_warnings(record=True) as wlog):
773+
warnings.simplefilter('error')
774+
warnings.filterwarnings('always', module=module_re)
775+
warnings.filterwarnings('error', module='syntax_warnings')
776+
ns = run_module('test.test_import.data.syntax_warnings')
777+
self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10, 13, 14, 21])
778+
filename = ns['__file__']
779+
for wm in wlog:
780+
self.assertEqual(wm.filename, filename)
781+
self.assertIs(wm.category, SyntaxWarning)
782+
783+
def test_run_path_filter_syntax_warnings_by_module(self):
784+
filename = support.findfile('test_import/data/syntax_warnings.py')
785+
with warnings.catch_warnings(record=True) as wlog:
786+
warnings.simplefilter('error')
787+
warnings.filterwarnings('always', module=r'<run_path>\z')
788+
warnings.filterwarnings('error', module='test')
789+
warnings.filterwarnings('error', module='syntax_warnings')
790+
warnings.filterwarnings('error',
791+
module=r'test\.test_import\.data\.syntax_warnings')
792+
run_path(filename)
793+
self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10, 13, 14, 21])
794+
for wm in wlog:
795+
self.assertEqual(wm.filename, filename)
796+
self.assertIs(wm.category, SyntaxWarning)
797+
798+
with warnings.catch_warnings(record=True) as wlog:
799+
warnings.simplefilter('error')
800+
warnings.filterwarnings('always', module=r'package\.script\z')
801+
warnings.filterwarnings('error', module='<run_path>')
802+
warnings.filterwarnings('error', module='test')
803+
warnings.filterwarnings('error', module='syntax_warnings')
804+
warnings.filterwarnings('error',
805+
module=r'test\.test_import\.data\.syntax_warnings')
806+
run_path(filename, run_name='package.script')
807+
self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10, 13, 14, 21])
808+
766809

767810
@force_not_colorized_test_class
768811
class TestExit(unittest.TestCase):

Lib/test/test_zipimport_support.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import inspect
1414
import linecache
1515
import unittest
16+
import warnings
17+
from test import support
1618
from test.support import os_helper
1719
from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
1820
make_script, make_zip_script)
21+
from test.support import import_helper
1922

2023
verbose = test.support.verbose
2124

@@ -236,6 +239,26 @@ def f():
236239
# bdb/pdb applies normcase to its filename before displaying
237240
self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
238241

242+
def test_import_filter_syntax_warnings_by_module(self):
243+
filename = support.findfile('test_import/data/syntax_warnings.py')
244+
with (os_helper.temp_dir() as tmpdir,
245+
import_helper.DirsOnSysPath()):
246+
zip_name, _ = make_zip_script(tmpdir, "test_zip",
247+
filename, 'test_pkg/test_mod.py')
248+
sys.path.insert(0, zip_name)
249+
import_helper.unload('test_pkg.test_mod')
250+
with warnings.catch_warnings(record=True) as wlog:
251+
warnings.simplefilter('error')
252+
warnings.filterwarnings('always', module=r'test_pkg\.test_mod\z')
253+
warnings.filterwarnings('error', module='test_mod')
254+
import test_pkg.test_mod
255+
self.assertEqual(sorted(wm.lineno for wm in wlog),
256+
sorted([4, 7, 10, 13, 14, 21]*2))
257+
filename = test_pkg.test_mod.__file__
258+
for wm in wlog:
259+
self.assertEqual(wm.filename, filename)
260+
self.assertIs(wm.category, SyntaxWarning)
261+
239262

240263
def tearDownModule():
241264
test.support.reap_children()

0 commit comments

Comments
 (0)