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

Skip to content

Commit 3e43979

Browse files
committed
merge 3.4 (#21642)
2 parents ce6c525 + c416162 commit 3e43979

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

Lib/test/test_grammar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def test_floats(self):
8080
x = .3e14
8181
x = 3.1e4
8282

83+
def test_float_exponent_tokenization(self):
84+
# See issue 21642.
85+
self.assertEqual(1 if 1else 0, 1)
86+
self.assertEqual(1 if 0else 0, 0)
87+
self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
88+
8389
def test_string_literals(self):
8490
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
8591
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #21642: If the conditional if-else expression, allow an integer written
14+
with no space between itself and the ``else`` keyword (e.g. ``True if 42else
15+
False``) to be valid syntax.
16+
1317
- Issue #21523: Fix over-pessimistic computation of the stack effect of
1418
some opcodes in the compiler. This also fixes a quadratic compilation
1519
time issue noticeable when compiling code with a large number of "and"

Parser/tokenizer.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,15 +1603,24 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
16031603
} while (isdigit(c));
16041604
}
16051605
if (c == 'e' || c == 'E') {
1606-
exponent:
1606+
int e;
1607+
exponent:
1608+
e = c;
16071609
/* Exponent part */
16081610
c = tok_nextc(tok);
1609-
if (c == '+' || c == '-')
1611+
if (c == '+' || c == '-') {
16101612
c = tok_nextc(tok);
1611-
if (!isdigit(c)) {
1612-
tok->done = E_TOKEN;
1613+
if (!isdigit(c)) {
1614+
tok->done = E_TOKEN;
1615+
tok_backup(tok, c);
1616+
return ERRORTOKEN;
1617+
}
1618+
} else if (!isdigit(c)) {
16131619
tok_backup(tok, c);
1614-
return ERRORTOKEN;
1620+
tok_backup(tok, e);
1621+
*p_start = tok->start;
1622+
*p_end = tok->cur;
1623+
return NUMBER;
16151624
}
16161625
do {
16171626
c = tok_nextc(tok);

0 commit comments

Comments
 (0)