[pypy-commit] pypy stmgc-c7: Add _stm.hashtable().

arigo noreply at buildbot.pypy.org
Wed Nov 12 11:46:39 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: stmgc-c7
Changeset: r74476:f0c0bada6f6a
Date: 2014-11-12 11:46 +0100
http://bitbucket.org/pypy/pypy/changeset/f0c0bada6f6a/

Log:	Add _stm.hashtable().

diff --git a/pypy/module/_stm/__init__.py b/pypy/module/_stm/__init__.py
--- a/pypy/module/_stm/__init__.py
+++ b/pypy/module/_stm/__init__.py
@@ -9,4 +9,5 @@
     interpleveldefs = {
         'local': 'local.STMLocal',
         'count': 'count.count',
+        'hashtable': 'hashtable.W_Hashtable',
     }
diff --git a/pypy/module/_stm/hashtable.py b/pypy/module/_stm/hashtable.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_stm/hashtable.py
@@ -0,0 +1,47 @@
+"""
+The class _stm.hashtable, mapping integers to objects.
+"""
+
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+
+from rpython.rlib import rstm
+from rpython.rtyper.annlowlevel import cast_gcref_to_instance
+from rpython.rtyper.annlowlevel import cast_instance_to_gcref
+
+
+class W_Hashtable(W_Root):
+
+    def __init__(self):
+        self.h = rstm.create_hashtable()
+
+    @unwrap_spec(key=int)
+    def getitem_w(self, space, key):
+        gcref = self.h.get(key)
+        if not gcref:
+            space.raise_key_error(space.wrap(key))
+        return cast_gcref_to_instance(W_Root, gcref)
+
+    @unwrap_spec(key=int)
+    def setitem_w(self, key, w_value):
+        gcref = cast_instance_to_gcref(w_value)
+        self.h.set(key, gcref)
+
+    @unwrap_spec(key=int)
+    def delitem_w(self, key):
+        self.h.set(key, rstm.NULL_GCREF)
+
+
+def W_Hashtable___new__(space, w_subtype):
+    r = space.allocate_instance(W_Hashtable, w_subtype)
+    r.__init__()
+    return space.wrap(r)
+
+W_Hashtable.typedef = TypeDef(
+    '_stm.hashtable',
+    __new__ = interp2app(W_Hashtable___new__),
+    __getitem__ = interp2app(W_Hashtable.getitem_w),
+    __setitem__ = interp2app(W_Hashtable.setitem_w),
+    __delitem__ = interp2app(W_Hashtable.delitem_w),
+    )
diff --git a/pypy/module/_stm/test/test_hashtable.py b/pypy/module/_stm/test/test_hashtable.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_stm/test/test_hashtable.py
@@ -0,0 +1,15 @@
+
+
+class AppTestHashtable:
+    spaceconfig = dict(usemodules=['_stm'])
+
+    def test_simple(self):
+        import _stm
+        h = _stm.hashtable()
+        h[42+65536] = "bar"
+        raises(KeyError, "h[42]")
+        h[42] = "foo"
+        assert h[42] == "foo"
+        del h[42]
+        raises(KeyError, "h[42]")
+        assert h[42+65536] == "bar"
diff --git a/rpython/rlib/rstm.py b/rpython/rlib/rstm.py
--- a/rpython/rlib/rstm.py
+++ b/rpython/rlib/rstm.py
@@ -216,9 +216,9 @@
     h.ll_raw_hashtable = llop.stm_hashtable_create(_STM_HASHTABLE_P, p)
     return h
 
+NULL_GCREF = lltype.nullptr(llmemory.GCREF.TO)
+
 class HashtableForTest(object):
-    _NULL = lltype.nullptr(llmemory.GCREF.TO)
-
     def __init__(self):
         self._content = {}      # dict {integer: GCREF}
 
@@ -227,7 +227,7 @@
 
     def get(self, key):
         assert type(key) is int
-        return self._content.get(key, self._NULL)
+        return self._content.get(key, NULL_GCREF)
 
     def set(self, key, value):
         assert type(key) is int


More information about the pypy-commit mailing list