[Python-checkins] r74119 - in python/trunk: Lib/test/test_ast.py Misc/NEWS Python/ast.c

benjamin.peterson python-checkins at python.org
Mon Jul 20 22:28:09 CEST 2009


Author: benjamin.peterson
Date: Mon Jul 20 22:28:08 2009
New Revision: 74119

Log:
the Slice in x[::] has to have step as None to help the interpreter


Modified:
   python/trunk/Lib/test/test_ast.py
   python/trunk/Misc/NEWS
   python/trunk/Python/ast.c

Modified: python/trunk/Lib/test/test_ast.py
==============================================================================
--- python/trunk/Lib/test/test_ast.py	(original)
+++ python/trunk/Lib/test/test_ast.py	Mon Jul 20 22:28:08 2009
@@ -150,7 +150,8 @@
         slc = ast.parse("x[::]").body[0].value.slice
         self.assertIsNone(slc.upper)
         self.assertIsNone(slc.lower)
-        self.assertIsNone(slc.step)
+        self.assertTrue(isinstance(slc.step, ast.Name))
+        self.assertEqual(slc.step.id, "None")
 
     def test_from_import(self):
         im = ast.parse("from . import y").body[0]

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Jul 20 22:28:08 2009
@@ -44,9 +44,6 @@
 
 - Assignment to None using import statements now raises a SyntaxError.
 
-- In the slice AST type, the step field will always be None if a step expression
-  is not specified.
-
 - Issue #4547: When debugging a very large function, it was not always
   possible to update the lineno attribute of the current frame.
 

Modified: python/trunk/Python/ast.c
==============================================================================
--- python/trunk/Python/ast.c	(original)
+++ python/trunk/Python/ast.c	Mon Jul 20 22:28:08 2009
@@ -1471,7 +1471,21 @@
 
     ch = CHILD(n, NCH(n) - 1);
     if (TYPE(ch) == sliceop) {
-        if (NCH(ch) != 1) {
+        if (NCH(ch) == 1) {
+            /* 
+              This is an extended slice (ie "x[::]") with no expression in the
+              step field. We set this literally to "None" in order to
+              disambiguate it from x[:]. (The interpreter might have to call
+              __getslice__ for x[:], but it must call __getitem__ for x[::].)
+            */
+            identifier none = new_identifier("None", c->c_arena);
+            if (!none)
+                return NULL;
+            ch = CHILD(ch, 0);
+            step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
+            if (!step)
+                return NULL;
+        } else {
             ch = CHILD(ch, 1);
             if (TYPE(ch) == test) {
                 step = ast_for_expr(c, ch);


More information about the Python-checkins mailing list