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

Skip to content

Commit c416162

Browse files
committed
allow the keyword else immediately after (no space) an integer (closes #21642)
1 parent 024b2f5 commit c416162

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: XXXX-XX-XX
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
@@ -1597,15 +1597,24 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
15971597
} while (isdigit(c));
15981598
}
15991599
if (c == 'e' || c == 'E') {
1600-
exponent:
1600+
int e;
1601+
exponent:
1602+
e = c;
16011603
/* Exponent part */
16021604
c = tok_nextc(tok);
1603-
if (c == '+' || c == '-')
1605+
if (c == '+' || c == '-') {
16041606
c = tok_nextc(tok);
1605-
if (!isdigit(c)) {
1606-
tok->done = E_TOKEN;
1607+
if (!isdigit(c)) {
1608+
tok->done = E_TOKEN;
1609+
tok_backup(tok, c);
1610+
return ERRORTOKEN;
1611+
}
1612+
} else if (!isdigit(c)) {
16071613
tok_backup(tok, c);
1608-
return ERRORTOKEN;
1614+
tok_backup(tok, e);
1615+
*p_start = tok->start;
1616+
*p_end = tok->cur;
1617+
return NUMBER;
16091618
}
16101619
do {
16111620
c = tok_nextc(tok);

0 commit comments

Comments
 (0)