Author: Armin Rigo <[email protected]>
Branch: stmgc-c7
Changeset: r75505:edeba38dd1ee
Date: 2015-01-23 20:38 +0100
http://bitbucket.org/pypy/pypy/changeset/edeba38dd1ee/
Log: hashtable.get(), hashtable.setdefault()
diff --git a/pypy/module/_stm/hashtable.py b/pypy/module/_stm/hashtable.py
--- a/pypy/module/_stm/hashtable.py
+++ b/pypy/module/_stm/hashtable.py
@@ -4,7 +4,7 @@
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from rpython.rlib import rstm
from rpython.rtyper.annlowlevel import cast_gcref_to_instance
@@ -40,6 +40,22 @@
gcref = self.h.get(key)
return space.newbool(not not gcref)
+ @unwrap_spec(key=int, w_default=WrappedDefault(None))
+ def get_w(self, space, key, w_default):
+ gcref = self.h.get(key)
+ if not gcref:
+ return w_default
+ return cast_gcref_to_instance(W_Root, gcref)
+
+ @unwrap_spec(key=int, w_default=WrappedDefault(None))
+ def setdefault_w(self, space, key, w_default):
+ gcref = self.h.get(key)
+ if not gcref:
+ gcref = cast_instance_to_gcref(w_default)
+ self.h.set(key, gcref)
+ return w_default
+ return cast_gcref_to_instance(W_Root, gcref)
+
def W_Hashtable___new__(space, w_subtype):
r = space.allocate_instance(W_Hashtable, w_subtype)
@@ -53,4 +69,6 @@
__setitem__ = interp2app(W_Hashtable.setitem_w),
__delitem__ = interp2app(W_Hashtable.delitem_w),
__contains__ = interp2app(W_Hashtable.contains_w),
+ get = interp2app(W_Hashtable.get_w),
+ setdefault = interp2app(W_Hashtable.setdefault_w),
)
diff --git a/pypy/module/_stm/test/test_hashtable.py
b/pypy/module/_stm/test/test_hashtable.py
--- a/pypy/module/_stm/test/test_hashtable.py
+++ b/pypy/module/_stm/test/test_hashtable.py
@@ -16,3 +16,20 @@
raises(KeyError, "h[42]")
assert h[42+65536] == "bar"
raises(KeyError, "del h[42]")
+
+ def test_get_setdefault(self):
+ import _stm
+ h = _stm.hashtable()
+ assert h.get(42) is None
+ assert h.get(-43, None) is None
+ assert h.get(44, 81) == 81
+ raises(KeyError, "h[42]")
+ raises(KeyError, "h[-43]")
+ raises(KeyError, "h[44]")
+ assert h.setdefault(42) is None
+ assert h[42] is None
+ assert h.setdefault(42, "81") is None
+ assert h[42] is None
+ assert h.setdefault(44, "-81") == "-81"
+ assert h[44] == "-81"
+ assert h[42] is None
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit