Skip to content

Commit 5b956ca

Browse files
authored
bpo-40585: Normalize errors messages in codeop when comparing them (GH-20030)
With the new parser, the error message contains always the trailing newlines, causing the comparison of the repr of the error messages in codeop to fail. This commit makes the new parser mirror the old parser's behaviour regarding trailing newlines.
1 parent 2cc9b84 commit 5b956ca

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_codeop.py

Lines changed: 9 additions & 0 deletions
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)
Lines changed: 2 additions & 0 deletions
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

Parser/pegen/pegen.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ get_error_line(char *buffer, int is_file)
310310
newline = strchr(buffer, '\n');
311311
}
312312

313+
if (is_file) {
314+
while (newline > buffer && newline[-1] == '\n') {
315+
--newline;
316+
}
317+
}
318+
313319
if (newline) {
314320
return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
315321
}

0 commit comments

Comments
 (0)