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

antocuni at codespeak.net antocuni at codespeak.net
Wed Dec 5 17:45:44 CET 2007


Author: antocuni
Date: Wed Dec  5 17:45:43 2007
New Revision: 49392

Added:
   pypy/branch/clr-module-improvements/pypy/module/clr/test/test_interp_clr.py   (contents, props changed)
Modified:
   pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py
   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:
now we can iterate over .NET objects that implement IEnumerable



Modified: pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py	Wed Dec  5 17:45:43 2007
@@ -16,8 +16,8 @@
         'load_cli_class': 'interp_clr.load_cli_class',
     }
 
-    def setup_after_space_initialization(self):
-        self.space.appexec([self], """(clr_module):
-            import sys
-            sys.meta_path.append(clr_module.dotnetimporter())
-            """)
+##    def setup_after_space_initialization(self):
+##        self.space.appexec([self], """(clr_module):
+##            import sys
+##            sys.meta_path.append(clr_module.dotnetimporter())
+##            """)

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 17:45:43 2007
@@ -9,7 +9,6 @@
 
     def __call__(self, *args):
         import clr
-        # now call the .NET static method  call_staticmethod in interp_cl 
         return clr.call_staticmethod(self.class_name, self.meth_name, args)
 
     def __repr__(self):
@@ -67,8 +66,9 @@
         return self.im_self.__cliobj__.call_method(self.im_name, args)
 
     def __repr__(self):
-        return '<bound CLI method %s.%s of %s>' % (self.im_self.__class__.__cliclass__, self.im_name, self.im_self)
-
+        return '<bound CLI method %s.%s of %s>' % (self.im_self.__class__.__cliclass__,
+                                                   self.im_name,
+                                                   self.im_self)
 
 class StaticProperty(object):
     def __init__(self, fget=None, fset=None):
@@ -86,31 +86,34 @@
         else:
             type.__setattr__(cls, name, value)
 
-#class Dummy(object):
-#    def __init__(self, iterObj):
-#        self.iterObj = iterObj
-#        self.index = 0 
-#
-#    def next(self):
-#        temp = self.index
-#        if self.index == self.iterObj.Count:
-#            raise StopIteration
-#        self.index = self.index + 1
-#        return self.iterObj.__getitem__(temp)
-
-
 class CliClassWrapper(object):
     __slots__ = ('__cliobj__',)
 
     def __init__(self, *args):
         import clr
         self.__cliobj__ = clr._CliObject_internal(self.__cliclass__, args)
-        print self.__cliobj__
-        print self.Count
 
-#    def __iter__(self):
-#        return Dummy(self)
-#        return Dummy(self.Count, self.__getitem__(self.Count - 1))
+
+class IEnumeratorWrapper(object):
+    def __init__(self, enumerator):
+        self.__enumerator__ = enumerator
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        if not self.__enumerator__.MoveNext():
+            raise StopIteration
+        return self.__enumerator__.Current
+
+# this method need to be attached only to classes that implements IEnumerable (see build_wrapper)
+def __iter__(self):
+    return IEnumeratorWrapper(self.GetEnumerator())
+
+def wrapper_from_cliobj(cls, cliobj):
+    obj = cls.__new__(cls)
+    obj.__cliobj__ = cliobj
+    return obj
 
 def build_wrapper(namespace, classname, staticmethods, methods, properties, indexers):
     fullname = '%s.%s' % (namespace, classname)
@@ -121,20 +124,17 @@
     for name in methods:
         d[name] = MethodWrapper(name)
 
-    # check if there is GetEnumerator() method 
+    # 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":
-            print "Enumerator found .. Hurray !!!!!"
             # now add the __iter__ method to the class 
-            d['__iter__'] = d['GetEnumerator']
-#            d['next'] = d['MoveNext']
-
+            d['__iter__'] = __iter__
 
     assert len(indexers) <= 1
     if indexers:
         name, getter, setter, is_static = indexers[0]
         assert not is_static
-        print " Indexers Tuple -----> (%s,%s,%s,%s) "%(name, getter, setter, is_static)
         if getter:
             d['__getitem__'] = d[getter]
         if setter:
@@ -144,8 +144,6 @@
     # we must add properties *after* the class has been created
     # because we need to store UnboundMethods as getters and setters
     for (name, getter, setter, is_static) in properties:
-        print " Properties Tuple -----> (%s,%s,%s,%s) "%(name, getter, setter, is_static)
-
         fget = None
         fset = None
         if getter:
@@ -159,8 +157,3 @@
         setattr(cls, name, prop)
 
     return cls
-
-
-
-
-

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 17:45:43 2007
@@ -109,8 +109,16 @@
         strval = unbox(b_obj, ootype.String)
         return space.wrap(strval)
     else:
-        msg = "Can't convert object %s to Python" % str(b_obj.ToString())
-        raise OperationError(space.w_TypeError, space.wrap(msg))
+        namespace, classname = split_fullname(b_type.ToString())
+        w_cls = load_cli_class(space, namespace, classname)
+        cliobj = W_CliObject(space, b_obj)
+        return wrapper_from_cliobj(space, w_cls, cliobj)
+
+def split_fullname(name):
+    lastdot = name.rfind('.')
+    if lastdot == -1:
+        return '', name
+    return name[:lastdot], name[lastdot+1:]
 
 def wrap_list_of_tuples(space, lst):
     list_w = []
@@ -188,9 +196,6 @@
 
        - classname: the name of the class in the specified namespace
          (e.g. ``ArrayList``).    """
-    #import sys
-    #for module in sys.modules:
-    #    print "mod ----> %s"%module
     fullname = '%s.%s' % (namespace, classname)
     w_cls = CliClassCache.get(fullname)
     if w_cls is None:
@@ -246,3 +251,4 @@
 app = ApplevelClass(file(app_clr).read())
 del path, app_clr
 build_wrapper = app.interphook("build_wrapper")
+wrapper_from_cliobj = app.interphook("wrapper_from_cliobj")

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 17:45:43 2007
@@ -153,12 +153,19 @@
         import clr
         ArrayList = clr.load_cli_class('System.Collections', 'ArrayList')
         x = ArrayList()
+        enum = x.GetEnumerator()
+        assert enum.MoveNext() is False
+
+    def test_iteratrion(self):
+        import clr
+        ArrayList = clr.load_cli_class('System.Collections', 'ArrayList')
+        x = ArrayList()
         x.Add(1)
-        x.Add(6)
-        x.Add(31)
         x.Add(2)
+        x.Add(3)
+        x.Add(4)
+        sum = 0
         for i in x:
-           print i 
-
-
+           sum += i
+        assert sum == 1+2+3+4
 

Added: pypy/branch/clr-module-improvements/pypy/module/clr/test/test_interp_clr.py
==============================================================================
--- (empty file)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/test/test_interp_clr.py	Wed Dec  5 17:45:43 2007
@@ -0,0 +1,9 @@
+from pypy.module.clr.interp_clr import split_fullname
+
+def test_split_fullname():
+    split = split_fullname
+    assert split('Foo') == ('', 'Foo')
+    assert split('System.Foo') == ('System', 'Foo')
+    assert split('System.Foo.Bar') == ('System.Foo', 'Bar')
+    assert split('System.Foo.A+B') == ('System.Foo', 'A+B')
+    assert split('System.') == ('System', '')



More information about the Pypy-commit mailing list