[pypy-commit] pypy simple-range-strategy: move sort to base class
squeaky
noreply at buildbot.pypy.org
Tue Mar 4 18:52:24 CET 2014
Author: Squeaky <squeaky_pl at gmx.com>
Branch: simple-range-strategy
Changeset: r69676:1e70d4d7d970
Date: 2014-03-04 12:43 +0100
http://bitbucket.org/pypy/pypy/changeset/1e70d4d7d970/
Log: move sort to base class
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -1088,15 +1088,20 @@
w_list.extend(w_any)
def reverse(self, w_list):
- # XXX this could be specialized for SimpleRange to promote to Range
self.switch_to_integer_strategy(w_list)
w_list.reverse()
+ def sort(self, w_list, reverse):
+ step = self.step(w_list)
+ if step > 0 and reverse or step < 0 and not reverse:
+ self.switch_to_integer_strategy(w_list)
+ w_list.sort(reverse)
+
class SimpleRangeListStrategy(BaseRangeListStrategy):
"""SimpleRangeListStrategy is used when a list is created using the range
method providing only positive length. The storage is a one element tuple
- with positive integer less than 2**31 - 1 storing length."""
+ with positive integer storing length."""
_applevel_repr = "simple_range"
@@ -1117,6 +1122,9 @@
def length(self, w_list):
return self.unerase(w_list.lstorage)[0]
+ def step(self, w_list):
+ return 1
+
def _getitem_unwrapped(self, w_list, i):
length = self.unerase(w_list.lstorage)
if 0 <= i < length:
@@ -1155,11 +1163,6 @@
self.switch_to_integer_strategy(w_list)
return w_list.pop(index)
- def sort(self, w_list, reverse):
- if reverse:
- self.switch_to_integer_strategy(w_list)
- w_list.sort(reverse)
-
class RangeListStrategy(BaseRangeListStrategy):
"""RangeListStrategy is used when a list is created using the range method.
@@ -1193,6 +1196,9 @@
def length(self, w_list):
return self.unerase(w_list.lstorage)[2]
+ def step(self, w_list):
+ return self.unerase(w_list.lstorage)[1]
+
def _getitem_unwrapped(self, w_list, i):
v = self.unerase(w_list.lstorage)
start = v[0]
@@ -1254,12 +1260,6 @@
self.switch_to_integer_strategy(w_list)
return w_list.pop(index)
- def sort(self, w_list, reverse):
- step = self.unerase(w_list.lstorage)[1]
- if step > 0 and reverse or step < 0 and not reverse:
- self.switch_to_integer_strategy(w_list)
- w_list.sort(reverse)
-
class AbstractUnwrappedStrategy(object):
diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -460,8 +460,12 @@
assert l.length() == 5
+ l = make_range_list(self.space, 0, 1, 1)
+ assert self.space.eq_w(l.pop(0), self.space.wrap(0))
-
+ l = make_range_list(self.space, 0, 1, 10)
+ l.sort(False)
+ assert isinstance(l.strategy, SimpleRangeListStrategy)
def test_keep_range(self):
# simple list
More information about the pypy-commit
mailing list