[Python-checkins] bpo-42918: Improve built-in function compile() in mode 'single' (GH-29934) (GH-30040)
ambv
webhook-mailer at python.org
Mon Dec 27 11:16:00 EST 2021
https://github.com/python/cpython/commit/576e38f9db61ca09ca6dc57ad13b02a7bf9d370a
commit: 576e38f9db61ca09ca6dc57ad13b02a7bf9d370a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-12-27T17:15:44+01:00
summary:
bpo-42918: Improve built-in function compile() in mode 'single' (GH-29934) (GH-30040)
Co-authored-by: Alex Waygood <Alex.Waygood at Gmail.com>
(cherry picked from commit 28179aac796ed1debdce336c4b8ca18e8475d40d)
Co-authored-by: Weipeng Hong <hongweichen8888 at sina.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-06-15-32-12.bpo-42918.Czpgtg.rst
M Lib/test/test_compile.py
M Parser/pegen.c
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 4de54488d84b6..5f80a583450ea 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -502,6 +502,7 @@ def test_single_statement(self):
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')
@@ -512,6 +513,7 @@ def test_bad_single_statement(self):
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
index 0000000000000..f03dadebcf3b3
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-06-15-32-12.bpo-42918.Czpgtg.rst
@@ -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.
diff --git a/Parser/pegen.c b/Parser/pegen.c
index bae073cb5b347..0504906c947d0 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -1131,31 +1131,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 (;;) {
More information about the Python-checkins
mailing list