https://github.com/python/cpython/commit/39d102c2ee8eec8ab0bacbcd62d62a72742ecc7c
commit: 39d102c2ee8eec8ab0bacbcd62d62a72742ecc7c
branch: main
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-01-30T16:21:30Z
summary:

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

Signed-off-by: Pablo Galindo <[email protected]>

files:
M Doc/data/stable_abi.dat
M Include/pyerrors.h
M Lib/codeop.py
M Lib/test/exception_hierarchy.txt
M Lib/test/test_pickle.py
M Lib/test/test_stable_abi_ctypes.py
M Misc/stable_abi.toml
M Objects/exceptions.c
M PC/python3dll.c
M Parser/pegen.c
M Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat
index 811b1bd84d2417..da28a2be60bc1b 100644
--- a/Doc/data/stable_abi.dat
+++ b/Doc/data/stable_abi.dat
@@ -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,,
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 5d0028c116e2d8..68d7985dac8876 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -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;
diff --git a/Lib/codeop.py b/Lib/codeop.py
index 91146be2c438e2..6ad60e7f85098d 100644
--- a/Lib/codeop.py
+++ b/Lib/codeop.py
@@ -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)
diff --git a/Lib/test/exception_hierarchy.txt b/Lib/test/exception_hierarchy.txt
index 1eca123be0fecb..217ee15d4c8af5 100644
--- a/Lib/test/exception_hierarchy.txt
+++ b/Lib/test/exception_hierarchy.txt
@@ -44,6 +44,7 @@ BaseException
       ├── StopAsyncIteration
       ├── StopIteration
       ├── SyntaxError
+      │    └── IncompleteInputError
       │    └── IndentationError
       │         └── TabError
       ├── SystemError
diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py
index b2245ddf72f708..5e187e5189d117 100644
--- a/Lib/test/test_pickle.py
+++ b/Lib/test/test_pickle.py
@@ -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),
diff --git a/Lib/test/test_stable_abi_ctypes.py 
b/Lib/test/test_stable_abi_ctypes.py
index 90d45272838420..054e7f0feb1a19 100644
--- a/Lib/test/test_stable_abi_ctypes.py
+++ b/Lib/test/test_stable_abi_ctypes.py
@@ -261,6 +261,7 @@ def test_windows_feature_macros(self):
     "PyExc_IOError",
     "PyExc_ImportError",
     "PyExc_ImportWarning",
+    "PyExc_IncompleteInputError",
     "PyExc_IndentationError",
     "PyExc_IndexError",
     "PyExc_InterruptedError",
diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml
index 2e6b0fff9cd770..ae19d25809ec86 100644
--- a/Misc/stable_abi.toml
+++ b/Misc/stable_abi.toml
@@ -2485,3 +2485,5 @@
 [function._Py_SetRefcnt]
     added = '3.13'
     abi_only = true
+[data.PyExc_IncompleteInputError]
+    added = '3.13'
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index a685ed803cd02d..cff55d05163b6b 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -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)
diff --git a/PC/python3dll.c b/PC/python3dll.c
index 07aa84c91f9fc7..09ecf98fe56ea6 100755
--- a/PC/python3dll.c
+++ b/PC/python3dll.c
@@ -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)
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 7766253a76066f..3d3e64559403b1 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -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;
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv 
b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index e3a1b5d532bda2..0b02ad01d39983 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -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  -

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to