|
15 | 15 | import os |
16 | 16 | import py_compile |
17 | 17 | import random |
| 18 | +import re |
18 | 19 | import shutil |
19 | 20 | import stat |
20 | 21 | import subprocess |
|
23 | 24 | import threading |
24 | 25 | import time |
25 | 26 | import types |
| 27 | +import warnings |
26 | 28 | import unittest |
27 | 29 | from unittest import mock |
28 | 30 | import _imp |
|
51 | 53 | TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE) |
52 | 54 | from test.support import script_helper |
53 | 55 | from test.support import threading_helper |
54 | | -from test.test_importlib.util import uncache |
| 56 | +from test.test_importlib.util import uncache, temporary_pycache_prefix |
55 | 57 | from types import ModuleType |
56 | 58 | try: |
57 | 59 | import _testsinglephase |
@@ -412,7 +414,6 @@ def test_from_import_missing_attr_path_is_canonical(self): |
412 | 414 | self.assertIsNotNone(cm.exception) |
413 | 415 |
|
414 | 416 | def test_from_import_star_invalid_type(self): |
415 | | - import re |
416 | 417 | with ready_to_import() as (name, path): |
417 | 418 | with open(path, 'w', encoding='utf-8') as f: |
418 | 419 | f.write("__all__ = [b'invalid_type']") |
@@ -1250,6 +1251,35 @@ class Spec2: |
1250 | 1251 | origin = "a\x00b" |
1251 | 1252 | _imp.create_dynamic(Spec2()) |
1252 | 1253 |
|
| 1254 | + def test_filter_syntax_warnings_by_module(self): |
| 1255 | + module_re = r'test\.test_import\.data\.syntax_warnings\z' |
| 1256 | + unload('test.test_import.data.syntax_warnings') |
| 1257 | + with (os_helper.temp_dir() as tmpdir, |
| 1258 | + temporary_pycache_prefix(tmpdir), |
| 1259 | + warnings.catch_warnings(record=True) as wlog): |
| 1260 | + warnings.simplefilter('error') |
| 1261 | + warnings.filterwarnings('always', module=module_re) |
| 1262 | + import test.test_import.data.syntax_warnings |
| 1263 | + self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10, 13, 14, 21]) |
| 1264 | + filename = test.test_import.data.syntax_warnings.__file__ |
| 1265 | + for wm in wlog: |
| 1266 | + self.assertEqual(wm.filename, filename) |
| 1267 | + self.assertIs(wm.category, SyntaxWarning) |
| 1268 | + |
| 1269 | + module_re = r'syntax_warnings\z' |
| 1270 | + unload('test.test_import.data.syntax_warnings') |
| 1271 | + with (os_helper.temp_dir() as tmpdir, |
| 1272 | + temporary_pycache_prefix(tmpdir), |
| 1273 | + warnings.catch_warnings(record=True) as wlog): |
| 1274 | + warnings.simplefilter('error') |
| 1275 | + warnings.filterwarnings('always', module=module_re) |
| 1276 | + import test.test_import.data.syntax_warnings |
| 1277 | + self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10, 13, 14, 21]) |
| 1278 | + filename = test.test_import.data.syntax_warnings.__file__ |
| 1279 | + for wm in wlog: |
| 1280 | + self.assertEqual(wm.filename, filename) |
| 1281 | + self.assertIs(wm.category, SyntaxWarning) |
| 1282 | + |
1253 | 1283 |
|
1254 | 1284 | @skip_if_dont_write_bytecode |
1255 | 1285 | class FilePermissionTests(unittest.TestCase): |
|
0 commit comments