Author: Lukas Diekmann <[email protected]>
Branch: list-strategies
Changeset: r47432:f6705d4e93a3
Date: 2011-02-23 16:43 +0100
http://bitbucket.org/pypy/pypy/changeset/f6705d4e93a3/
Log: Implemented extend
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
@@ -105,6 +105,9 @@
def insert(self, index, w_item):
self.strategy.insert(self, index, w_item)
+ def extend(self, items_w):
+ self.strategy.extend(self, items_w)
+
registerimplementation(W_ListObject)
@@ -145,6 +148,9 @@
def insert(self, w_list, index, w_item):
raise NotImplementedError
+ def extend(self, w_list, items_w):
+ raise NotImplementedError
+
class EmptyListStrategy(ListStrategy):
def init_from_list_w(self, w_list, list_w):
assert len(list_w) == 0
@@ -195,6 +201,11 @@
assert index == 0
self.append(w_list, w_item)
+ def extend(self, w_list, items_w):
+ #XXX: would be faster if items_w was a W_List and we could get its
strategy
+ w_list.strategy = get_strategy_from_list_objects(items_w)
+ w_list.strategy.init_from_list_w(w_list, items_w)
+
class AbstractUnwrappedStrategy(ListStrategy):
def unwrap(self, w_obj):
# XXX override later
@@ -210,6 +221,9 @@
def is_correct_type(self, w_obj):
raise NotImplementedError("abstract base class")
+ def list_is_correct_type(self, w_list):
+ raise NotImplementedError("abstract base class")
+
def length(self, w_list):
return len(self.cast_from_void_star(w_list.storage))
@@ -252,6 +266,16 @@
w_list.strategy.init_from_list_w(w_list, list_w)
w_list.insert(index, w_item)
+ def extend(self, w_list, w_other):
+ list_w = self.cast_from_void_star(w_list.storage)
+ if self.list_is_correct_type(w_other):
+ list_w += w_other.getitems() # or
self.cast_from_void_star(w_other.storage) ?
+ return
+
+ w_list.strategy = ObjectListStrategy()
+ w_list.strategy.init_from_list_w(w_list, list_w)
+ w_list.extend(w_other)
+
def setitem(self, w_list, index, w_item):
list_w = self.cast_from_void_star(w_list.storage)
@@ -356,6 +380,9 @@
def is_correct_type(self, w_obj):
return True
+ def list_is_correct_type(self, w_list):
+ return True
+
def init_from_list_w(self, w_list, list_w):
w_list.storage = cast_to_void_star(list_w, "object")
@@ -367,6 +394,9 @@
def is_correct_type(self, w_obj):
return is_W_IntObject(w_obj)
+ def list_is_correct_type(self, w_list):
+ return type(self) == type(w_list.strategy)
+
def init_from_list_w(self, w_list, list_w):
w_list.storage = cast_to_void_star(list_w, "integer")
@@ -378,6 +408,9 @@
def is_correct_type(self, w_obj):
return is_W_StringObject(w_obj)
+ def list_is_correct_type(self, w_list):
+ return type(self) == type(w_list.strategy)
+
def init_from_list_w(self, w_list, list_w):
w_list.storage = cast_to_void_star(list_w, "string")
@@ -619,11 +652,12 @@
return space.w_None
def list_extend__List_List(space, w_list, w_other):
- w_list.wrappeditems += w_other.wrappeditems
+ w_list.extend(w_other)
return space.w_None
def list_extend__List_ANY(space, w_list, w_any):
- w_list.wrappeditems += space.listview(w_any)
+ w_other = W_ListObject(space.listview(w_any))
+ w_list.extend(w_other)
return space.w_None
# note that the default value will come back wrapped!!!
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit