Author: Antonio Cuni <[email protected]>
Branch: unicode-strategies
Changeset: r58476:9cbe431d72f5
Date: 2012-10-26 21:18 +0200
http://bitbucket.org/pypy/pypy/changeset/9cbe431d72f5/

Log:    implement unicode set strategy

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
@@ -304,6 +304,8 @@
             strategy = self.space.fromcache(IntegerSetStrategy)
         elif type(w_key) is W_StringObject:
             strategy = self.space.fromcache(StringSetStrategy)
+        elif type(w_key) is W_UnicodeObject:
+            strategy = self.space.fromcache(UnicodeSetStrategy)
         else:
             strategy = self.space.fromcache(ObjectSetStrategy)
         w_set.strategy = strategy
@@ -714,6 +716,42 @@
     def iter(self, w_set):
         return StringIteratorImplementation(self.space, self, w_set)
 
+
+class UnicodeSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy):
+    erase, unerase = rerased.new_erasing_pair("unicode")
+    erase = staticmethod(erase)
+    unerase = staticmethod(unerase)
+
+    def get_empty_storage(self):
+        return self.erase({})
+
+    def get_empty_dict(self):
+        return {}
+
+    ## def listview_unicode(self, w_set):
+    ##     return self.unerase(w_set.sstorage).keys()
+
+    def is_correct_type(self, w_key):
+        return type(w_key) is W_UnicodeObject
+
+    def may_contain_equal_elements(self, strategy):
+        if strategy is self.space.fromcache(IntegerSetStrategy):
+            return False
+        if strategy is self.space.fromcache(EmptySetStrategy):
+            return False
+        return True
+
+    def unwrap(self, w_item):
+        return self.space.unicode_w(w_item)
+
+    def wrap(self, item):
+        return self.space.wrap(item)
+
+    def iter(self, w_set):
+        XXX
+        return StringIteratorImplementation(self.space, self, w_set)
+
+
 class IntegerSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy):
     erase, unerase = rerased.new_erasing_pair("integer")
     erase = staticmethod(erase)
@@ -932,6 +970,13 @@
         w_set.sstorage = strategy.get_storage_from_unwrapped_list(stringlist)
         return
 
+    unicodelist = space.listview_unicode(w_iterable)
+    if unicodelist is not None:
+        strategy = space.fromcache(UnicodeSetStrategy)
+        w_set.strategy = strategy
+        w_set.sstorage = strategy.get_storage_from_unwrapped_list(unicodelist)
+        return
+
     intlist = space.listview_int(w_iterable)
     if intlist is not None:
         strategy = space.fromcache(IntegerSetStrategy)
diff --git a/pypy/objspace/std/test/test_setstrategies.py 
b/pypy/objspace/std/test/test_setstrategies.py
--- a/pypy/objspace/std/test/test_setstrategies.py
+++ b/pypy/objspace/std/test/test_setstrategies.py
@@ -1,6 +1,7 @@
 from pypy.objspace.std.setobject import W_SetObject
 from pypy.objspace.std.setobject import (IntegerSetStrategy, ObjectSetStrategy,
-                                         EmptySetStrategy, StringSetStrategy)
+                                         EmptySetStrategy, StringSetStrategy,
+                                         UnicodeSetStrategy)
 from pypy.objspace.std.listobject import W_ListObject
 
 class TestW_SetStrategies:
@@ -24,6 +25,9 @@
         s = W_SetObject(self.space, self.wrapped(["a", "b"]))
         assert s.strategy is self.space.fromcache(StringSetStrategy)
 
+        s = W_SetObject(self.space, self.wrapped([u"a", u"b"]))
+        assert s.strategy is self.space.fromcache(UnicodeSetStrategy)
+
     def test_switch_to_object(self):
         s = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
         s.add(self.space.wrap("six"))
@@ -34,6 +38,11 @@
         s1.update(s2)
         assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
 
+    def test_switch_to_unicode(self):
+        s = W_SetObject(self.space, self.wrapped([]))
+        s.add(self.space.wrap(u"six"))
+        assert s.strategy is self.space.fromcache(UnicodeSetStrategy)
+
     def test_symmetric_difference(self):
         s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
         s2 = W_SetObject(self.space, self.wrapped(["six", "seven"]))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to