Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);

/* get the value of the start attribute (the int * may not be NULL)
/* get the value of the start attribute (the int *may not be NULL)
return 0 on success, -1 on failure */
PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
Expand Down
71 changes: 70 additions & 1 deletion Python/errors.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/* Error handling */

#include "Python.h"
Expand Down Expand Up @@ -2083,3 +2082,73 @@
{
return _PyErr_ProgramDecodedTextObject(filename, lineno, NULL);
}

/* Error codes for the parser */

#define E_SYNTAX 1
#define E_INDENTATION 2
#define E_UNTERMINATED_STRING 3
#define E_UNTERMINATED_COMMENT 4
#define E_BAD_CHARACTER 5
#define E_EOF_IN_STRING 6
#define E_EOF_IN_COMMENT 7
#define E_MULTILINE_COMMENT 8
#define E_BACKSLASH 9
#define E_CONTINUED 10
#define E_INVALID_ESCAPE_SEQUENCE 11
#define E_INVALID_SYNTAX 12
#define E_INVALID_CASE_OUTSIDE_MATCH 13

/* Error messages for the parser */

static PyObject *
parser_error_msg(PyThreadState *tstate, int error)
{
switch (error) {
case E_SYNTAX:
return PyUnicode_FromString("invalid syntax");
case E_INDENTATION:
return PyUnicode_FromString("unexpected indent");
case E_UNTERMINATED_STRING:
return PyUnicode_FromString("unterminated string literal");
case E_UNTERMINATED_COMMENT:
return PyUnicode_FromString("unterminated comment");
case E_BAD_CHARACTER:
return PyUnicode_FromString("invalid character in identifier");
case E_EOF_IN_STRING:
return PyUnicode_FromString("eof in multi-line string");
case E_EOF_IN_COMMENT:
return PyUnicode_FromString("eof in multi-line comment");
case E_MULTILINE_COMMENT:
return PyUnicode_FromString("multi-line comment not terminated");
case E_BACKSLASH:
return PyUnicode_FromString("unexpected character after line continuation character");
case E_CONTINUED:
return PyUnicode_FromString("unexpected EOF while parsing");
case E_INVALID_ESCAPE_SEQUENCE:
return PyUnicode_FromString("invalid escape sequence");
case E_INVALID_SYNTAX:
return PyUnicode_FromString("invalid syntax");
case E_INVALID_CASE_OUTSIDE_MATCH:
return PyUnicodeFromFormat("case statement must be inside match statement");

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'PyUnicodeFromFormat' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'PyUnicodeFromFormat' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'PyUnicodeFromFormat' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'PyUnicodeFromFormat' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]

Check warning on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2133 in Python/errors.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘PyUnicodeFromFormat’; did you mean ‘PyUnicode_FromFormat’? [-Werror=implicit-function-declaration]
}
return NULL;
}

/* Parser error reporting */

void
_PyErr_SetParserError(PyThreadState *tstate, int error)
{
PyObject *msg = parser_error_msg(tstate, error);
if (msg == NULL) {
return;
}
PyObject *exc = PyObject_CallFunctionObjArgs(PyExc_SyntaxError, msg, NULL);
Py_DECREF(msg);
if (exc == NULL) {
return;
}
_PyErr_SetRaisedException(tstate, exc);
Py_DECREF(exc);
}
Loading