Skip to content

Commit dbb2281

Browse files
authored
bpo-43163: Handle unclosed parentheses in codeop (GH-24483)
1 parent b74396c commit dbb2281

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/codeop.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,20 @@ def _maybe_compile(compiler, source, filename, symbol):
102102
try:
103103
if code:
104104
return code
105-
if not code1 and repr(err1) == repr(err2):
105+
if not code1 and _is_syntax_error(err1, err2):
106106
raise err1
107107
finally:
108108
err1 = err2 = None
109109

110+
def _is_syntax_error(err1, err2):
111+
rep1 = repr(err1)
112+
rep2 = repr(err2)
113+
if "was never closed" in rep1 and "was never closed" in rep2:
114+
return False
115+
if rep1 == rep2:
116+
return True
117+
return False
118+
110119
def _compile(source, filename, symbol):
111120
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
112121

Lib/test/test_codeop.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def test_incomplete(self):
135135
ai("a = {")
136136
ai("b + {")
137137

138+
ai("print([1,\n2,")
139+
ai("print({1:1,\n2:3,")
140+
ai("print((1,\n2,")
141+
138142
ai("if 9==3:\n pass\nelse:")
139143
ai("if 9==3:\n pass\nelse:\n")
140144
ai("if 9==3:\n pass\nelse:\n pass")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :mod:`codeop` that was causing it to not ask for more input
2+
when multi-line snippets have unclosed parentheses. Patch by Pablo Galindo

0 commit comments

Comments
 (0)