Skip to content

Commit 09f5f89

Browse files
committed
gh-113744: Add a new IncompleteInputError exception to improve incomplete input detection in the codeop module
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent 5e1916b commit 09f5f89

File tree

6 files changed

+13
-3
lines changed

6 files changed

+13
-3
lines changed

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/_compat_pickle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
('urllib', 'urlretrieve'): ('urllib.request', 'urlretrieve'),
8888
('urllib2', 'HTTPError'): ('urllib.error', 'HTTPError'),
8989
('urllib2', 'URLError'): ('urllib.error', 'URLError'),
90+
('exceptions', 'IncompleteInputError'): ('builtins', 'IncompleteInputError'),
9091
}
9192

9293
PYTHON2_EXCEPTIONS = (

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

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)

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;

0 commit comments

Comments
 (0)