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

hpk at codespeak.net hpk at codespeak.net
Sun Jun 26 14:11:35 CEST 2005


Author: hpk
Date: Sun Jun 26 14:11:34 2005
New Revision: 13939

Modified:
   pypy/dist/pypy/rpython/rconstantdict.py
   pypy/dist/pypy/rpython/rdict.py
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rdict.py
Log:
(hpk, arigo)

convert_const() for StrDictRepr.


Modified: pypy/dist/pypy/rpython/rconstantdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rconstantdict.py	(original)
+++ pypy/dist/pypy/rpython/rconstantdict.py	Sun Jun 26 14:11:34 2005
@@ -44,7 +44,8 @@
                                 ("entries", self.DICTENTRYARRAY)))
 
     def convert_const(self, dictobj):
-        dictobj = getattr(dictobj, '__self__', dictobj) # bound dict methods
+        # get object from bound dict methods
+        dictobj = getattr(dictobj, '__self__', dictobj)
         if not isinstance(dictobj, dict):
             raise TyperError("expected a dict: %r" % (dictobj,))
         try:

Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py	(original)
+++ pypy/dist/pypy/rpython/rdict.py	Sun Jun 26 14:11:34 2005
@@ -57,7 +57,7 @@
         else:
             self.value_repr = value_repr  
         self.dictvalue = dictvalue
-        #self.dict_cache = {}
+        self.dict_cache = {}
         # setup() needs to be called to finish this initialization
 
     def setup(self):
@@ -74,23 +74,25 @@
                                 ("num_pristine_entries", lltype.Signed), 
                                 ("entries", lltype.Ptr(self.DICTENTRYARRAY))))
 
-    #def convert_const(self, dictobj):
-    #    dictobj = getattr(dictobj, '__self__', dictobj) # for bound list methods
-    #    if not isinstance(dictobj, list):
-    #        raise TyperError("expected a list: %r" % (dictobj,))
-    #    try:
-    #        key = Constant(dictobj)
-    #        return self.list_cache[key]
-    #    except KeyError:
-    #        self.setup()
-    #        result = malloc(self.STRDICT, immortal=True)
-    #        self.list_cache[key] = result
-    #        result.items = malloc(self.STRDICT.items.TO, len(dictobj))
-    #        r_item = self.value_repr
-    #        for i in range(len(dictobj)):
-    #            x = dictobj[i]
-    #            result.items[i] = r_item.convert_const(x)
-    #        return result
+    def convert_const(self, dictobj):
+        # get object from bound dict methods
+        dictobj = getattr(dictobj, '__self__', dictobj) 
+        if not isinstance(dictobj, dict):
+            raise TyperError("expected a dict: %r" % (dictobj,))
+        try:
+            key = Constant(dictobj)
+            return self.dict_cache[key]
+        except KeyError:
+            self.setup()
+            l_dict = ll_newstrdict(self.lowleveltype) 
+            self.dict_cache[key] = l_dict 
+            r_key = string_repr 
+            r_value = self.value_repr
+            for dictkey, dictvalue in dictobj.items():
+                llkey = r_key.convert_const(dictkey)
+                llvalue = r_value.convert_const(dictvalue)
+                ll_strdict_setitem(l_dict, llkey, llvalue)
+            return l_dict 
 
     def rtype_len(self, hop):
         v_dict, = hop.inputargs(self)

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Sun Jun 26 14:11:34 2005
@@ -61,7 +61,8 @@
             self.LIST.become(GcStruct("list", ("items", Ptr(ITEMARRAY))))
 
     def convert_const(self, listobj):
-        listobj = getattr(listobj, '__self__', listobj) # for bound list methods
+        # get object from bound list method
+        listobj = getattr(listobj, '__self__', listobj)
         if not isinstance(listobj, list):
             raise TyperError("expected a list: %r" % (listobj,))
         try:

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	Sun Jun 26 14:11:34 2005
@@ -244,3 +244,13 @@
         return ' 4' in dic and ' 9' not in dic
     res = interpret(func, ())
     assert res is True
+
+def test_dict_contains_with_constant_dict():
+    dic = {'4':1000, ' 8':200}
+    def func(i):
+        return chr(i) in dic 
+    res = interpret(func, [ord('4')]) 
+    assert res is True
+    res = interpret(func, [1]) 
+    assert res is False 
+



More information about the Pypy-commit mailing list