Skip to content

Commit

Permalink
Implemented Sliceobj and slice bltin as per #582, #364, #577, #434, #496
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielKluev committed Jun 26, 2011
1 parent d798191 commit b5d29a9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
6 changes: 1 addition & 5 deletions examples/libtest/BuiltinTest.py
Expand Up @@ -586,11 +586,7 @@ def testSlice(self):
self.assertEqual(s1, s2)
self.assertNotEqual(s1, s3, "slice() is mis-used, issue #582")
# members
try:
s = slice(1)
except Exception, e:
self.fail("slice() is mis-used, issue #582")
return False
s = slice(1)
self.assertEqual(s.start, None)
self.assertEqual(s.stop, 1)
self.assertEqual(s.step, None)
Expand Down
94 changes: 94 additions & 0 deletions pyjs/src/pyjs/builtin/pyjslib.py
Expand Up @@ -4283,6 +4283,100 @@ def __rmul__(self, n):
JS("@{{list}}.__str__ = @{{list}}.__repr__;")
JS("@{{list}}.toString = @{{list}}.__str__;")

class slice:
def __init__(self, a1, *args):
if args:
self.start = a1
self.stop = args[0]
if len(args) > 1:
self.step = args[1]
else:
self.step = None
else:
self.stop = a1
self.start = None
self.step = None

def __cmp__(self, x):
r = cmp(self.start, x.start)
if r != 0:
return r
r = cmp(self.stop, x.stop)
if r != 0:
return r
r = cmp(self.step, x.step)
return r

def indices(self, length):
"""
PySlice_GetIndicesEx at ./Objects/sliceobject.c
"""
step = 0
start = 0
stop = 0
if self.step is None:
step = 1
else:
step = self.step
if step == 0:
raise ValueError("slice step cannot be zero")

if step < 0:
defstart = length - 1
defstop = -1
else:
defstart = 0
defstop = length

if self.start is None:
start = defstart
else:
start = self.start
if start < 0:
start += length
if start < 0:
if step < 0:
start = -1
else:
start = 0
if start >= length:
if step < 0:
start = length - 1
else:
start = length

if self.stop is None:
stop = defstop
else:
stop = self.stop
if stop < 0:
stop += length
if stop < 0:
if step < 0:
stop = -1
else:
stop = 0
if stop >= length:
if step < 0:
stop = length - 1
else:
stop = length

if ((step < 0 and stop >= start)
or (step > 0 and start >= stop)):
slicelength = 0
elif step < 0:
slicelength = (stop - start + 1)/step + 1;
else:
slicelength = (stop - start - 1)/step + 1;

return (start, stop, step)

def __repr__(self):
return "slice(%s, %s, %s)" % (self.start, self.stop, self.step)

JS("@{{slice}}.__str__ = @{{slice}}.__repr__;")
JS("@{{slice}}.toString = @{{slice}}.__str__;")

class tuple:
def __init__(self, data=JS("[]")):
Expand Down
6 changes: 6 additions & 0 deletions pyjs/src/pyjs/translator.py
Expand Up @@ -3939,6 +3939,10 @@ def _dict(self, node, current_klass):

def _tuple(self, node, current_klass):
return self.track_call("$p['tuple']([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])", node.lineno)

def _sliceobj(self, node, current_klass):
args = ", ".join([self.expr(x, current_klass) for x in node.nodes])
return self.track_call("@{{slice}}(%s)" % args, node.lineno)

def _lambda(self, node, current_klass):
save_local_prefix, self.local_prefix = self.local_prefix, None
Expand Down Expand Up @@ -4215,6 +4219,8 @@ def expr(self, node, current_klass):
return self._dict(node, current_klass)
elif isinstance(node, self.ast.Tuple):
return self._tuple(node, current_klass)
elif isinstance(node, self.ast.Sliceobj):
return self._sliceobj(node, current_klass)
elif isinstance(node, self.ast.Slice):
return self._slice(node, current_klass)
elif isinstance(node, self.ast.Lambda):
Expand Down

0 comments on commit b5d29a9

Please sign in to comment.