diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-02-19 06:30:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-19 06:30:15 (GMT) |
commit | e7a4bb554edb72fc6619d23241d59162d06f249a (patch) | |
tree | ba74116b687307c63725d6bed28fce0e46cade90 /Lib/test/support | |
parent | ee0f927bd8dba805a04963dbec1ad49fe830b842 (diff) | |
download | cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.zip cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.tar.gz cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.tar.bz2 |
bpo-35798: Add test.support.check_syntax_warning(). (#11895)
It checks that a SyntaxWarning is raised when compile specified
statement, that it is raised only once, that it is converted to
a SyntaxError when raised as exception, and that both warning and
exception objects have corresponding attributes.
Diffstat (limited to 'Lib/test/support')
-rw-r--r-- | Lib/test/support/__init__.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 436f648..9b75a21 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -86,6 +86,7 @@ __all__ = [ # unittest "is_resource_enabled", "requires", "requires_freebsd_version", "requires_linux_version", "requires_mac_ver", "check_syntax_error", + "check_syntax_warning", "TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset", "transient_internet", "BasicTestRunner", "run_unittest", "run_doctest", "skip_unless_symlink", "requires_gzip", "requires_bz2", "requires_lzma", @@ -1113,6 +1114,7 @@ def make_bad_fd(): file.close() unlink(TESTFN) + def check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None): with testcase.assertRaisesRegex(SyntaxError, errtext) as cm: compile(statement, '<test string>', 'exec') @@ -1124,6 +1126,33 @@ def check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=N if offset is not None: testcase.assertEqual(err.offset, offset) +def check_syntax_warning(testcase, statement, errtext='', *, lineno=1, offset=None): + # Test also that a warning is emitted only once. + with warnings.catch_warnings(record=True) as warns: + warnings.simplefilter('always', SyntaxWarning) + compile(statement, '<testcase>', 'exec') + testcase.assertEqual(len(warns), 1, warns) + + warn, = warns + testcase.assertTrue(issubclass(warn.category, SyntaxWarning), warn.category) + if errtext: + testcase.assertRegex(str(warn.message), errtext) + testcase.assertEqual(warn.filename, '<testcase>') + testcase.assertIsNotNone(warn.lineno) + if lineno is not None: + testcase.assertEqual(warn.lineno, lineno) + + # SyntaxWarning should be converted to SyntaxError when raised, + # since the latter contains more information and provides better + # error report. + with warnings.catch_warnings(record=True) as warns: + warnings.simplefilter('error', SyntaxWarning) + check_syntax_error(testcase, statement, errtext, + lineno=lineno, offset=offset) + # No warnings are leaked when a SyntaxError is raised. + testcase.assertEqual(warns, []) + + def open_urlresource(url, *args, **kw): import urllib.request, urllib.parse |