Skip to content

Commit 2ecddf4

Browse files
authored
Merge pull request #816 from dmamelin/fix-ast-slice
fix ast slices
2 parents 5d9fba4 + 6a66877 commit 2ecddf4

2 files changed

Lines changed: 12 additions & 33 deletions

File tree

custom_components/pyscript/eval.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,16 +1385,7 @@ async def recurse_assign(self, lhs, val):
13851385
val_idx += 1
13861386
elif isinstance(lhs, ast.Subscript):
13871387
var = await self.aeval(lhs.value)
1388-
if isinstance(lhs.slice, ast.Index):
1389-
ind = await self.aeval(lhs.slice.value)
1390-
var[ind] = val
1391-
elif isinstance(lhs.slice, ast.Slice):
1392-
lower = await self.aeval(lhs.slice.lower) if lhs.slice.lower else None
1393-
upper = await self.aeval(lhs.slice.upper) if lhs.slice.upper else None
1394-
step = await self.aeval(lhs.slice.step) if lhs.slice.step else None
1395-
var[slice(lower, upper, step)] = val
1396-
else:
1397-
var[await self.aeval(lhs.slice)] = val
1388+
var[await self.aeval(lhs.slice)] = val
13981389
else:
13991390
var_name = await self.aeval(lhs)
14001391
if isinstance(var_name, EvalAttrSet):
@@ -1455,21 +1446,7 @@ async def ast_delete(self, arg):
14551446
for arg1 in arg.targets:
14561447
if isinstance(arg1, ast.Subscript):
14571448
var = await self.aeval(arg1.value)
1458-
if isinstance(arg1.slice, ast.Index):
1459-
ind = await self.aeval(arg1.slice.value)
1460-
for elt in ind if isinstance(ind, list) else [ind]:
1461-
del var[elt]
1462-
elif isinstance(arg1.slice, ast.Slice):
1463-
lower, upper, step = None, None, None
1464-
if arg1.slice.lower:
1465-
lower = await self.aeval(arg1.slice.lower)
1466-
if arg1.slice.upper:
1467-
upper = await self.aeval(arg1.slice.upper)
1468-
if arg1.slice.step:
1469-
step = await self.aeval(arg1.slice.step)
1470-
del var[slice(lower, upper, step)]
1471-
else:
1472-
del var[await self.aeval(arg1.slice)]
1449+
del var[await self.aeval(arg1.slice)]
14731450
elif isinstance(arg1, ast.Name):
14741451
if self.curr_func and arg1.id in self.curr_func.global_names:
14751452
if arg1.id in self.global_sym_table:
@@ -1857,13 +1834,6 @@ async def ast_subscript(self, arg):
18571834
"""Evaluate subscript."""
18581835
var = await self.aeval(arg.value)
18591836
if isinstance(arg.ctx, ast.Load):
1860-
if isinstance(arg.slice, ast.Index):
1861-
return var[await self.aeval(arg.slice)]
1862-
if isinstance(arg.slice, ast.Slice):
1863-
lower = (await self.aeval(arg.slice.lower)) if arg.slice.lower else None
1864-
upper = (await self.aeval(arg.slice.upper)) if arg.slice.upper else None
1865-
step = (await self.aeval(arg.slice.step)) if arg.slice.step else None
1866-
return var[slice(lower, upper, step)]
18671837
return var[await self.aeval(arg.slice)]
18681838
return None
18691839

@@ -1873,7 +1843,10 @@ async def ast_index(self, arg):
18731843

18741844
async def ast_slice(self, arg):
18751845
"""Evaluate slice."""
1876-
return await self.aeval(arg.value)
1846+
lower = (await self.aeval(arg.lower)) if arg.lower else None
1847+
upper = (await self.aeval(arg.upper)) if arg.upper else None
1848+
step = (await self.aeval(arg.step)) if arg.step else None
1849+
return slice(lower, upper, step)
18771850

18781851
async def ast_call(self, arg):
18791852
"""Evaluate function call."""

tests/test_unit_eval.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
["[0, 1, 2, 3, 4, 5, 6, 7, 8][:4:]", [0, 1, 2, 3]],
114114
["[0, 1, 2, 3, 4, 5, 6, 7, 8][::2]", [0, 2, 4, 6, 8]],
115115
["[0, 1, 2, 3, 4, 5, 6, 7, 8][::]", [0, 1, 2, 3, 4, 5, 6, 7, 8]],
116+
["[0, 1, 2, 3, 4][4:1:-1]", [4, 3, 2]],
117+
["a = 1; b = 6; c = 2; [0, 1, 2, 3, 4, 5, 6][a:b:c]", [1, 3, 5]],
116118
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[1:5:2] = [6, 8]; z", [0, 6, 2, 8, 4, 5, 6, 7, 8]],
117119
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[1:5] = [10, 11]; z", [0, 10, 11, 5, 6, 7, 8]],
118120
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[1::3] = [10, 11, 12]; z", [0, 10, 2, 3, 11, 5, 6, 12, 8]],
@@ -121,6 +123,10 @@
121123
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[:6:2] = [10, 11, 12]; z", [10, 1, 11, 3, 12, 5, 6, 7, 8]],
122124
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[:4:] = [10, 11, 12, 13]; z", [10, 11, 12, 13, 4, 5, 6, 7, 8]],
123125
["z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[::2] = [10, 11, 12, 13, 14]; z", [10, 1, 11, 3, 12, 5, 13, 7, 14]],
126+
["x = [0, 1, 2, 3, 4]; x[1:4] = [9, 8]; x", [0, 9, 8, 4]],
127+
["x = [0, 1, 2, 3, 4, 5]; x[::2] = [9, 8, 7]; x", [9, 1, 8, 3, 7, 5]],
128+
["x = [0, 1, 2, 3, 4, 5]; del x[1:5:2]; x", [0, 2, 4, 5]],
129+
["x = [0, 1, 2, 3, 4]; del x[::-2]; x", [1, 3]],
124130
[
125131
"z = [0, 1, 2, 3, 4, 5, 6, 7, 8]; z[::] = [10, 11, 12, 13, 14, 15, 16, 17]; z",
126132
[10, 11, 12, 13, 14, 15, 16, 17],

0 commit comments

Comments
 (0)