]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123969: refactor _PyErr_RaiseSyntaxError and _PyErr_EmitSyntaxWarning out of compi...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 16 Sep 2024 14:05:00 +0000 (15:05 +0100)
committerGitHub <noreply@github.com>
Mon, 16 Sep 2024 14:05:00 +0000 (15:05 +0100)
Include/internal/pycore_compile.h
Include/internal/pycore_pyerrors.h
Python/compile.c
Python/errors.c

index f089eb05097b521c98f28c268d49664745be10ff..9f0ca33892a43bf129b1aff5e18427fba9d04653 100644 (file)
@@ -33,8 +33,6 @@ extern int _PyCompile_AstOptimize(
     int optimize,
     struct _arena *arena);
 
-struct _Py_SourceLocation;
-
 extern int _PyAST_Optimize(
     struct _mod *,
     struct _arena *arena,
index 9835e495d176e7e3ec79b4e283d9878c814824b8..02945f0e71a1451ff78b78b160077047ebc7aaf2 100644 (file)
@@ -120,6 +120,11 @@ extern void _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
 
 extern PyObject* _PyErr_NoMemory(PyThreadState *tstate);
 
+extern int _PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset,
+                                    int end_lineno, int end_col_offset);
+extern void _PyErr_RaiseSyntaxError(PyObject *msg, PyObject *filename, int lineno, int col_offset,
+                                    int end_lineno, int end_col_offset);
+
 PyAPI_FUNC(void) _PyErr_SetString(
     PyThreadState *tstate,
     PyObject *exception,
index cdd4878257525f2b93a55959b6270cadb7f36020..7b3e6f336e44b18bc2107c30d790e369aaef43a0 100644 (file)
@@ -1112,27 +1112,15 @@ _PyCompile_Error(compiler *c, location loc, const char *format, ...)
     if (msg == NULL) {
         return ERROR;
     }
-    PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno);
-    if (loc_obj == NULL) {
-        loc_obj = Py_None;
-    }
-    PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
-                                   loc.lineno, loc.col_offset + 1, loc_obj,
-                                   loc.end_lineno, loc.end_col_offset + 1);
+    _PyErr_RaiseSyntaxError(msg, c->c_filename, loc.lineno, loc.col_offset + 1,
+                            loc.end_lineno, loc.end_col_offset + 1);
     Py_DECREF(msg);
-    if (args == NULL) {
-        goto exit;
-    }
-    PyErr_SetObject(PyExc_SyntaxError, args);
- exit:
-    Py_DECREF(loc_obj);
-    Py_XDECREF(args);
     return ERROR;
 }
 
-/* Emits a SyntaxWarning and returns 1 on success.
+/* Emits a SyntaxWarning and returns 0 on success.
    If a SyntaxWarning raised as error, replaces it with a SyntaxError
-   and returns 0.
+   and returns -1.
 */
 int
 _PyCompile_Warn(compiler *c, location loc, const char *format, ...)
@@ -1144,21 +1132,10 @@ _PyCompile_Warn(compiler *c, location loc, const char *format, ...)
     if (msg == NULL) {
         return ERROR;
     }
-    if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
-                                 loc.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();
-            assert(PyUnicode_AsUTF8(msg) != NULL);
-            _PyCompile_Error(c, loc, PyUnicode_AsUTF8(msg));
-        }
-        Py_DECREF(msg);
-        return ERROR;
-    }
+    int ret = _PyErr_EmitSyntaxWarning(msg, c->c_filename, loc.lineno, loc.col_offset + 1,
+                                       loc.end_lineno, loc.end_col_offset + 1);
     Py_DECREF(msg);
-    return SUCCESS;
+    return ret;
 }
 
 PyObject *
index ad6b7dbef075ccf696348638bff4e06b7e8f09e7..29249ac41c634669440844468e15bd0f04d18dfc 100644 (file)
@@ -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.