|
20 | 20 | requires_subprocess, |
21 | 21 | verbose, |
22 | 22 | ) |
| 23 | +from test import support |
23 | 24 | from test.support.import_helper import forget, make_legacy_pyc, unload |
24 | 25 | from test.support.os_helper import create_empty_file, temp_dir, FakePath |
25 | 26 | from test.support.script_helper import make_script, make_zip_script |
| 27 | +from test.test_importlib.util import temporary_pycache_prefix |
26 | 28 |
|
27 | 29 |
|
28 | 30 | import runpy |
@@ -763,6 +765,47 @@ def test_encoding(self): |
763 | 765 | result = run_path(filename) |
764 | 766 | self.assertEqual(result['s'], "non-ASCII: h\xe9") |
765 | 767 |
|
| 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 | + |
766 | 809 |
|
767 | 810 | @force_not_colorized_test_class |
768 | 811 | class TestExit(unittest.TestCase): |
|
0 commit comments