Skip to content

bpo-43163: Handle unclosed parentheses in codeop #24483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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