gh-113744: Add a new IncompleteInputError exception to improve incomp… · python/cpython@39d102c

GitHub

Original file line numberDiff line numberDiff line change@@ -108,6 +108,7 @@ PyAPI_DATA(PyObject *) PyExc_NotImplementedError;

108108PyAPI_DATA(PyObject*) PyExc_SyntaxError;

109109PyAPI_DATA(PyObject*) PyExc_IndentationError;

110110PyAPI_DATA(PyObject*) PyExc_TabError;

111+PyAPI_DATA(PyObject*) PyExc_IncompleteInputError;

111112PyAPI_DATA(PyObject*) PyExc_ReferenceError;

112113PyAPI_DATA(PyObject*) PyExc_SystemError;

113114PyAPI_DATA(PyObject*) PyExc_SystemExit;

Original file line numberDiff line numberDiff line change@@ -65,9 +65,10 @@ def _maybe_compile(compiler, source, filename, symbol):

6565try:

6666compiler(source+"\n", filename, symbol)

6767returnNone

68+exceptIncompleteInputErrorase:

69+returnNone

6870exceptSyntaxErrorase:

69-if"incomplete input"instr(e):

70-returnNone

71+pass

7172# fallthrough

72737374returncompiler(source, filename, symbol, incomplete_input=False)

Original file line numberDiff line numberDiff line change@@ -44,6 +44,7 @@ BaseException

4444 ├── StopAsyncIteration

4545 ├── StopIteration

4646 ├── SyntaxError

47+ │ └── IncompleteInputError

4748 │ └── IndentationError

4849 │ └── TabError

4950 ├── SystemError

Original file line numberDiff line numberDiff line change@@ -567,7 +567,8 @@ def test_exceptions(self):

567567RecursionError,

568568EncodingWarning,

569569BaseExceptionGroup,

570-ExceptionGroup):

570+ExceptionGroup,

571+IncompleteInputError):

571572continue

572573ifexcisnotOSErrorandissubclass(exc, OSError):

573574self.assertEqual(reverse_mapping('builtins', name),

Original file line numberDiff line numberDiff line change@@ -2485,3 +2485,5 @@

24852485[function._Py_SetRefcnt]

24862486added = '3.13'

24872487abi_only = true

2488+[data.PyExc_IncompleteInputError]

2489+added = '3.13'

Original file line numberDiff line numberDiff line change@@ -2566,6 +2566,11 @@ MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,

25662566MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,

25672567"Improper mixture of spaces and tabs.");

256825682569+/*

2570+ * IncompleteInputError extends SyntaxError

2571+ */

2572+MiddlingExtendsException(PyExc_SyntaxError, IncompleteInputError, SyntaxError,

2573+"incomplete input.");

2569257425702575/*

25712576 * LookupError extends Exception

@@ -3635,6 +3640,7 @@ static struct static_exception static_exceptions[] = {

3635364036363641// Level 4: Other subclasses

36373642ITEM(IndentationError), // base: SyntaxError(Exception)

3643+ITEM(IncompleteInputError), // base: SyntaxError(Exception)

36383644ITEM(IndexError), // base: LookupError(Exception)

36393645ITEM(KeyError), // base: LookupError(Exception)

36403646ITEM(ModuleNotFoundError), // base: ImportError(Exception)

Original file line numberDiff line numberDiff line change@@ -844,7 +844,7 @@ _PyPegen_run_parser(Parser *p)

844844if (res==NULL) {

845845if ((p->flags&PyPARSE_ALLOW_INCOMPLETE_INPUT) &&_is_end_of_source(p)) {

846846PyErr_Clear();

847-returnRAISE_SYNTAX_ERROR("incomplete input");

847+return_PyPegen_raise_error(p, PyExc_IncompleteInputError, 0, "incomplete input");

848848 }

849849if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_SyntaxError)) {

850850returnNULL;