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

tismer at codespeak.net tismer at codespeak.net
Sat Jun 25 17:15:41 CEST 2005


Author: tismer
Date: Sat Jun 25 17:15:40 2005
New Revision: 13866

Modified:
   pypy/dist/pypy/rpython/rdict.py
   pypy/dist/pypy/rpython/test/test_rdict.py
Log:
StrDictRepr.get implemented & tested

Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py	(original)
+++ pypy/dist/pypy/rpython/rdict.py	Sat Jun 25 17:15:40 2005
@@ -89,6 +89,20 @@
     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)
+
+"""
+            rdict: (easy) method_get (?)
+                   (easy) method_copy
+                   (easy) method_update (?)
+                   (easy) method_keys (?)
+                   (easy) method_values (?)
+                   (easy) method_items (?)
+"""
+
 class __extend__(pairtype(StrDictRepr, rmodel.StringRepr)): 
 
     def rtype_getitem((r_dict, r_string), hop):
@@ -283,3 +297,15 @@
             return key
     iter.index = index
     raise StopIteration
+
+# _____________________________________________________________
+# methods
+
+def ll_get(v_dict, v_key, v_default):
+    entry = ll_strdict_lookup(v_dict, v_key) 
+    if entry.key and entry.key != deleted_entry_marker: 
+        return entry.value
+    else: 
+        return v_default
+
+

Modified: pypy/dist/pypy/rpython/test/test_rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rdict.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rdict.py	Sat Jun 25 17:15:40 2005
@@ -160,4 +160,11 @@
         return d2['world']['hello'] 
     res = interpret(func, [5])
     assert res == 6
-    
+
+def test_dict_get():
+    def func():
+        # XXX shouldn't the get imply the type by its default?
+        dic = {'blah': 1}
+        return dic.get('hi', 42) * 10 + dic.get('blah', 2)
+    res = interpret(func, ())
+    assert res == 421



More information about the Pypy-commit mailing list