]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43163: Handle unclosed parentheses in codeop (GH-24483)
authorPablo Galindo <Pablogsal@gmail.com>
Tue, 9 Feb 2021 20:07:38 +0000 (20:07 +0000)
committerGitHub <noreply@github.com>
Tue, 9 Feb 2021 20:07:38 +0000 (20:07 +0000)
Lib/codeop.py
Lib/test/test_codeop.py
Misc/NEWS.d/next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst [new file with mode: 0644]

index 4c10470aee7b7c1db059a29dd95b02157305fa7f..7a08610239c3571ee5207eb2f4515ddda3b75e66 100644 (file)
@@ -102,11 +102,20 @@ def _maybe_compile(compiler, source, filename, symbol):
     try:
         if code:
             return code
-        if not code1 and repr(err1) == repr(err2):
+        if not code1 and _is_syntax_error(err1, err2):
             raise err1
     finally:
         err1 = err2 = None
 
+def _is_syntax_error(err1, err2):
+    rep1 = repr(err1)
+    rep2 = repr(err2)
+    if "was never closed" in rep1 and "was never closed" in rep2:
+        return False
+    if rep1 == rep2:
+        return True
+    return False
+
 def _compile(source, filename, symbol):
     return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
 
index 1da6ca55c48f72dc50afc4a26524d0d0b9d728a5..ecc46affea262955c36c4fb3f567d543f0e5a436 100644 (file)
@@ -135,6 +135,10 @@ class CodeopTests(unittest.TestCase):
         ai("a = {")
         ai("b + {")
 
+        ai("print([1,\n2,")
+        ai("print({1:1,\n2:3,")
+        ai("print((1,\n2,")
+
         ai("if 9==3:\n   pass\nelse:")
         ai("if 9==3:\n   pass\nelse:\n")
         ai("if 9==3:\n   pass\nelse:\n   pass")
diff --git a/Misc/NEWS.d/next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst b/Misc/NEWS.d/next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst
new file mode 100644 (file)
index 0000000..ddd60ea
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a bug in :mod:`codeop` that was causing it to not ask for more input
+when multi-line snippets have unclosed parentheses. Patch by Pablo Galindo