[pypy-svn] r49420 - in pypy/branch/clr-module-improvements/pypy/module/clr: . test

regmee at codespeak.net regmee at codespeak.net
Wed Dec 5 20:57:19 CET 2007


Author: regmee
Date: Wed Dec  5 20:57:18 2007
New Revision: 49420

Modified:
   pypy/branch/clr-module-improvements/pypy/module/clr/app_clr.py
   pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py
   pypy/branch/clr-module-improvements/pypy/module/clr/test/test_clr.py
Log:
Fix for check if a class implements  IEnumerable and addition of test cases for iterations on ArrayList and Stack objects

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/app_clr.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/app_clr.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/app_clr.py	Wed Dec  5 20:57:18 2007
@@ -115,7 +115,7 @@
     obj.__cliobj__ = cliobj
     return obj
 
-def build_wrapper(namespace, classname, staticmethods, methods, properties, indexers):
+def build_wrapper(namespace, classname, staticmethods, methods, properties, indexers, hasIEnumerable):
     fullname = '%s.%s' % (namespace, classname)
     d = {'__cliclass__': fullname,
          '__module__': namespace}
@@ -124,12 +124,9 @@
     for name in methods:
         d[name] = MethodWrapper(name)
 
-    # check if there is GetEnumerator() method
-    # XXX: use .NET Reflection for this, checking for method name is not safe
-    for method in methods:
-        if method == "GetEnumerator":
-            # now add the __iter__ method to the class 
-            d['__iter__'] = __iter__
+    # check if IEnumerable is implemented
+    if hasIEnumerable:
+        d['__iter__'] = __iter__
 
     assert len(indexers) <= 1
     if indexers:

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py	Wed Dec  5 20:57:18 2007
@@ -206,6 +206,15 @@
 
 def build_cli_class(space, namespace, classname, fullname):
     b_type = System.Type.GetType(fullname)
+    hasIEnumerable = 0      # flag 
+
+    # this is where we locate the interfaces inherited by the class
+    # set the flag hasIEnumerable if IEnumerable interface has been by the class
+    ifaces = b_type.GetInterfaces()
+    for interface in ifaces:
+        if interface.ToString() == "System.Collections.IEnumerable":
+            hasIEnumerable = 1 
+
     w_staticmethods, w_methods = get_methods(space, b_type)
     w_properties, w_indexers = get_properties(space, b_type)
     return build_wrapper(space,
@@ -214,7 +223,8 @@
                          w_staticmethods,
                          w_methods,
                          w_properties,
-                         w_indexers)
+                         w_indexers,
+                         space.wrap(hasIEnumerable))
 
 
 class W_CliObject(Wrappable):

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/test/test_clr.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/test/test_clr.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/test/test_clr.py	Wed Dec  5 20:57:18 2007
@@ -158,6 +158,8 @@
 
     def test_iteratrion(self):
         import clr
+
+        # test iteration in ArrayList
         ArrayList = clr.load_cli_class('System.Collections', 'ArrayList')
         x = ArrayList()
         x.Add(1)
@@ -169,3 +171,20 @@
            sum += i
         assert sum == 1+2+3+4
 
+        # test iteration in Stack
+        Stack = clr.load_cli_class('System.Collections', 'Stack')
+        obj = Stack()
+        obj.Push(1)
+        obj.Push(54)
+        obj.Push(21)
+        for i in obj:
+            sum += i
+        assert sum == 1+54+21
+
+
+
+
+
+
+
+



More information about the Pypy-commit mailing list