summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2024-09-16 14:05:00 (GMT)
committerGitHub <noreply@github.com>2024-09-16 14:05:00 (GMT)
commitaba42c0b547e6395c9c268cf98a298d0494cb9df (patch)
treecb257c39525b590b19b5c5e86a45d6bb75aa729b /Python/errors.c
parent9aa1f60e2dedd8a67c42fb4f4c56858b6ba5b947 (diff)
downloadcpython-aba42c0b547e6395c9c268cf98a298d0494cb9df.zip
cpython-aba42c0b547e6395c9c268cf98a298d0494cb9df.tar.gz
cpython-aba42c0b547e6395c9c268cf98a298d0494cb9df.tar.bz2
gh-123969: refactor _PyErr_RaiseSyntaxError and _PyErr_EmitSyntaxWarning out of compiler (#123972)
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index ad6b7db..29249ac 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1850,6 +1850,52 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
Py_XDECREF(fileobj);
}
+/* Raises a SyntaxError.
+ * If something goes wrong, a different exception may be raised.
+ */
+void
+_PyErr_RaiseSyntaxError(PyObject *msg, PyObject *filename, int lineno, int col_offset,
+ int end_lineno, int end_col_offset)
+{
+ PyObject *text = PyErr_ProgramTextObject(filename, lineno);
+ if (text == NULL) {
+ text = Py_NewRef(Py_None);
+ }
+ PyObject *args = Py_BuildValue("O(OiiOii)", msg, filename,
+ lineno, col_offset, text,
+ end_lineno, end_col_offset);
+ if (args == NULL) {
+ goto exit;
+ }
+ PyErr_SetObject(PyExc_SyntaxError, args);
+ exit:
+ Py_DECREF(text);
+ Py_XDECREF(args);
+}
+
+/* Emits a SyntaxWarning and returns 0 on success.
+ If a SyntaxWarning is raised as error, replaces it with a SyntaxError
+ and returns -1.
+*/
+int
+_PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset,
+ int end_lineno, int end_col_offset)
+{
+ if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, filename,
+ lineno, NULL, NULL) < 0)
+ {
+ if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
+ /* Replace the SyntaxWarning exception with a SyntaxError
+ to get a more accurate error report */
+ PyErr_Clear();
+ _PyErr_RaiseSyntaxError(msg, filename, lineno, col_offset,
+ end_lineno, end_col_offset);
+ }
+ return -1;
+ }
+ return 0;
+}
+
/* Attempt to load the line of text that the exception refers to. If it
fails, it will return NULL but will not set an exception.