summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-10-21 07:09:39 (GMT)
committerGitHub <noreply@github.com>2018-10-21 07:09:39 (GMT)
commitd31e7730cd5d74efbd7320751dacd51d09cc415d (patch)
tree2ec0ee3852041197d9dda25b4e9eb6a4f5c2c9ca /Python/compile.c
parent2f73ed69130cdf63b773275f430c9abdab0757ad (diff)
downloadcpython-d31e7730cd5d74efbd7320751dacd51d09cc415d.zip
cpython-d31e7730cd5d74efbd7320751dacd51d09cc415d.tar.gz
cpython-d31e7730cd5d74efbd7320751dacd51d09cc415d.tar.bz2
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.
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 78b7baf..11958d3 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -174,6 +174,7 @@ static int compiler_addop(struct compiler *, int);
static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
static int compiler_addop_j(struct compiler *, int, basicblock *, int);
static int compiler_error(struct compiler *, const char *);
+static int compiler_warn(struct compiler *, const char *);
static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
@@ -2971,7 +2972,6 @@ compiler_assert(struct compiler *c, stmt_ty s)
{
static PyObject *assertion_error = NULL;
basicblock *end;
- PyObject* msg;
if (c->c_optimize)
return 1;
@@ -2981,18 +2981,13 @@ compiler_assert(struct compiler *c, stmt_ty s)
return 0;
}
if (s->v.Assert.test->kind == Tuple_kind &&
- asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
- msg = PyUnicode_FromString("assertion is always true, "
- "perhaps remove parentheses?");
- if (msg == NULL)
- return 0;
- if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
- c->c_filename, c->u->u_lineno,
- NULL, NULL) == -1) {
- Py_DECREF(msg);
+ asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
+ {
+ if (!compiler_warn(c, "assertion is always true, "
+ "perhaps remove parentheses?"))
+ {
return 0;
}
- Py_DECREF(msg);
}
end = compiler_new_block(c);
if (end == NULL)
@@ -4793,6 +4788,31 @@ compiler_error(struct compiler *c, const char *errstr)
return 0;
}
+/* Emits a SyntaxWarning and returns 1 on success.
+ If a SyntaxWarning raised as error, replaces it with a SyntaxError
+ and returns 0.
+*/
+static int
+compiler_warn(struct compiler *c, const char *errstr)
+{
+ PyObject *msg = PyUnicode_FromString(errstr);
+ if (msg == NULL) {
+ return 0;
+ }
+ if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
+ c->u->u_lineno, NULL, NULL) < 0)
+ {
+ Py_DECREF(msg);
+ if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
+ PyErr_Clear();
+ return compiler_error(c, errstr);
+ }
+ return 0;
+ }
+ Py_DECREF(msg);
+ return 1;
+}
+
static int
compiler_handle_subscr(struct compiler *c, const char *kind,
expr_context_ty ctx)