Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement
diff --git a/Parser/parsetok.c b/Parser/parsetok.c
index f22ac67..9b2d730 100644
--- a/Parser/parsetok.c
+++ b/Parser/parsetok.c
@@ -224,6 +224,23 @@
     if (err_ret->error == E_DONE) {
         n = ps->p_tree;
         ps->p_tree = NULL;
+
+        /* 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.  */
+        if (start == single_input) {
+            char *cur = tok->cur;
+            char c = *tok->cur;
+
+            while (c == ' ' || c == '\t' || c == '\n' || c == '\014')
+                c = *++cur;
+
+            if (c && c != '#') {
+                err_ret->error = E_BADSINGLE;
+                n = NULL;
+            }
+        }
     }
     else
         n = NULL;