]> granicus.if.org Git - python/commitdiff
allow the keyword else immediately after (no space) an integer (closes #21642)
authorBenjamin Peterson <benjamin@python.org>
Sat, 7 Jun 2014 19:36:39 +0000 (12:36 -0700)
committerBenjamin Peterson <benjamin@python.org>
Sat, 7 Jun 2014 19:36:39 +0000 (12:36 -0700)
Lib/test/test_grammar.py
Misc/NEWS
Parser/tokenizer.c

index bba8820ab1e3d8eaf7fb451df2ef4310824e96d4..f012de1671069fa2cb27cb65fefe834879acfc3e 100644 (file)
@@ -80,6 +80,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 test_string_literals(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)
index acc76b4f6306f8017ee5aca9bc673a3a3fd224c8..8f05a13b19a57d4cddfb36e6416256a01cf1ea3e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ Release date: XXXX-XX-XX
 Core and Builtins
 -----------------
 
+- 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"
index 7283058fc88bf36ffbe859ce85c7da1acfabe3b6..22accd1061aeaffaeca1bfdf013404fa169b0853 100644 (file)
@@ -1597,15 +1597,24 @@ tok_get(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);