Commit 93e51aac authored by Benjamin Peterson's avatar Benjamin Peterson

allow the keyword else immediately after (no space) an integer (closes #21642)

parent 4807dd04
...@@ -75,6 +75,12 @@ class TokenTests(unittest.TestCase): ...@@ -75,6 +75,12 @@ class TokenTests(unittest.TestCase):
x = .3e14 x = .3e14
x = 3.1e4 x = 3.1e4
def test_float_exponent_tokenization(self):
# See issue 21642.
self.assertEqual(1 if 1else 0, 1)
self.assertEqual(1 if 0else 0, 0)
self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
def testStringLiterals(self): def testStringLiterals(self):
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
......
...@@ -13,6 +13,10 @@ Core and Builtins ...@@ -13,6 +13,10 @@ Core and Builtins
- Issue #19656: Running Python with the -3 option now also warns about - Issue #19656: Running Python with the -3 option now also warns about
non-ascii bytes literals. non-ascii bytes literals.
- Issue #21642: If the conditional if-else expression, allow an integer written
with no space between itself and the ``else`` keyword (e.g. ``True if 42else
False``) to be valid syntax.
- Issue #21523: Fix over-pessimistic computation of the stack effect of - Issue #21523: Fix over-pessimistic computation of the stack effect of
some opcodes in the compiler. This also fixes a quadratic compilation some opcodes in the compiler. This also fixes a quadratic compilation
time issue noticeable when compiling code with a large number of "and" time issue noticeable when compiling code with a large number of "and"
......
...@@ -1500,16 +1500,25 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end) ...@@ -1500,16 +1500,25 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
} while (isdigit(c)); } while (isdigit(c));
} }
if (c == 'e' || c == 'E') { if (c == 'e' || c == 'E') {
int e;
exponent: exponent:
e = c;
/* Exponent part */ /* Exponent part */
c = tok_nextc(tok); c = tok_nextc(tok);
if (c == '+' || c == '-') if (c == '+' || c == '-') {
c = tok_nextc(tok); c = tok_nextc(tok);
if (!isdigit(c)) { if (!isdigit(c)) {
tok->done = E_TOKEN; tok->done = E_TOKEN;
tok_backup(tok, c); tok_backup(tok, c);
return ERRORTOKEN; return ERRORTOKEN;
} }
} else if (!isdigit(c)) {
tok_backup(tok, c);
tok_backup(tok, e);
*p_start = tok->start;
*p_end = tok->cur;
return NUMBER;
}
do { do {
c = tok_nextc(tok); c = tok_nextc(tok);
} while (isdigit(c)); } while (isdigit(c));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment