From 93e51aac540f0dbd795e709a6fad0b9a62a5ae73 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 7 Jun 2014 12:36:39 -0700 Subject: [PATCH] allow the keyword else immediately after (no space) an integer (closes #21642) --- Lib/test/test_grammar.py | 6 ++++++ Misc/NEWS | 4 ++++ Parser/tokenizer.c | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 86cc0843eef..5f77c1d018b 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -75,6 +75,12 @@ class TokenTests(unittest.TestCase): x = .3e14 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): x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) diff --git a/Misc/NEWS b/Misc/NEWS index 6b5b4553cae..d7510b5df3a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,10 @@ Core and Builtins - Issue #19656: Running Python with the -3 option now also warns about 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 some opcodes in the compiler. This also fixes a quadratic compilation time issue noticeable when compiling code with a large number of "and" diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 249bb962065..16d311c83d7 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1500,15 +1500,24 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end) } while (isdigit(c)); } if (c == 'e' || c == 'E') { - exponent: + int e; + exponent: + e = c; /* Exponent part */ c = tok_nextc(tok); - if (c == '+' || c == '-') + if (c == '+' || c == '-') { c = tok_nextc(tok); - if (!isdigit(c)) { - tok->done = E_TOKEN; + if (!isdigit(c)) { + tok->done = E_TOKEN; + tok_backup(tok, c); + return ERRORTOKEN; + } + } else if (!isdigit(c)) { tok_backup(tok, c); - return ERRORTOKEN; + tok_backup(tok, e); + *p_start = tok->start; + *p_end = tok->cur; + return NUMBER; } do { c = tok_nextc(tok);