Skip to content

Commit f2af978

Browse files
committed
fix: Add better error message for case without match
1 parent 805e336 commit f2af978

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

Include/pyerrors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
274274
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
275275
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
276276

277-
/* get the value of the start attribute (the int * may not be NULL)
277+
/* get the value of the start attribute (the int *may not be NULL)
278278
return 0 on success, -1 on failure */
279279
PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
280280
PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);

Python/errors.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Error handling */
32

43
#include "Python.h"
@@ -2083,3 +2082,73 @@ PyErr_ProgramTextObject(PyObject *filename, int lineno)
20832082
{
20842083
return _PyErr_ProgramDecodedTextObject(filename, lineno, NULL);
20852084
}
2085+
2086+
/* Error codes for the parser */
2087+
2088+
#define E_SYNTAX 1
2089+
#define E_INDENTATION 2
2090+
#define E_UNTERMINATED_STRING 3
2091+
#define E_UNTERMINATED_COMMENT 4
2092+
#define E_BAD_CHARACTER 5
2093+
#define E_EOF_IN_STRING 6
2094+
#define E_EOF_IN_COMMENT 7
2095+
#define E_MULTILINE_COMMENT 8
2096+
#define E_BACKSLASH 9
2097+
#define E_CONTINUED 10
2098+
#define E_INVALID_ESCAPE_SEQUENCE 11
2099+
#define E_INVALID_SYNTAX 12
2100+
#define E_INVALID_CASE_OUTSIDE_MATCH 13
2101+
2102+
/* Error messages for the parser */
2103+
2104+
static PyObject *
2105+
parser_error_msg(PyThreadState *tstate, int error)
2106+
{
2107+
switch (error) {
2108+
case E_SYNTAX:
2109+
return PyUnicode_FromString("invalid syntax");
2110+
case E_INDENTATION:
2111+
return PyUnicode_FromString("unexpected indent");
2112+
case E_UNTERMINATED_STRING:
2113+
return PyUnicode_FromString("unterminated string literal");
2114+
case E_UNTERMINATED_COMMENT:
2115+
return PyUnicode_FromString("unterminated comment");
2116+
case E_BAD_CHARACTER:
2117+
return PyUnicode_FromString("invalid character in identifier");
2118+
case E_EOF_IN_STRING:
2119+
return PyUnicode_FromString("eof in multi-line string");
2120+
case E_EOF_IN_COMMENT:
2121+
return PyUnicode_FromString("eof in multi-line comment");
2122+
case E_MULTILINE_COMMENT:
2123+
return PyUnicode_FromString("multi-line comment not terminated");
2124+
case E_BACKSLASH:
2125+
return PyUnicode_FromString("unexpected character after line continuation character");
2126+
case E_CONTINUED:
2127+
return PyUnicode_FromString("unexpected EOF while parsing");
2128+
case E_INVALID_ESCAPE_SEQUENCE:
2129+
return PyUnicode_FromString("invalid escape sequence");
2130+
case E_INVALID_SYNTAX:
2131+
return PyUnicode_FromString("invalid syntax");
2132+
case E_INVALID_CASE_OUTSIDE_MATCH:
2133+
return PyUnicodeFromFormat("case statement must be inside match statement");
2134+
}
2135+
return NULL;
2136+
}
2137+
2138+
/* Parser error reporting */
2139+
2140+
void
2141+
_PyErr_SetParserError(PyThreadState *tstate, int error)
2142+
{
2143+
PyObject *msg = parser_error_msg(tstate, error);
2144+
if (msg == NULL) {
2145+
return;
2146+
}
2147+
PyObject *exc = PyObject_CallFunctionObjArgs(PyExc_SyntaxError, msg, NULL);
2148+
Py_DECREF(msg);
2149+
if (exc == NULL) {
2150+
return;
2151+
}
2152+
_PyErr_SetRaisedException(tstate, exc);
2153+
Py_DECREF(exc);
2154+
}

0 commit comments

Comments
 (0)