Skip to content
9 changes: 9 additions & 0 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ def test_invalid(self):

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

def test_invalid_exec(self):
ai = self.assertInvalid
ai("raise = 4", symbol="exec")
ai('def a-b', symbol='exec')
ai('await?', symbol='exec')
ai('=!=', symbol='exec')
ai('a await raise b', symbol='exec')
ai('a await raise b?+1', symbol='exec')

def test_filename(self):
self.assertEqual(compile_command("a = 1\n", "abc").co_filename,
compile("a = 1\n", "abc", 'single').co_filename)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug when using :func:`codeop.compile_command` that was causing
exceptions to be swallowed with the new parser. Patch by Pablo Galindo
6 changes: 6 additions & 0 deletions Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ get_error_line(char *buffer, int is_file)
newline = strchr(buffer, '\n');
}

if (is_file) {
while (newline > buffer && newline[-1] == '\n') {
--newline;
}
}

if (newline) {
return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
}
Expand Down