[pypy-svn] r37526 - in pypy/dist/pypy/module/_dotnet: . test
antocuni at codespeak.net
antocuni at codespeak.net
Mon Jan 29 13:16:24 CET 2007
Author: antocuni
Date: Mon Jan 29 13:16:23 2007
New Revision: 37526
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 properties and indexers.
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 13:16:23 2007
@@ -77,7 +77,7 @@
self.__cliobj__ = _dotnet._CliObject_internal(self.__cliclass__)
-def build_wrapper(namespace, classname, staticmethods, methods):
+def build_wrapper(namespace, classname, staticmethods, methods, properties, indexers):
fullname = '%s.%s' % (namespace, classname)
d = {'__cliclass__': fullname,
'__module__': namespace}
@@ -85,4 +85,25 @@
d[name] = StaticMethodWrapper(fullname, name)
for name in methods:
d[name] = MethodWrapper(name)
- return type(classname, (CliClassWrapper,), d)
+
+ assert len(indexers) <= 1
+ if indexers:
+ name, getter, setter = indexers[0]
+ if getter:
+ d['__getitem__'] = d[getter]
+ if setter:
+ d['__setitem__'] = d[setter]
+ cls = type(classname, (CliClassWrapper,), d)
+
+ # we must add properties *after* the class has been created
+ # because we need to store UnboundMethods as getters and setters
+ for (name, getter, setter) in properties:
+ fget = None
+ fset = None
+ if getter:
+ fget = getattr(cls, getter)
+ if setter:
+ fset = getattr(cls, setter)
+ setattr(cls, name, property(fget, fset))
+
+ return cls
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 13:16:23 2007
@@ -45,7 +45,10 @@
message = str(b_inner.get_Message())
# TODO: use the appropriate exception, not StandardError
raise OperationError(space.w_StandardError, space.wrap(message))
- return cli2py(space, b_res)
+ if b_meth.ReturnType.Name == 'Void':
+ return space.w_None
+ else:
+ return cli2py(space, b_res)
def call_staticmethod(space, typename, methname, w_args):
b_type = System.Type.GetType(typename) # XXX: cache this!
@@ -87,21 +90,42 @@
def load_cli_class(space, namespace, classname):
fullname = '%s.%s' % (namespace, classname)
- t = System.Type.GetType(fullname)
+ b_type = System.Type.GetType(fullname)
methods = []
staticmethods = []
- methodsinfo = t.GetMethods()
- for i in range(len(methodsinfo)):
- meth = methodsinfo[i]
- if meth.IsPublic:
- if meth.IsStatic:
- staticmethods.append(str(meth.Name))
+ properties = []
+ indexers = []
+
+ b_methodinfos = b_type.GetMethods()
+ for i in range(len(b_methodinfos)):
+ b_meth = b_methodinfos[i]
+ if b_meth.IsPublic:
+ if b_meth.IsStatic:
+ staticmethods.append(str(b_meth.Name))
else:
- methods.append(str(meth.Name))
+ methods.append(str(b_meth.Name))
+
+ b_propertyinfos = b_type.GetProperties()
+ for i in range(len(b_propertyinfos)):
+ b_prop = b_propertyinfos[i]
+ get_name = None
+ set_name = None
+ if b_prop.CanRead:
+ get_name = b_prop.GetGetMethod().Name
+ if b_prop.CanWrite:
+ set_name = b_prop.GetSetMethod().Name
+ b_indexparams = b_prop.GetIndexParameters()
+ if len(b_indexparams) == 0:
+ properties.append((b_prop.Name, get_name, set_name))
+ else:
+ indexers.append((b_prop.Name, get_name, set_name))
+
w_staticmethods = space.wrap(staticmethods)
w_methods = space.wrap(methods)
+ w_properties = space.wrap(properties)
+ w_indexers = space.wrap(indexers)
return build_wrapper(space, space.wrap(namespace), space.wrap(classname),
- w_staticmethods, w_methods)
+ w_staticmethods, w_methods, w_properties, w_indexers)
load_cli_class.unwrap_spec = [ObjSpace, str, str]
def cli_object_new(space, w_subtype, typename):
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 13:16:23 2007
@@ -35,13 +35,21 @@
assert isinstance(item, float)
def test_getitem(self):
- skip('skip for now')
import _dotnet
ArrayList = _dotnet.load_cli_class('System.Collections', 'ArrayList')
obj = ArrayList()
obj.Add(42)
assert obj[0] == 42
+ def test_property(self):
+ import _dotnet
+ ArrayList = _dotnet.load_cli_class('System.Collections', 'ArrayList')
+ obj = ArrayList()
+ obj.Add(42)
+ assert obj.Count == 1
+ obj.Capacity = 10
+ assert obj.Capacity == 10
+
def test_unboundmethod(self):
import _dotnet
ArrayList = _dotnet.load_cli_class('System.Collections', 'ArrayList')
More information about the Pypy-commit
mailing list