gh-113744: Add a new IncompleteInputError exception to improve incomplete input detection in the codeop module (#113745)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
Pablo Galindo Salgado 2024-01-30 16:21:30 +00:00 committed by GitHub
parent 1f515e8a10
commit 39d102c2ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 21 additions and 4 deletions

View File

@ -220,6 +220,7 @@ var,PyExc_GeneratorExit,3.2,,
var,PyExc_IOError,3.2,,
var,PyExc_ImportError,3.2,,
var,PyExc_ImportWarning,3.2,,
var,PyExc_IncompleteInputError,3.13,,
var,PyExc_IndentationError,3.2,,
var,PyExc_IndexError,3.2,,
var,PyExc_InterruptedError,3.7,,

View File

@ -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;

View File

@ -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)

View File

@ -44,6 +44,7 @@ BaseException
├── StopAsyncIteration
├── StopIteration
├── SyntaxError
│ └── IncompleteInputError
│ └── IndentationError
│ └── TabError
├── SystemError

View File

@ -567,7 +567,8 @@ class CompatPickleTests(unittest.TestCase):
RecursionError,
EncodingWarning,
BaseExceptionGroup,
ExceptionGroup):
ExceptionGroup,
IncompleteInputError):
continue
if exc is not OSError and issubclass(exc, OSError):
self.assertEqual(reverse_mapping('builtins', name),

View File

@ -261,6 +261,7 @@ SYMBOL_NAMES = (
"PyExc_IOError",
"PyExc_ImportError",
"PyExc_ImportWarning",
"PyExc_IncompleteInputError",
"PyExc_IndentationError",
"PyExc_IndexError",
"PyExc_InterruptedError",

View File

@ -2485,3 +2485,5 @@
[function._Py_SetRefcnt]
added = '3.13'
abi_only = true
[data.PyExc_IncompleteInputError]
added = '3.13'

View File

@ -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
@ -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)

1
PC/python3dll.c generated
View File

@ -830,6 +830,7 @@ EXPORT_DATA(PyExc_FutureWarning)
EXPORT_DATA(PyExc_GeneratorExit)
EXPORT_DATA(PyExc_ImportError)
EXPORT_DATA(PyExc_ImportWarning)
EXPORT_DATA(PyExc_IncompleteInputError)
EXPORT_DATA(PyExc_IndentationError)
EXPORT_DATA(PyExc_IndexError)
EXPORT_DATA(PyExc_InterruptedError)

View File

@ -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;

View File

@ -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 -
@ -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 -

Can't render this file because it has a wrong number of fields in line 4.