Skip to content

Commit d31e773

Browse files
bpo-35029: Replace the SyntaxWarning exception with a SyntaxError. (GH-9999)
If SyntaxWarning was raised as an exception, it will be replaced with a SyntaxError for better error reporting.
1 parent 2f73ed6 commit d31e773

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

Lib/test/test_grammar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import inspect
66
import unittest
77
import sys
8+
import warnings
89
# testing import *
910
from sys import *
1011

@@ -1099,6 +1100,14 @@ def testAssert2(self):
10991100
else:
11001101
self.fail("AssertionError not raised by 'assert False'")
11011102

1103+
with self.assertWarnsRegex(SyntaxWarning, 'assertion is always true'):
1104+
compile('assert(x, "msg")', '<testcase>', 'exec')
1105+
with warnings.catch_warnings():
1106+
warnings.filterwarnings('error', category=SyntaxWarning)
1107+
with self.assertRaisesRegex(SyntaxError, 'assertion is always true'):
1108+
compile('assert(x, "msg")', '<testcase>', 'exec')
1109+
compile('assert x, "msg"', '<testcase>', 'exec')
1110+
11021111

11031112
### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
11041113
# Tested below
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:exc:`SyntaxWarning` raised as an exception at code generation time will be
2+
now replaced with a :exc:`SyntaxError` for better error reporting.

Python/compile.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ static int compiler_addop(struct compiler *, int);
174174
static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
175175
static int compiler_addop_j(struct compiler *, int, basicblock *, int);
176176
static int compiler_error(struct compiler *, const char *);
177+
static int compiler_warn(struct compiler *, const char *);
177178
static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
178179

179180
static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
@@ -2971,7 +2972,6 @@ compiler_assert(struct compiler *c, stmt_ty s)
29712972
{
29722973
static PyObject *assertion_error = NULL;
29732974
basicblock *end;
2974-
PyObject* msg;
29752975

29762976
if (c->c_optimize)
29772977
return 1;
@@ -2981,18 +2981,13 @@ compiler_assert(struct compiler *c, stmt_ty s)
29812981
return 0;
29822982
}
29832983
if (s->v.Assert.test->kind == Tuple_kind &&
2984-
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2985-
msg = PyUnicode_FromString("assertion is always true, "
2986-
"perhaps remove parentheses?");
2987-
if (msg == NULL)
2988-
return 0;
2989-
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2990-
c->c_filename, c->u->u_lineno,
2991-
NULL, NULL) == -1) {
2992-
Py_DECREF(msg);
2984+
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
2985+
{
2986+
if (!compiler_warn(c, "assertion is always true, "
2987+
"perhaps remove parentheses?"))
2988+
{
29932989
return 0;
29942990
}
2995-
Py_DECREF(msg);
29962991
}
29972992
end = compiler_new_block(c);
29982993
if (end == NULL)
@@ -4793,6 +4788,31 @@ compiler_error(struct compiler *c, const char *errstr)
47934788
return 0;
47944789
}
47954790

4791+
/* Emits a SyntaxWarning and returns 1 on success.
4792+
If a SyntaxWarning raised as error, replaces it with a SyntaxError
4793+
and returns 0.
4794+
*/
4795+
static int
4796+
compiler_warn(struct compiler *c, const char *errstr)
4797+
{
4798+
PyObject *msg = PyUnicode_FromString(errstr);
4799+
if (msg == NULL) {
4800+
return 0;
4801+
}
4802+
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
4803+
c->u->u_lineno, NULL, NULL) < 0)
4804+
{
4805+
Py_DECREF(msg);
4806+
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4807+
PyErr_Clear();
4808+
return compiler_error(c, errstr);
4809+
}
4810+
return 0;
4811+
}
4812+
Py_DECREF(msg);
4813+
return 1;
4814+
}
4815+
47964816
static int
47974817
compiler_handle_subscr(struct compiler *c, const char *kind,
47984818
expr_context_ty ctx)

0 commit comments

Comments
 (0)