[pypy-commit] pypy stmgc-c7: hashtable.keys(), values(), items()
arigo
noreply at buildbot.pypy.org
Sun Feb 1 11:39:29 CET 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: stmgc-c7
Changeset: r75624:d7857d4bcf41
Date: 2015-02-01 11:39 +0100
http://bitbucket.org/pypy/pypy/changeset/d7857d4bcf41/
Log: hashtable.keys(), values(), items()
diff --git a/pypy/module/pypystm/hashtable.py b/pypy/module/pypystm/hashtable.py
--- a/pypy/module/pypystm/hashtable.py
+++ b/pypy/module/pypystm/hashtable.py
@@ -7,6 +7,7 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from rpython.rlib import rstm
+from rpython.rlib.rarithmetic import intmask
from rpython.rtyper.annlowlevel import cast_gcref_to_instance
from rpython.rtyper.annlowlevel import cast_instance_to_gcref
@@ -59,6 +60,34 @@
def len_w(self, space):
return space.wrap(self.h.len())
+ def keys_w(self, space):
+ array, count = self.h.list()
+ try:
+ lst = [intmask(array[i].index) for i in range(count)]
+ finally:
+ self.h.freelist(array)
+ return space.newlist_int(lst)
+
+ def values_w(self, space):
+ array, count = self.h.list()
+ try:
+ lst_w = [cast_gcref_to_instance(W_Root, array[i].object)
+ for i in range(count)]
+ finally:
+ self.h.freelist(array)
+ return space.newlist(lst_w)
+
+ def items_w(self, space):
+ array, count = self.h.list()
+ try:
+ lst_w = [space.newtuple([
+ space.wrap(intmask(array[i].index)),
+ cast_gcref_to_instance(W_Root, array[i].object)])
+ for i in range(count)]
+ finally:
+ self.h.freelist(array)
+ return space.newlist(lst_w)
+
def W_Hashtable___new__(space, w_subtype):
r = space.allocate_instance(W_Hashtable, w_subtype)
@@ -76,4 +105,7 @@
setdefault = interp2app(W_Hashtable.setdefault_w),
__len__ = interp2app(W_Hashtable.len_w),
+ keys = interp2app(W_Hashtable.keys_w),
+ values = interp2app(W_Hashtable.values_w),
+ items = interp2app(W_Hashtable.items_w),
)
diff --git a/pypy/module/pypystm/test/test_hashtable.py b/pypy/module/pypystm/test/test_hashtable.py
--- a/pypy/module/pypystm/test/test_hashtable.py
+++ b/pypy/module/pypystm/test/test_hashtable.py
@@ -46,3 +46,12 @@
assert len(h) == 2
del h[42]
assert len(h) == 1
+
+ def test_keys_values_items(self):
+ import pypystm
+ h = pypystm.hashtable()
+ h[42] = "foo"
+ h[43] = "bar"
+ assert sorted(h.keys()) == [42, 43]
+ assert sorted(h.values()) == ["bar", "foo"]
+ assert sorted(h.items()) == [(42, "foo"), (43, "bar")]
More information about the pypy-commit
mailing list