[pypy-commit] pypy list-strategies: Preparation for wrapping/unwrapping in Strategies

l.diekmann noreply at buildbot.pypy.org
Fri Sep 23 13:11:49 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47445:12e339ff3b4a
Date: 2011-03-01 13:49 +0100
http://bitbucket.org/pypy/pypy/changeset/12e339ff3b4a/

Log:	Preparation for wrapping/unwrapping in Strategies

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
@@ -22,25 +22,25 @@
     return wrapper._content
 
 # don't know where to put this function, so it is global for now
-def get_strategy_from_list_objects(list_w):
+def get_strategy_from_list_objects(space, list_w):
     if list_w == []:
-        return EmptyListStrategy()
+        return EmptyListStrategy(space)
 
     # check for ints
     for e in list_w:
         if not is_W_IntObject(e):
             break
         if e is list_w[-1]:
-            return IntegerListStrategy()
+            return IntegerListStrategy(space)
 
     # check for ints
     for e in list_w:
         if not is_W_StringObject(e):
             break
         if e is list_w[-1]:
-            return StringListStrategy()
+            return StringListStrategy(space)
 
-    return ObjectListStrategy()
+    return ObjectListStrategy(space)
 
 def is_W_IntObject(w_object):
     from pypy.objspace.std.intobject import W_IntObject
@@ -53,9 +53,9 @@
 class W_ListObject(W_Object):
     from pypy.objspace.std.listtype import list_typedef as typedef
 
-    def __init__(w_self, wrappeditems):
+    def __init__(w_self, space, wrappeditems):
         assert isinstance(wrappeditems, list)
-        w_self.strategy = get_strategy_from_list_objects(wrappeditems)
+        w_self.strategy = get_strategy_from_list_objects(space, wrappeditems)
         w_self.strategy.init_from_list_w(w_self, wrappeditems)
 
     def __repr__(w_self):
@@ -126,6 +126,10 @@
 
 
 class ListStrategy(object):
+
+    def __init__(self, space):
+        self.space = space
+
     def init_from_list_w(self, w_list, list_w):
         raise NotImplementedError
 
@@ -222,13 +226,12 @@
         pass
 
 class AbstractUnwrappedStrategy(ListStrategy):
-    def unwrap(self, w_obj):
-        # XXX override later
-        return w_obj
 
-    def wrap(self, item):
-        # XXX override later
-        return item
+    def wrap(self, unwrapped):
+        raise NotImplementedError
+
+    def unwrap(self, wrapped):
+        raise NotImplementedError
 
     def cast_from_void_star(self, storage):
         raise NotImplementedError("abstract base class")
@@ -406,6 +409,12 @@
         self.cast_from_void_star(w_list.storage).reverse()
 
 class ObjectListStrategy(AbstractUnwrappedStrategy):
+    def unwrap(self, w_obj):
+        return w_obj
+
+    def wrap(self, item):
+        return item
+
     def cast_from_void_star(self, storage):
         return cast_from_void_star(storage, "object")
 
@@ -420,6 +429,12 @@
 
 class IntegerListStrategy(AbstractUnwrappedStrategy):
 
+    def wrap(self, intval):
+        return self.space.wrap(intval)
+
+    def unwrap(self, w_int):
+        return self.space.int_w(w_int)
+
     def cast_from_void_star(self, storage):
         return cast_from_void_star(storage, "integer")
 
@@ -434,6 +449,12 @@
 
 class StringListStrategy(AbstractUnwrappedStrategy):
 
+    def wrap(self, stringval):
+        return self.space.wrap(stringval)
+
+    def unwrap(self, w_string):
+        return self.space.str_w(w_string)
+
     def cast_from_void_star(self, storage):
         return cast_from_void_star(storage, "string")
 


More information about the pypy-commit mailing list