[pypy-commit] pypy set-strategies: added listview_str/int for setobjects to later create lists from sets without wrapping/unwrapping the elements

l.diekmann noreply at buildbot.pypy.org
Wed Jan 11 14:09:59 CET 2012


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: set-strategies
Changeset: r51219:764907052fed
Date: 2012-01-10 17:43 +0100
http://bitbucket.org/pypy/pypy/changeset/764907052fed/

Log:	added listview_str/int for setobjects to later create lists from
	sets without wrapping/unwrapping the elements

diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -438,11 +438,15 @@
     def listview_str(self, w_obj):
         if isinstance(w_obj, W_ListObject):
             return w_obj.getitems_str()
+        if isinstance(w_obj, W_SetObject):
+            return w_obj.listview_str()
         return None
 
     def listview_int(self, w_obj):
         if isinstance(w_obj, W_ListObject):
             return w_obj.getitems_int()
+        if isinstance(w_obj, W_SetObject):
+            return w_obj.listview_int()
         return None
 
     def sliceindices(self, w_slice, w_length):
diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -63,6 +63,7 @@
 
     # _____________ strategy methods ________________
 
+
     def clear(self):
         """ Removes all elements from the set. """
         self.strategy.clear(self)
@@ -87,6 +88,14 @@
         """ Returns a dict with all elements of the set. Needed only for switching to ObjectSetStrategy. """
         return self.strategy.getdict_w(self)
 
+    def listview_str(self):
+        """ If this is a string set return its contents as a list of uwnrapped strings. Otherwise return None. """
+        return self.strategy.listview_str(self)
+
+    def listview_int(self):
+        """ If this is an int set return its contents as a list of uwnrapped ints. Otherwise return None. """
+        return self.strategy.listview_int(self)
+
     def get_storage_copy(self):
         """ Returns a copy of the storage. Needed when we want to clone all elements from one set and
         put them into another. """
@@ -189,6 +198,12 @@
         """ Returns an empty storage (erased) object. Used to initialize an empty set."""
         raise NotImplementedError
 
+    def listview_str(self, w_set):
+        return None
+
+    def listview_int(self, w_set):
+        return None
+
     #def erase(self, storage):
     #    raise NotImplementedError
 
@@ -694,6 +709,9 @@
     def get_empty_dict(self):
         return {}
 
+    def listview_str(self, w_set):
+        return self.unerase(w_set.sstorage).keys()
+
     def is_correct_type(self, w_key):
         return type(w_key) is W_StringObject
 
@@ -724,6 +742,9 @@
     def get_empty_dict(self):
         return {}
 
+    def listview_int(self, w_set):
+        return self.unerase(w_set.sstorage).keys()
+
     def is_correct_type(self, w_key):
         from pypy.objspace.std.intobject import W_IntObject
         return type(w_key) is W_IntObject
diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py
--- a/pypy/objspace/std/test/test_setobject.py
+++ b/pypy/objspace/std/test/test_setobject.py
@@ -123,6 +123,19 @@
         # changed cached object, need to change it back for other tests to pass
         intstr.get_storage_from_list = tmp_func
 
+    def test_listview_str_int_on_set(self):
+        w = self.space.wrap
+
+        w_a = W_SetObject(self.space)
+        _initialize_set(self.space, w_a, w("abcdefg"))
+        assert sorted(self.space.listview_str(w_a)) == list("abcdefg")
+        assert self.space.listview_int(w_a) is None
+
+        w_b = W_SetObject(self.space)
+        _initialize_set(self.space, w_b, self.space.newlist([w(1),w(2),w(3),w(4),w(5)]))
+        assert sorted(self.space.listview_int(w_b)) == [1,2,3,4,5]
+        assert self.space.listview_str(w_b) is None
+
 class AppTestAppSetTest:
 
     def setup_class(self):


More information about the pypy-commit mailing list