Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa21bf0

Browse files
committed
Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement
1 parent 00c7f85 commit fa21bf0

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

Include/errcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#define E_EOLS 24 /* EOL in single-quoted string */
3131
#define E_LINECONT 25 /* Unexpected characters after a line continuation */
3232
#define E_IDENTIFIER 26 /* Invalid characters in identifier */
33+
#define E_BADSINGLE 27 /* Ill-formed single statement input */
3334

3435
#ifdef __cplusplus
3536
}

Lib/test/test_compile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
class TestSpecifics(unittest.TestCase):
88

9+
def compile_single(self, source):
10+
compile(source, "<single>", "single")
11+
12+
def assertInvalidSingle(self, source):
13+
self.assertRaises(SyntaxError, self.compile_single, source)
14+
915
def test_no_ending_newline(self):
1016
compile("hi", "<test>", "exec")
1117
compile("hi\r", "<test>", "exec")
@@ -442,6 +448,28 @@ def test_same_filename_used(self):
442448
if isinstance(obj, types.CodeType):
443449
self.assertIs(obj.co_filename, c.co_filename)
444450

451+
def test_single_statement(self):
452+
self.compile_single("1 + 2")
453+
self.compile_single("\n1 + 2")
454+
self.compile_single("1 + 2\n")
455+
self.compile_single("1 + 2\n\n")
456+
self.compile_single("1 + 2\t\t\n")
457+
self.compile_single("1 + 2\t\t\n ")
458+
self.compile_single("1 + 2 # one plus two")
459+
self.compile_single("1; 2")
460+
self.compile_single("import sys; sys")
461+
self.compile_single("def f():\n pass")
462+
self.compile_single("while False:\n pass")
463+
self.compile_single("if x:\n f(x)")
464+
self.compile_single("if x:\n f(x)\nelse:\n g(x)")
465+
self.compile_single("class T:\n pass")
466+
467+
def test_bad_single_statement(self):
468+
self.assertInvalidSingle('1\n2')
469+
self.assertInvalidSingle('def f(): pass')
470+
self.assertInvalidSingle('a = 13\nb = 187')
471+
self.assertInvalidSingle('del x\ndel y')
472+
self.assertInvalidSingle('f()\ng()')
445473

446474
def test_main():
447475
support.run_unittest(TestSpecifics)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #12705: A SyntaxError exception is now raised when attempting to
14+
compile multiple statements as a single interactive statement.
15+
1316
- Fix the builtin module initialization code to store the init function for
1417
future reinitialization.
1518

Parser/parsetok.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,23 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
224224
if (err_ret->error == E_DONE) {
225225
n = ps->p_tree;
226226
ps->p_tree = NULL;
227+
228+
/* Check that the source for a single input statement really
229+
is a single statement by looking at what is left in the
230+
buffer after parsing. Trailing whitespace and comments
231+
are OK. */
232+
if (start == single_input) {
233+
char *cur = tok->cur;
234+
char c = *tok->cur;
235+
236+
while (c == ' ' || c == '\t' || c == '\n' || c == '\014')
237+
c = *++cur;
238+
239+
if (c && c != '#') {
240+
err_ret->error = E_BADSINGLE;
241+
n = NULL;
242+
}
243+
}
227244
}
228245
else
229246
n = NULL;

Python/pythonrun.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,9 @@ err_input(perrdetail *err)
21292129
case E_IDENTIFIER:
21302130
msg = "invalid character in identifier";
21312131
break;
2132+
case E_BADSINGLE:
2133+
msg = "multiple statements found while compiling a single statement";
2134+
break;
21322135
default:
21332136
fprintf(stderr, "error=%d\n", err->error);
21342137
msg = "unknown parsing error";

0 commit comments

Comments
 (0)