From ecf7b35c16408f329ee039320cc22ba6b95097ee Mon Sep 17 00:00:00 2001 From: isidentical Date: Sun, 1 Dec 2019 00:38:36 +0300 Subject: [PATCH 1/6] bpo-38673: dont switch to ps2 if the line starts with comment or whitespace --- .../2019-12-01-00-17-44.bpo-38673.K_Tze-.rst | 1 + Parser/tokenizer.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst new file mode 100644 index 00000000000000..7cc193f0d39a82 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst @@ -0,0 +1 @@ +In REPL, dont switch to PS2 if the line starts with comment or whitespace. diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 5763e47c4b00b3..3a54f3d0719816 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -874,8 +874,12 @@ tok_nextc(struct tok_state *tok) strcpy(newtok, buf); Py_DECREF(u); } - if (tok->nextprompt != NULL) - tok->prompt = tok->nextprompt; + if (tok->nextprompt != NULL) { + if (newtok != NULL && (*newtok == '#' || *newtok == ' ' || *newtok == '\t')) + tok->nextprompt = NULL; + else + tok->prompt = tok->nextprompt; + } if (newtok == NULL) tok->done = E_INTR; else if (*newtok == '\0') { @@ -1399,7 +1403,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) /* Newline */ if (c == '\n') { tok->atbol = 1; - if (blankline || tok->level > 0) { + if ((blankline || tok->level > 0) && + !(tok->prompt != NULL && tok->nextprompt == NULL)) { goto nextline; } *p_start = tok->start; From 5e632fb43ad7d62c3a91154711b33b01825020bf Mon Sep 17 00:00:00 2001 From: isidentical Date: Tue, 3 Dec 2019 16:30:46 +0300 Subject: [PATCH 2/6] use strchr to compare --- .../2019-12-01-00-17-44.bpo-38673.K_Tze-.rst | 2 +- Parser/tokenizer.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst index 7cc193f0d39a82..54b4ee688dfc75 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst @@ -1 +1 @@ -In REPL, dont switch to PS2 if the line starts with comment or whitespace. +In REPL mode, don't switch to PS2 if the line starts with comment or whitespace. Patch by Batuhan Taşkaya. diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 3a54f3d0719816..9aec33c9636061 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -875,10 +875,12 @@ tok_nextc(struct tok_state *tok) Py_DECREF(u); } if (tok->nextprompt != NULL) { - if (newtok != NULL && (*newtok == '#' || *newtok == ' ' || *newtok == '\t')) + if (newtok != NULL && strchr("# \t", *newtok)) { tok->nextprompt = NULL; - else + } + else { tok->prompt = tok->nextprompt; + } } if (newtok == NULL) tok->done = E_INTR; From 0b97cf34e7d7dc5afcb68c649e6495d891f870d0 Mon Sep 17 00:00:00 2001 From: isidentical Date: Sat, 7 Dec 2019 13:07:42 +0300 Subject: [PATCH 3/6] check on single --- Parser/tokenizer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 9aec33c9636061..88f1b0587e57eb 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1406,7 +1406,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) if (c == '\n') { tok->atbol = 1; if ((blankline || tok->level > 0) && - !(tok->prompt != NULL && tok->nextprompt == NULL)) { + !(tok->prompt != NULL && tok->nextprompt == NULL && + tok->lineno == 1)) { goto nextline; } *p_start = tok->start; From 0e0da91a35b0b7035bf1b201a32ed973d4420500 Mon Sep 17 00:00:00 2001 From: isidentical Date: Sun, 8 Dec 2019 11:16:54 +0300 Subject: [PATCH 4/6] apply suggestions --- Parser/tokenizer.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 88f1b0587e57eb..65d392205cdc98 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -874,14 +874,8 @@ tok_nextc(struct tok_state *tok) strcpy(newtok, buf); Py_DECREF(u); } - if (tok->nextprompt != NULL) { - if (newtok != NULL && strchr("# \t", *newtok)) { - tok->nextprompt = NULL; - } - else { - tok->prompt = tok->nextprompt; - } - } + if (tok->nextprompt != NULL) + tok->prompt = tok->nextprompt; if (newtok == NULL) tok->done = E_INTR; else if (*newtok == '\0') { @@ -1154,6 +1148,12 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) if (col == 0 && c == '\n' && tok->prompt != NULL) { blankline = 0; /* Let it through */ } + else if (tok->prompt != NULL && tok->lineno == 1) { + /* In interactive mode, if the first line contains + only spaces and a comment, let it through. */ + blankline = 0; + col = altcol = 0; + } else { blankline = 1; /* Ignore completely */ } @@ -1405,9 +1405,7 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) /* Newline */ if (c == '\n') { tok->atbol = 1; - if ((blankline || tok->level > 0) && - !(tok->prompt != NULL && tok->nextprompt == NULL && - tok->lineno == 1)) { + if (blankline || tok->level > 0) { goto nextline; } *p_start = tok->start; From 24d0caafd2860df03555d700c032056a655df854 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 8 Dec 2019 11:37:10 -0800 Subject: [PATCH 5/6] Tweak comment text --- Parser/tokenizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 65d392205cdc98..f84093dae5b62e 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1150,7 +1150,7 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) } else if (tok->prompt != NULL && tok->lineno == 1) { /* In interactive mode, if the first line contains - only spaces and a comment, let it through. */ + only spaces and/or a comment, let it through. */ blankline = 0; col = altcol = 0; } From 4d15dfb7b30cc9cafd0b77feabcdfe2a5c528754 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 8 Dec 2019 11:37:39 -0800 Subject: [PATCH 6/6] Tweak news message --- .../Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst index 54b4ee688dfc75..8f8cf88e5e2103 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-00-17-44.bpo-38673.K_Tze-.rst @@ -1 +1 @@ -In REPL mode, don't switch to PS2 if the line starts with comment or whitespace. Patch by Batuhan Taşkaya. +In REPL mode, don't switch to PS2 if the line starts with comment or whitespace. Based on work by Batuhan Taşkaya.