Skip to content

Commit 39d102c

Browse files
authored
gh-113744: Add a new IncompleteInputError exception to improve incomplete input detection in the codeop module (#113745)
Signed-off-by: Pablo Galindo <[email protected]>
1 parent 1f515e8 commit 39d102c

File tree

11 files changed

+21
-4
lines changed

11 files changed

+21
-4
lines changed

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/pyerrors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
108108
PyAPI_DATA(PyObject *) PyExc_SyntaxError;
109109
PyAPI_DATA(PyObject *) PyExc_IndentationError;
110110
PyAPI_DATA(PyObject *) PyExc_TabError;
111+
PyAPI_DATA(PyObject *) PyExc_IncompleteInputError;
111112
PyAPI_DATA(PyObject *) PyExc_ReferenceError;
112113
PyAPI_DATA(PyObject *) PyExc_SystemError;
113114
PyAPI_DATA(PyObject *) PyExc_SystemExit;

Lib/codeop.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def _maybe_compile(compiler, source, filename, symbol):
6565
try:
6666
compiler(source + "\n", filename, symbol)
6767
return None
68+
except IncompleteInputError as e:
69+
return None
6870
except SyntaxError as e:
69-
if "incomplete input" in str(e):
70-
return None
71+
pass
7172
# fallthrough
7273

7374
return compiler(source, filename, symbol, incomplete_input=False)

Lib/test/exception_hierarchy.txt

Lines changed: 1 addition & 0 deletions
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

Lib/test/test_pickle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ def test_exceptions(self):
567567
RecursionError,
568568
EncodingWarning,
569569
BaseExceptionGroup,
570-
ExceptionGroup):
570+
ExceptionGroup,
571+
IncompleteInputError):
571572
continue
572573
if exc is not OSError and issubclass(exc, OSError):
573574
self.assertEqual(reverse_mapping('builtins', name),

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,3 +2485,5 @@
24852485
[function._Py_SetRefcnt]
24862486
added = '3.13'
24872487
abi_only = true
2488+
[data.PyExc_IncompleteInputError]
2489+
added = '3.13'

Objects/exceptions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,11 @@ MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
25662566
MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
25672567
"Improper mixture of spaces and tabs.");
25682568

2569+
/*
2570+
* IncompleteInputError extends SyntaxError
2571+
*/
2572+
MiddlingExtendsException(PyExc_SyntaxError, IncompleteInputError, SyntaxError,
2573+
"incomplete input.");
25692574

25702575
/*
25712576
* LookupError extends Exception
@@ -3635,6 +3640,7 @@ static struct static_exception static_exceptions[] = {
36353640

36363641
// Level 4: Other subclasses
36373642
ITEM(IndentationError), // base: SyntaxError(Exception)
3643+
ITEM(IncompleteInputError), // base: SyntaxError(Exception)
36383644
ITEM(IndexError), // base: LookupError(Exception)
36393645
ITEM(KeyError), // base: LookupError(Exception)
36403646
ITEM(ModuleNotFoundError), // base: ImportError(Exception)

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Parser/pegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ _PyPegen_run_parser(Parser *p)
844844
if (res == NULL) {
845845
if ((p->flags & PyPARSE_ALLOW_INCOMPLETE_INPUT) && _is_end_of_source(p)) {
846846
PyErr_Clear();
847-
return RAISE_SYNTAX_ERROR("incomplete input");
847+
return _PyPegen_raise_error(p, PyExc_IncompleteInputError, 0, "incomplete input");
848848
}
849849
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_SyntaxError)) {
850850
return NULL;

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ Objects/exceptions.c - _PyExc_AttributeError -
197197
Objects/exceptions.c - _PyExc_SyntaxError -
198198
Objects/exceptions.c - _PyExc_IndentationError -
199199
Objects/exceptions.c - _PyExc_TabError -
200+
Objects/exceptions.c - _PyExc_IncompleteInputError -
200201
Objects/exceptions.c - _PyExc_LookupError -
201202
Objects/exceptions.c - _PyExc_IndexError -
202203
Objects/exceptions.c - _PyExc_KeyError -
@@ -261,6 +262,7 @@ Objects/exceptions.c - PyExc_AttributeError -
261262
Objects/exceptions.c - PyExc_SyntaxError -
262263
Objects/exceptions.c - PyExc_IndentationError -
263264
Objects/exceptions.c - PyExc_TabError -
265+
Objects/exceptions.c - PyExc_IncompleteInputError -
264266
Objects/exceptions.c - PyExc_LookupError -
265267
Objects/exceptions.c - PyExc_IndexError -
266268
Objects/exceptions.c - PyExc_KeyError -

0 commit comments

Comments
 (0)