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.
This commit is contained in:
Pablo Galindo 2020-05-11 01:41:26 +01:00 committed by GitHub
parent 2cc9b8486d
commit 5b956ca42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -288,6 +288,15 @@ class CodeopTests(unittest.TestCase):
ai("[i for i in range(10)] = (1, 2, 3)") 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): def test_filename(self):
self.assertEqual(compile_command("a = 1\n", "abc").co_filename, self.assertEqual(compile_command("a = 1\n", "abc").co_filename,
compile("a = 1\n", "abc", 'single').co_filename) compile("a = 1\n", "abc", 'single').co_filename)

View File

@ -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

View File

@ -310,6 +310,12 @@ get_error_line(char *buffer, int is_file)
newline = strchr(buffer, '\n'); newline = strchr(buffer, '\n');
} }
if (is_file) {
while (newline > buffer && newline[-1] == '\n') {
--newline;
}
}
if (newline) { if (newline) {
return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace"); return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
} }