[pypy-svn] r26461 - in pypy/dist/pypy/translator/cli: . src test

antocuni at codespeak.net antocuni at codespeak.net
Thu Apr 27 21:50:34 CEST 2006


Author: antocuni
Date: Thu Apr 27 21:50:24 2006
New Revision: 26461

Modified:
   pypy/dist/pypy/translator/cli/cts.py
   pypy/dist/pypy/translator/cli/record.py
   pypy/dist/pypy/translator/cli/src/pypylib.cs
   pypy/dist/pypy/translator/cli/test/test_dict.py
Log:
Added support for dict iteration.



Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py	(original)
+++ pypy/dist/pypy/translator/cli/cts.py	Thu Apr 27 21:50:24 2006
@@ -18,6 +18,7 @@
 
 PYPY_LIST = '[pypylib]pypy.runtime.List`1<%s>'
 PYPY_DICT = '[pypylib]pypy.runtime.Dict`2<%s, %s>'
+PYPY_DICT_ITEMS_ITERATOR = '[pypylib]pypy.runtime.DictItemsIterator`2<%s, %s>'
 
 _lltype_to_cts = {
     ootype.Void: 'void',
@@ -35,6 +36,9 @@
     ootype.Dict.SELFTYPE_T: 'class ' + (PYPY_DICT % ('!0', '!1')),
     ootype.Dict.KEYTYPE_T: '!0',
     ootype.Dict.VALUETYPE_T: '!1',
+    ootype.DictItemsIterator.SELFTYPE_T: 'class ' + (PYPY_DICT_ITEMS_ITERATOR % ('!0', '!1')),
+    ootype.DictItemsIterator.KEYTYPE_T: '!0',
+    ootype.DictItemsIterator.VALUETYPE_T: '!1',
     }
 
 _pyexception_to_cts = {
@@ -79,6 +83,10 @@
             key_type = self.lltype_to_cts(t._KEYTYPE)
             value_type = self.lltype_to_cts(t._VALUETYPE)
             return self.__class(PYPY_DICT % (key_type, value_type), include_class)
+        elif isinstance(t, ootype.DictItemsIterator):
+            key_type = self.lltype_to_cts(t._KEYTYPE)
+            value_type = self.lltype_to_cts(t._VALUETYPE)
+            return self.__class(PYPY_DICT_ITEMS_ITERATOR % (key_type, value_type), include_class)
 
         return _get_from_dict(_lltype_to_cts, t, 'Unknown type %s' % t)
 

Modified: pypy/dist/pypy/translator/cli/record.py
==============================================================================
--- pypy/dist/pypy/translator/cli/record.py	(original)
+++ pypy/dist/pypy/translator/cli/record.py	Thu Apr 27 21:50:24 2006
@@ -9,7 +9,7 @@
         self.cts = CTS(db)
         self.record = record
 
-        trans = string.maketrans('(),', '___')
+        trans = string.maketrans('<>(), ', '______')
         name = ['Record']
         for f_name, (f_type, f_default) in record._fields.iteritems():
             type_name = f_type._short_name().translate(trans)

Modified: pypy/dist/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/pypylib.cs	Thu Apr 27 21:50:24 2006
@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 
 namespace pypy.runtime
 {
@@ -112,5 +113,35 @@
         {
             this.Clear();
         }
+
+        public DictItemsIterator<TKey, TValue> ll_get_items_iterator()
+        {
+            return new DictItemsIterator<TKey, TValue>(this.GetEnumerator());
+        }
+    }
+
+    public class DictItemsIterator<TKey, TValue>
+    {
+        IEnumerator<KeyValuePair<TKey, TValue>> it;
+
+        public DictItemsIterator(IEnumerator<KeyValuePair<TKey, TValue>> it)
+        {
+            this.it = it;
+        }
+
+        public bool ll_go_next()
+        {
+            return it.MoveNext();
+        }
+
+        public TKey ll_current_key()
+        {
+            return it.Current.Key;
+        }
+
+        public TValue ll_current_value()
+        {
+            return it.Current.Value;
+        }
     }
 }

Modified: pypy/dist/pypy/translator/cli/test/test_dict.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dict.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dict.py	Thu Apr 27 21:50:24 2006
@@ -6,3 +6,12 @@
         d = {x: x+1, y: y+1}
         return d[x]
     check(func, [int, int], (42, 13))
+
+def test_iteration():
+    def func(x, y):
+        d = {x: x+1, y: y+1}
+        tot = 0
+        for value in d.itervalues():
+            tot += value
+        return tot
+    check(func, [int, int], (42, 13))



More information about the Pypy-commit mailing list