Skip to content

Commit de6892a

Browse files
committed
bpo-40585: Normalize errors messages in codeop when comparing them
With the new parser, the error message contains always the trailing newlines, causing the naive comparison of the repr of the error messages in codeop to fail.
1 parent 2cc9b84 commit de6892a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/codeop.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ def _maybe_compile(compiler, source, filename, symbol):
9393
except SyntaxError as e:
9494
err2 = e
9595

96+
def normalize_error(err):
97+
return repr(err).replace("\\n", "")
98+
9699
try:
97100
if code:
98101
return code
99-
if not code1 and repr(err1) == repr(err2):
102+
if not code1 and normalize_error(err1) == normalize_error(err2):
100103
raise err1
101104
finally:
102105
err1 = err2 = None

Lib/test/test_codeop.py

+9
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ def test_invalid(self):
288288

289289
ai("[i for i in range(10)] = (1, 2, 3)")
290290

291+
def test_invalid_exec(self):
292+
ai = self.assertInvalid
293+
ai("raise = 4", symbol="exec")
294+
ai('def a-b', symbol='exec')
295+
ai('await?', symbol='exec')
296+
ai('=!=', symbol='exec')
297+
ai('a await raise b', symbol='exec')
298+
ai('a await raise b?+1', symbol='exec')
299+
291300
def test_filename(self):
292301
self.assertEqual(compile_command("a = 1\n", "abc").co_filename,
293302
compile("a = 1\n", "abc", 'single').co_filename)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug when using :func:`codeop.compile_command` that was causing
2+
exceptions to be swallowed with the new parser. Patch by Pablo Galindo

0 commit comments

Comments
 (0)