Skip to content

Commit aba42c0

Browse files
authored
gh-123969: refactor _PyErr_RaiseSyntaxError and _PyErr_EmitSyntaxWarning out of compiler (#123972)
1 parent 9aa1f60 commit aba42c0

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

Include/internal/pycore_compile.h

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ extern int _PyCompile_AstOptimize(
3333
int optimize,
3434
struct _arena *arena);
3535

36-
struct _Py_SourceLocation;
37-
3836
extern int _PyAST_Optimize(
3937
struct _mod *,
4038
struct _arena *arena,

Include/internal/pycore_pyerrors.h

+5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ extern void _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
120120

121121
extern PyObject* _PyErr_NoMemory(PyThreadState *tstate);
122122

123+
extern int _PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset,
124+
int end_lineno, int end_col_offset);
125+
extern void _PyErr_RaiseSyntaxError(PyObject *msg, PyObject *filename, int lineno, int col_offset,
126+
int end_lineno, int end_col_offset);
127+
123128
PyAPI_FUNC(void) _PyErr_SetString(
124129
PyThreadState *tstate,
125130
PyObject *exception,

Python/compile.c

+7-30
Original file line numberDiff line numberDiff line change
@@ -1112,27 +1112,15 @@ _PyCompile_Error(compiler *c, location loc, const char *format, ...)
11121112
if (msg == NULL) {
11131113
return ERROR;
11141114
}
1115-
PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno);
1116-
if (loc_obj == NULL) {
1117-
loc_obj = Py_None;
1118-
}
1119-
PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
1120-
loc.lineno, loc.col_offset + 1, loc_obj,
1121-
loc.end_lineno, loc.end_col_offset + 1);
1115+
_PyErr_RaiseSyntaxError(msg, c->c_filename, loc.lineno, loc.col_offset + 1,
1116+
loc.end_lineno, loc.end_col_offset + 1);
11221117
Py_DECREF(msg);
1123-
if (args == NULL) {
1124-
goto exit;
1125-
}
1126-
PyErr_SetObject(PyExc_SyntaxError, args);
1127-
exit:
1128-
Py_DECREF(loc_obj);
1129-
Py_XDECREF(args);
11301118
return ERROR;
11311119
}
11321120

1133-
/* Emits a SyntaxWarning and returns 1 on success.
1121+
/* Emits a SyntaxWarning and returns 0 on success.
11341122
If a SyntaxWarning raised as error, replaces it with a SyntaxError
1135-
and returns 0.
1123+
and returns -1.
11361124
*/
11371125
int
11381126
_PyCompile_Warn(compiler *c, location loc, const char *format, ...)
@@ -1144,21 +1132,10 @@ _PyCompile_Warn(compiler *c, location loc, const char *format, ...)
11441132
if (msg == NULL) {
11451133
return ERROR;
11461134
}
1147-
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
1148-
loc.lineno, NULL, NULL) < 0)
1149-
{
1150-
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
1151-
/* Replace the SyntaxWarning exception with a SyntaxError
1152-
to get a more accurate error report */
1153-
PyErr_Clear();
1154-
assert(PyUnicode_AsUTF8(msg) != NULL);
1155-
_PyCompile_Error(c, loc, PyUnicode_AsUTF8(msg));
1156-
}
1157-
Py_DECREF(msg);
1158-
return ERROR;
1159-
}
1135+
int ret = _PyErr_EmitSyntaxWarning(msg, c->c_filename, loc.lineno, loc.col_offset + 1,
1136+
loc.end_lineno, loc.end_col_offset + 1);
11601137
Py_DECREF(msg);
1161-
return SUCCESS;
1138+
return ret;
11621139
}
11631140

11641141
PyObject *

Python/errors.c

+46
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,52 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
18501850
Py_XDECREF(fileobj);
18511851
}
18521852

1853+
/* Raises a SyntaxError.
1854+
* If something goes wrong, a different exception may be raised.
1855+
*/
1856+
void
1857+
_PyErr_RaiseSyntaxError(PyObject *msg, PyObject *filename, int lineno, int col_offset,
1858+
int end_lineno, int end_col_offset)
1859+
{
1860+
PyObject *text = PyErr_ProgramTextObject(filename, lineno);
1861+
if (text == NULL) {
1862+
text = Py_NewRef(Py_None);
1863+
}
1864+
PyObject *args = Py_BuildValue("O(OiiOii)", msg, filename,
1865+
lineno, col_offset, text,
1866+
end_lineno, end_col_offset);
1867+
if (args == NULL) {
1868+
goto exit;
1869+
}
1870+
PyErr_SetObject(PyExc_SyntaxError, args);
1871+
exit:
1872+
Py_DECREF(text);
1873+
Py_XDECREF(args);
1874+
}
1875+
1876+
/* Emits a SyntaxWarning and returns 0 on success.
1877+
If a SyntaxWarning is raised as error, replaces it with a SyntaxError
1878+
and returns -1.
1879+
*/
1880+
int
1881+
_PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset,
1882+
int end_lineno, int end_col_offset)
1883+
{
1884+
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, filename,
1885+
lineno, NULL, NULL) < 0)
1886+
{
1887+
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
1888+
/* Replace the SyntaxWarning exception with a SyntaxError
1889+
to get a more accurate error report */
1890+
PyErr_Clear();
1891+
_PyErr_RaiseSyntaxError(msg, filename, lineno, col_offset,
1892+
end_lineno, end_col_offset);
1893+
}
1894+
return -1;
1895+
}
1896+
return 0;
1897+
}
1898+
18531899
/* Attempt to load the line of text that the exception refers to. If it
18541900
fails, it will return NULL but will not set an exception.
18551901

0 commit comments

Comments
 (0)