Skip to content

Commit 4879c90

Browse files
committed
the Slice in x[::] has to have step as None to help the interpreter
1 parent a2514f4 commit 4879c90

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Lib/test/test_ast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def test_slice(self):
150150
slc = ast.parse("x[::]").body[0].value.slice
151151
self.assertIsNone(slc.upper)
152152
self.assertIsNone(slc.lower)
153-
self.assertIsNone(slc.step)
153+
self.assertTrue(isinstance(slc.step, ast.Name))
154+
self.assertEqual(slc.step.id, "None")
154155

155156
def test_from_import(self):
156157
im = ast.parse("from . import y").body[0]

Misc/NEWS

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ Core and Builtins
4444

4545
- Assignment to None using import statements now raises a SyntaxError.
4646

47-
- In the slice AST type, the step field will always be None if a step expression
48-
is not specified.
49-
5047
- Issue #4547: When debugging a very large function, it was not always
5148
possible to update the lineno attribute of the current frame.
5249

Python/ast.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,21 @@ ast_for_slice(struct compiling *c, const node *n)
14711471

14721472
ch = CHILD(n, NCH(n) - 1);
14731473
if (TYPE(ch) == sliceop) {
1474-
if (NCH(ch) != 1) {
1474+
if (NCH(ch) == 1) {
1475+
/*
1476+
This is an extended slice (ie "x[::]") with no expression in the
1477+
step field. We set this literally to "None" in order to
1478+
disambiguate it from x[:]. (The interpreter might have to call
1479+
__getslice__ for x[:], but it must call __getitem__ for x[::].)
1480+
*/
1481+
identifier none = new_identifier("None", c->c_arena);
1482+
if (!none)
1483+
return NULL;
1484+
ch = CHILD(ch, 0);
1485+
step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1486+
if (!step)
1487+
return NULL;
1488+
} else {
14751489
ch = CHILD(ch, 1);
14761490
if (TYPE(ch) == test) {
14771491
step = ast_for_expr(c, ch);

0 commit comments

Comments
 (0)