]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42918: Improve build-in function compile() in mode 'single' (GH-29934)
authorWeipeng Hong <hongweichen8888@sina.com>
Fri, 10 Dec 2021 23:44:26 +0000 (07:44 +0800)
committerGitHub <noreply@github.com>
Fri, 10 Dec 2021 23:44:26 +0000 (00:44 +0100)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2021-12-06-15-32-12.bpo-42918.Czpgtg.rst [new file with mode: 0644]
Parser/pegen.c

index f72c7ca68f363a382646aaf1c7a8b8116abf92d5..f4ed6c706da668a76070526dd36581d17dc140c9 100644 (file)
@@ -504,6 +504,7 @@ if 1:
         self.compile_single("if x:\n   f(x)")
         self.compile_single("if x:\n   f(x)\nelse:\n   g(x)")
         self.compile_single("class T:\n   pass")
+        self.compile_single("c = '''\na=1\nb=2\nc=3\n'''")
 
     def test_bad_single_statement(self):
         self.assertInvalidSingle('1\n2')
@@ -514,6 +515,7 @@ if 1:
         self.assertInvalidSingle('f()\n# blah\nblah()')
         self.assertInvalidSingle('f()\nxy # blah\nblah()')
         self.assertInvalidSingle('x = 5 # comment\nx = 6\n')
+        self.assertInvalidSingle("c = '''\nd=1\n'''\na = 1\n\nb = 2\n")
 
     def test_particularly_evil_undecodable(self):
         # Issue 24022
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-06-15-32-12.bpo-42918.Czpgtg.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-06-15-32-12.bpo-42918.Czpgtg.rst
new file mode 100644 (file)
index 0000000..f03dade
--- /dev/null
@@ -0,0 +1,3 @@
+Fix bug where the built-in :func:`compile` function did not always raise a
+:exc:`SyntaxError` when passed multiple statements in 'single' mode. Patch by
+Weipeng Hong.
index ede281ac89cd9aa51091493479954e7a963e6d35..4158a81fd52f77f365959344329061664d1775fa 100644 (file)
@@ -675,31 +675,13 @@ _PyPegen_number_token(Parser *p)
                            t->end_col_offset, p->arena);
 }
 
-static int // bool
-newline_in_string(Parser *p, const char *cur)
-{
-    for (const char *c = cur; c >= p->tok->buf; c--) {
-        if (*c == '\'' || *c == '"') {
-            return 1;
-        }
-    }
-    return 0;
-}
-
 /* Check that the source for a single input statement really is a single
    statement by looking at what is left in the buffer after parsing.
    Trailing whitespace and comments are OK. */
 static int // bool
 bad_single_statement(Parser *p)
 {
-    const char *cur = strchr(p->tok->buf, '\n');
-
-    /* Newlines are allowed if preceded by a line continuation character
-       or if they appear inside a string. */
-    if (!cur || (cur != p->tok->buf && *(cur - 1) == '\\')
-             || newline_in_string(p, cur)) {
-        return 0;
-    }
+    char *cur = p->tok->cur;
     char c = *cur;
 
     for (;;) {