Skip to content
Permalink
Browse files

Merge pull request #698 from kostya/subscript

fix #661 simplify array subscript access nil check
  • Loading branch information
alex committed May 13, 2013
2 parents 14b8b76 + 6b6b67a commit 3a7b62a41e476868a7e05ab4ee9d165918d600a0
@@ -1,9 +1,3 @@
fails:Array#[] raises a RangeError when the start index is out of range of Fixnum
fails:Array#[] raises a RangeError when the length is out of range of Fixnum
fails:Array#[] with a subclass of Array returns a subclass instance with [n, m]
fails:Array#[] with a subclass of Array returns a subclass instance with [-n, m]
fails:Array#[] with a subclass of Array returns a subclass instance with [n..m]
fails:Array#[] with a subclass of Array returns a subclass instance with [n...m]
fails:Array#[] with a subclass of Array returns a subclass instance with [-n..-m]
fails:Array#[] with a subclass of Array returns a subclass instance with [-n...-m]
fails:Array.[] can unpack 2 or more nested referenced array
@@ -1,8 +1,2 @@
fails:Array#slice raises a RangeError when the start index is out of range of Fixnum
fails:Array#slice raises a RangeError when the length is out of range of Fixnum
fails:Array#slice with a subclass of Array returns a subclass instance with [n, m]
fails:Array#slice with a subclass of Array returns a subclass instance with [-n, m]
fails:Array#slice with a subclass of Array returns a subclass instance with [n..m]
fails:Array#slice with a subclass of Array returns a subclass instance with [n...m]
fails:Array#slice with a subclass of Array returns a subclass instance with [-n..-m]
fails:Array#slice with a subclass of Array returns a subclass instance with [-n...-m]
@@ -75,7 +75,7 @@ def method_subscript(self, space, w_idx, w_count=None):
elif as_range:
assert start >= 0
assert end >= 0
return space.newarray(self.items_w[start:end])
return W_ArrayObject(space, self.items_w[start:end], space.getnonsingletonclass(self))
else:
return self.items_w[start]

@@ -87,7 +87,7 @@ def method_subscript_assign(self, space, w_idx, w_count_or_obj, w_obj=None):
w_count = w_count_or_obj
else:
w_obj = w_count_or_obj
start, end, as_range, nil = space.subscript_access(len(self.items_w), w_idx, w_count=w_count)
start, end, as_range, _ = space.subscript_access(len(self.items_w), w_idx, w_count=w_count)

if w_count and end < start:
raise space.error(space.w_IndexError,
@@ -653,6 +653,7 @@ def subscript_access(self, length, w_idx, w_count):
inclusive = False
as_range = False
end = 0
nil = False

if isinstance(w_idx, W_RangeObject) and not w_count:
start = self.int_w(self.convert_type(w_idx.w_start, self.w_fixnum, "to_int"))
@@ -665,9 +666,14 @@ def subscript_access(self, length, w_idx, w_count):
end = self.int_w(self.convert_type(w_count, self.w_fixnum, "to_int"))
if end >= 0:
as_range = True
else:
if start < 0:
start += length
return (start, end, False, True)

if start < 0:
start += length

if as_range:
if w_count:
end += start
@@ -679,9 +685,10 @@ def subscript_access(self, length, w_idx, w_count):
end = start
elif end > length:
end = length
nil = start < 0 or end < 0 or start > length
else:
nil = start < 0 or start >= length

nil = ((not as_range and start >= length) or
start < 0 or end < 0 or (start > 0 and start > length))
return (start, end, as_range, nil)

def convert_type(self, w_obj, w_cls, method, raise_error=True):

0 comments on commit 3a7b62a

Please sign in to comment.
You can’t perform that action at this time.