[pypy-svn] r25368 - in pypy/dist/pypy: annotation annotation/test rpython rpython/test

pedronis at codespeak.net pedronis at codespeak.net
Wed Apr 5 13:32:13 CEST 2006


Author: pedronis
Date: Wed Apr  5 13:32:12 2006
New Revision: 25368

Modified:
   pypy/dist/pypy/annotation/test/test_annrpython.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rdict.py
   pypy/dist/pypy/rpython/test/test_rdict.py
Log:
implemented setdefault in RPython



Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py	Wed Apr  5 13:32:12 2006
@@ -495,6 +495,19 @@
         assert isinstance(s_key, annmodel.SomeString)
         assert isinstance(s_value, annmodel.SomeInteger)
 
+    def test_dict_setdefault(self):
+        a = self.RPythonAnnotator()
+        def f():
+            d = {}
+            d.setdefault('a', 2)
+            d.setdefault('a', -3)
+            return d
+        s = a.build_types(f, [])
+        assert isinstance(s, annmodel.SomeDict)
+        assert isinstance(dictkey(s), annmodel.SomeString)
+        assert isinstance(dictvalue(s), annmodel.SomeInteger)
+        assert not dictvalue(s).nonneg
+        
     def test_exception_deduction(self):
         a = self.RPythonAnnotator()
         s = a.build_types(snippet.exception_deduction, [])

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Wed Apr  5 13:32:12 2006
@@ -327,6 +327,8 @@
         dct.dictdef.generalize_value(dfl)
         return dct.dictdef.read_value()
 
+    method_setdefault = method_get
+
     def method_copy(dct):
         return SomeDict(dct.dictdef)
 

Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py	(original)
+++ pypy/dist/pypy/rpython/rdict.py	Wed Apr  5 13:32:12 2006
@@ -274,6 +274,13 @@
         v_res = hop.gendirectcall(ll_get, v_dict, v_key, v_default)
         return self.recast_value(hop.llops, v_res)
 
+    def rtype_method_setdefault(self, hop):
+        v_dict, v_key, v_default = hop.inputargs(self, self.key_repr,
+                                                 self.value_repr)
+        hop.exception_cannot_occur()
+        v_res = hop.gendirectcall(ll_setdefault, v_dict, v_key, v_default)
+        return self.recast_value(hop.llops, v_res)
+    
     def rtype_method_copy(self, hop):
         v_dict, = hop.inputargs(self)
         hop.exception_cannot_occur()
@@ -706,6 +713,14 @@
     else: 
         return default
 
+def ll_setdefault(dict, key, default):
+    entry = ll_dict_lookup(dict, key, dict.keyhash(key))
+    if entry.valid():
+        return entry.value
+    else:
+        ll_dict_setitem(dict, key, default)
+        return default
+
 def ll_copy(dict):
     DICT = lltype.typeOf(dict).TO
     dictsize = len(dict.entries)

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	Wed Apr  5 13:32:12 2006
@@ -234,6 +234,22 @@
     res = interpret(func, ())
     assert res == 422
 
+def test_dict_setdefault():
+    def f():
+        d = {}
+        d.setdefault('a', 2)
+        return d['a']
+    res = interpret(f, ())
+    assert res == 2
+
+    def f():
+        d = {}
+        d.setdefault('a', 2)
+        x = d.setdefault('a', -3)
+        return x
+    res = interpret(f, ())
+    assert res == 2
+
 def test_dict_copy():
     def func():
         # XXX this does not work if we use chars, only!



More information about the Pypy-commit mailing list