[pypy-svn] r14151 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Sun Jul 3 16:59:39 CEST 2005


Author: arigo
Date: Sun Jul  3 16:59:36 2005
New Revision: 14151

Modified:
   pypy/dist/pypy/rpython/rconstantdict.py
   pypy/dist/pypy/rpython/test/test_rconstantdict.py
Log:
The get() method of constant dictionaries.


Modified: pypy/dist/pypy/rpython/rconstantdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rconstantdict.py	(original)
+++ pypy/dist/pypy/rpython/rconstantdict.py	Sun Jul  3 16:59:36 2005
@@ -84,10 +84,10 @@
     #def make_iterator_repr(self):
     #    return StrDictIteratorRepr(self)
 
-    #def rtype_method_get(self, hop):
-    #    v_dict, v_key, v_default = hop.inputargs(self, string_repr,
-    #                                             self.value_repr)
-    #    return hop.gendirectcall(ll_get, v_dict, v_key, v_default)
+    def rtype_method_get(self, hop):
+        v_dict, v_key, v_default = hop.inputargs(self, self.key_repr,
+                                                 self.value_repr)
+        return hop.gendirectcall(ll_constantdict_get, v_dict, v_key, v_default)
 
 class __extend__(pairtype(ConstantDictRepr, rmodel.Repr)): 
 
@@ -125,6 +125,13 @@
     entry = ll_constantdict_lookup(d, key)#, hashcompute)
     return entry.valid
 
+def ll_constantdict_get(d, key, default):#, hashcompute):
+    entry = ll_constantdict_lookup(d, key)#, hashcompute)
+    if entry.valid:
+        return entry.value
+    else: 
+        return default
+
 def ll_constantdict_setnewitem(d, key, value):#, hashcompute): 
     entry = ll_constantdict_lookup(d, key)#, hashcompute)
     assert not entry.valid 

Modified: pypy/dist/pypy/rpython/test/test_rconstantdict.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rconstantdict.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rconstantdict.py	Sun Jul  3 16:59:36 2005
@@ -16,3 +16,12 @@
     assert res is False
     res = interpret(func, [4])
     assert res is True
+
+def test_constantdict_get():
+    d = {1: -11, 4: -44, 16: -66}
+    def func(i, j):
+        return d.get(i, j)
+    res = interpret(func, [15, 62])
+    assert res == 62
+    res = interpret(func, [4, 25])
+    assert res == -44



More information about the Pypy-commit mailing list