]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40585: Normalize errors messages in codeop when comparing them (GH-20030)
authorPablo Galindo <Pablogsal@gmail.com>
Mon, 11 May 2020 00:41:26 +0000 (01:41 +0100)
committerGitHub <noreply@github.com>
Mon, 11 May 2020 00:41:26 +0000 (01:41 +0100)
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.

Lib/test/test_codeop.py
Misc/NEWS.d/next/Core and Builtins/2020-05-11-00-19-42.bpo-40585.yusknY.rst [new file with mode: 0644]
Parser/pegen/pegen.c

index 1f27830ae50b841041a82f36f6c44c9f3f4d379a..0c5e362feea0ca13b3697c611d0fe7dbaafff7f9 100644 (file)
@@ -288,6 +288,15 @@ class CodeopTests(unittest.TestCase):
 
         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)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-05-11-00-19-42.bpo-40585.yusknY.rst b/Misc/NEWS.d/next/Core and Builtins/2020-05-11-00-19-42.bpo-40585.yusknY.rst
new file mode 100644 (file)
index 0000000..7a9258e
--- /dev/null
@@ -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
index 06af53b3597f7438a4d0d99c5a7e60a39222dbc0..c80f08668b07d620946f10da4b4f384bafd5baf3 100644 (file)
@@ -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");
     }