[pypy-svn] r37536 - in pypy/dist/pypy/module/_dotnet: . test

antocuni at codespeak.net antocuni at codespeak.net
Mon Jan 29 14:12:24 CET 2007


Author: antocuni
Date: Mon Jan 29 14:12:24 2007
New Revision: 37536

Modified:
   pypy/dist/pypy/module/_dotnet/app_dotnet.py
   pypy/dist/pypy/module/_dotnet/interp_dotnet.py
   pypy/dist/pypy/module/_dotnet/test/test_dotnet.py
Log:
Add support for passing parameters to constructors.



Modified: pypy/dist/pypy/module/_dotnet/app_dotnet.py
==============================================================================
--- pypy/dist/pypy/module/_dotnet/app_dotnet.py	(original)
+++ pypy/dist/pypy/module/_dotnet/app_dotnet.py	Mon Jan 29 14:12:24 2007
@@ -72,9 +72,9 @@
 class CliClassWrapper(object):
     __slots__ = ('__cliobj__',)
 
-    def __init__(self):
+    def __init__(self, *args):
         import _dotnet
-        self.__cliobj__ = _dotnet._CliObject_internal(self.__cliclass__)
+        self.__cliobj__ = _dotnet._CliObject_internal(self.__cliclass__, args)
 
 
 def build_wrapper(namespace, classname, staticmethods, methods, properties, indexers):

Modified: pypy/dist/pypy/module/_dotnet/interp_dotnet.py
==============================================================================
--- pypy/dist/pypy/module/_dotnet/interp_dotnet.py	(original)
+++ pypy/dist/pypy/module/_dotnet/interp_dotnet.py	Mon Jan 29 14:12:24 2007
@@ -20,6 +20,12 @@
         msg = 'Multiple overloads for %s could match' % name
         raise OperationError(space.w_TypeError, space.wrap(msg))
 
+def get_constructor(space, b_type, b_paramtypes):
+    try:
+        return b_type.GetConstructor(b_paramtypes)
+    except AmbiguousMatchException:
+        msg = 'Multiple overloads for %s could match' % name
+        raise OperationError(space.w_TypeError, space.wrap(msg))
 
 def rewrap_args(space, w_args, startfrom):
     args = space.unpackiterable(w_args)
@@ -78,16 +84,6 @@
         msg = "Can't convert object %s to Python" % str(b_obj.ToString())
         raise OperationError(space.w_TypeError, space.wrap(msg))
 
-
-class W_CliObject(Wrappable):
-    def __init__(self, space, b_obj):
-        self.space = space
-        self.b_obj = b_obj
-
-    def call_method(self, name, w_args, startfrom=0):
-        return call_method(self.space, self.b_obj, self.b_obj.GetType(), name, w_args, startfrom)
-    call_method.unwrap_spec = ['self', str, W_Root, int]
-
 def load_cli_class(space, namespace, classname):
     fullname = '%s.%s' % (namespace, classname)
     b_type = System.Type.GetType(fullname)
@@ -128,12 +124,30 @@
                          w_staticmethods, w_methods, w_properties, w_indexers)
 load_cli_class.unwrap_spec = [ObjSpace, str, str]
 
-def cli_object_new(space, w_subtype, typename):
+
+class W_CliObject(Wrappable):
+    def __init__(self, space, b_obj):
+        self.space = space
+        self.b_obj = b_obj
+
+    def call_method(self, name, w_args, startfrom=0):
+        return call_method(self.space, self.b_obj, self.b_obj.GetType(), name, w_args, startfrom)
+    call_method.unwrap_spec = ['self', str, W_Root, int]
+
+def cli_object_new(space, w_subtype, typename, w_args):
     b_type = System.Type.GetType(typename)
-    b_ctor = b_type.GetConstructor(init_array(System.Type))
-    b_obj = b_ctor.Invoke(init_array(System.Object))
+    b_args, b_paramtypes = rewrap_args(space, w_args, 0)
+    b_ctor = get_constructor(space, b_type, b_paramtypes)
+    #b_obj = b_ctor.Invoke(init_array(System.Object))
+    try:
+        b_obj = b_ctor.Invoke(b_args)
+    except TargetInvocationException, e:
+        b_inner = native_exc(e).get_InnerException()
+        message = str(b_inner.get_Message())
+        # TODO: use the appropriate exception, not StandardError
+        raise OperationError(space.w_StandardError, space.wrap(message))
     return space.wrap(W_CliObject(space, b_obj))
-cli_object_new.unwrap_spec = [ObjSpace, W_Root, str]
+cli_object_new.unwrap_spec = [ObjSpace, W_Root, str, W_Root]
 
 W_CliObject.typedef = TypeDef(
     '_CliObject_internal',

Modified: pypy/dist/pypy/module/_dotnet/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/module/_dotnet/test/test_dotnet.py	(original)
+++ pypy/dist/pypy/module/_dotnet/test/test_dotnet.py	Mon Jan 29 14:12:24 2007
@@ -7,7 +7,7 @@
 
     def test_cliobject(self):
         import _dotnet
-        obj = _dotnet._CliObject_internal('System.Collections.ArrayList')
+        obj = _dotnet._CliObject_internal('System.Collections.ArrayList', [])
         max_index = obj.call_method('Add', [42])
         assert max_index == 0
 
@@ -81,3 +81,9 @@
         res = Math.Abs(-42.0)
         assert res == 42.0
         assert type(res) is float
+
+    def test_constructor_args(self):
+        import _dotnet
+        ArrayList = _dotnet.load_cli_class('System.Collections', 'ArrayList')
+        obj = ArrayList(42)
+        assert obj.Capacity == 42


More information about the pypy-svn mailing list