Skip to content

Commit

Permalink
pythongh-113744: Add a new IncompleteInputError exception to improve …
Browse files Browse the repository at this point in the history
…incomplete input detection in the codeop module (python#113745)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal committed Jan 30, 2024
1 parent 1f515e8 commit 39d102c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
PyAPI_DATA(PyObject *) PyExc_SyntaxError;
PyAPI_DATA(PyObject *) PyExc_IndentationError;
PyAPI_DATA(PyObject *) PyExc_TabError;
PyAPI_DATA(PyObject *) PyExc_IncompleteInputError;
PyAPI_DATA(PyObject *) PyExc_ReferenceError;
PyAPI_DATA(PyObject *) PyExc_SystemError;
PyAPI_DATA(PyObject *) PyExc_SystemExit;
Expand Down
5 changes: 3 additions & 2 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ def _maybe_compile(compiler, source, filename, symbol):
try:
compiler(source + "\n", filename, symbol)
return None
except IncompleteInputError as e:
return None
except SyntaxError as e:
if "incomplete input" in str(e):
return None
pass
# fallthrough

return compiler(source, filename, symbol, incomplete_input=False)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/exception_hierarchy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ BaseException
├── StopAsyncIteration
├── StopIteration
├── SyntaxError
│ └── IncompleteInputError
│ └── IndentationError
│ └── TabError
├── SystemError
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ def test_exceptions(self):
RecursionError,
EncodingWarning,
BaseExceptionGroup,
ExceptionGroup):
ExceptionGroup,
IncompleteInputError):
continue
if exc is not OSError and issubclass(exc, OSError):
self.assertEqual(reverse_mapping('builtins', name),
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2485,3 +2485,5 @@
[function._Py_SetRefcnt]
added = '3.13'
abi_only = true
[data.PyExc_IncompleteInputError]
added = '3.13'
6 changes: 6 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,11 @@ MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
"Improper mixture of spaces and tabs.");

/*
* IncompleteInputError extends SyntaxError
*/
MiddlingExtendsException(PyExc_SyntaxError, IncompleteInputError, SyntaxError,
"incomplete input.");

/*
* LookupError extends Exception
Expand Down Expand Up @@ -3635,6 +3640,7 @@ static struct static_exception static_exceptions[] = {

// Level 4: Other subclasses
ITEM(IndentationError), // base: SyntaxError(Exception)
ITEM(IncompleteInputError), // base: SyntaxError(Exception)
ITEM(IndexError), // base: LookupError(Exception)
ITEM(KeyError), // base: LookupError(Exception)
ITEM(ModuleNotFoundError), // base: ImportError(Exception)
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ _PyPegen_run_parser(Parser *p)
if (res == NULL) {
if ((p->flags & PyPARSE_ALLOW_INCOMPLETE_INPUT) && _is_end_of_source(p)) {
PyErr_Clear();
return RAISE_SYNTAX_ERROR("incomplete input");
return _PyPegen_raise_error(p, PyExc_IncompleteInputError, 0, "incomplete input");
}
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_SyntaxError)) {
return NULL;
Expand Down
2 changes: 2 additions & 0 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Objects/exceptions.c - _PyExc_AttributeError -
Objects/exceptions.c - _PyExc_SyntaxError -
Objects/exceptions.c - _PyExc_IndentationError -
Objects/exceptions.c - _PyExc_TabError -
Objects/exceptions.c - _PyExc_IncompleteInputError -
Objects/exceptions.c - _PyExc_LookupError -
Objects/exceptions.c - _PyExc_IndexError -
Objects/exceptions.c - _PyExc_KeyError -
Expand Down Expand Up @@ -261,6 +262,7 @@ Objects/exceptions.c - PyExc_AttributeError -
Objects/exceptions.c - PyExc_SyntaxError -
Objects/exceptions.c - PyExc_IndentationError -
Objects/exceptions.c - PyExc_TabError -
Objects/exceptions.c - PyExc_IncompleteInputError -
Objects/exceptions.c - PyExc_LookupError -
Objects/exceptions.c - PyExc_IndexError -
Objects/exceptions.c - PyExc_KeyError -
Expand Down

0 comments on commit 39d102c

Please sign in to comment.