From dbb228189b4eb7ab41f326eb79dae669b2c81177 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 9 Feb 2021 20:07:38 +0000 Subject: bpo-43163: Handle unclosed parentheses in codeop (GH-24483) --- Lib/codeop.py | 11 ++++++++++- Lib/test/test_codeop.py | 4 ++++ .../next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst diff --git a/Lib/codeop.py b/Lib/codeop.py index 4c10470aee7..7a08610239c 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -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) diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 1da6ca55c48..ecc46affea2 100644 --- a/Lib/test/test_codeop.py +++ b/Lib/test/test_codeop.py @@ -135,6 +135,10 @@ def test_incomplete(self): 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 index 00000000000..ddd60ea3855 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-02-08-21-13-51.bpo-43163.E2MgzH.rst @@ -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 -- cgit v1.2.3-65-gdbad