[pypy-commit] pypy set-strategies: implemented popitem on W_SetObject
l.diekmann
noreply at buildbot.pypy.org
Thu Nov 10 13:51:06 CET 2011
Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: set-strategies
Changeset: r49196:8a7f58f9e061
Date: 2011-07-28 14:04 +0200
http://bitbucket.org/pypy/pypy/changeset/8a7f58f9e061/
Log: implemented popitem on W_SetObject
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
@@ -136,6 +136,9 @@
def iter(self):
return self.strategy.iter(self)
+ def popitem(self):
+ return self.strategy.popitem(self)
+
class W_SetObject(W_BaseSetObject):
from pypy.objspace.std.settype import set_typedef as typedef
@@ -288,6 +291,10 @@
def iter(self, w_set):
return EmptyIteratorImplementation(self.space, w_set)
+ def popitem(self, w_set):
+ raise OperationError(self.space.w_KeyError,
+ self.space.wrap('pop from an empty set'))
+
class AbstractUnwrappedSetStrategy(object):
_mixin_ = True
@@ -557,6 +564,16 @@
w_set.switch_to_object_strategy(self.space)
w_set.update(w_other)
+ def popitem(self, w_set):
+ storage = self.cast_from_void_star(w_set.sstorage)
+ try:
+ result = storage.popitem()
+ except KeyError:
+ # strategy may still be the same even if dict is empty
+ raise OperationError(self.space.w_KeyError,
+ self.space.wrap('pop from an empty set'))
+ return self.wrap(result)
+
class IntegerSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy):
cast_to_void_star, cast_from_void_star = rerased.new_erasing_pair("integer")
cast_to_void_star = staticmethod(cast_to_void_star)
@@ -1030,6 +1047,7 @@
#XXX move this to strategy so we don't have to
# wrap all items only to get the first one
#XXX use popitem
+ return w_left.popitem()
for w_key in w_left.getkeys():
break
else:
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
@@ -165,6 +165,9 @@
raises(KeyError, "a.remove(6)")
def test_pop(self):
+ b = set()
+ raises(KeyError, "b.pop()")
+
a = set([1,2,3,4,5])
for i in xrange(5):
a.pop()
More information about the pypy-commit
mailing list