[pypy-svn] r50785 - in pypy/dist/pypy/module/clr: . test
antocuni at codespeak.net
antocuni at codespeak.net
Sat Jan 19 17:56:59 CET 2008
Author: antocuni
Date: Sat Jan 19 17:56:59 2008
New Revision: 50785
Modified:
pypy/dist/pypy/module/clr/__init__.py
pypy/dist/pypy/module/clr/app_clr.py
pypy/dist/pypy/module/clr/interp_clr.py
pypy/dist/pypy/module/clr/test/test_clr.py
pypy/dist/pypy/module/clr/test/test_importer.py
Log:
classes loaded from external assemblies finally work! :-)
To do that I had to yak shave a lot, as confirmed by the lot of new
tests :-)
Modified: pypy/dist/pypy/module/clr/__init__.py
==============================================================================
--- pypy/dist/pypy/module/clr/__init__.py (original)
+++ pypy/dist/pypy/module/clr/__init__.py Sat Jan 19 17:56:59 2008
@@ -21,5 +21,6 @@
def startup(self, space):
self.space.appexec([self], """(clr_module):
import sys
+ clr_module.get_assemblies_info() # load info for std assemblies
sys.meta_path.append(clr_module.dotnetimporter())
""")
Modified: pypy/dist/pypy/module/clr/app_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/app_clr.py (original)
+++ pypy/dist/pypy/module/clr/app_clr.py Sat Jan 19 17:56:59 2008
@@ -131,7 +131,7 @@
def __init__(self, *args):
import clr
- self.__cliobj__ = clr._CliObject_internal(self.__cliclass__, args)
+ self.__cliobj__ = clr._CliObject_internal(self.__fullyqualifiedname__, args)
class IEnumeratorWrapper(object):
@@ -165,7 +165,7 @@
'__assemblyname__': assemblyname,
'__module__': namespace}
for name in staticmethods:
- d[name] = StaticMethodWrapper(fullname, name)
+ d[name] = StaticMethodWrapper(assembly_qualified_name, name)
for name in methods:
d[name] = MethodWrapper(name)
Modified: pypy/dist/pypy/module/clr/interp_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/interp_clr.py (original)
+++ pypy/dist/pypy/module/clr/interp_clr.py Sat Jan 19 17:56:59 2008
@@ -30,10 +30,14 @@
def get_constructor(space, b_type, b_paramtypes):
try:
- return b_type.GetConstructor(b_paramtypes)
+ ctor = b_type.GetConstructor(b_paramtypes)
except AmbiguousMatchException:
msg = 'Multiple constructors could match'
raise OperationError(space.w_TypeError, space.wrap(msg))
+ if ctor is None:
+ msg = 'No overloads for constructor could match'
+ raise OperationError(space.w_TypeError, space.wrap(msg))
+ return ctor
def rewrap_args(space, w_args, startfrom):
args = space.unpackiterable(w_args)
@@ -86,7 +90,20 @@
call_staticmethod.unwrap_spec = [ObjSpace, str, str, W_Root]
def py2cli(space, w_obj):
- return w_obj.tocli()
+ try:
+ cliobj = space.getattr(w_obj, space.wrap('__cliobj__'))
+ except OperationError, e:
+ if e.match(space, space.w_AttributeError):
+ # it hasn't got a __cloobj__
+ return w_obj.tocli()
+ else:
+ raise
+ else:
+ if isinstance(cliobj, W_CliObject):
+ return cliobj.b_obj # unwrap it!
+ else:
+ # this shouldn't happen! Fallback to the default impl
+ return w_obj.tocli()
def cli2py(space, b_obj):
# TODO: support other types and find the most efficient way to
Modified: pypy/dist/pypy/module/clr/test/test_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/test/test_clr.py (original)
+++ pypy/dist/pypy/module/clr/test/test_clr.py Sat Jan 19 17:56:59 2008
@@ -103,6 +103,10 @@
Math = clr.load_cli_class(self.mscorlib, 'System', 'Math')
raises(TypeError, Math.Abs, "foo")
+ def test_wrong_overload_ctor(self):
+ from System.Collections import ArrayList
+ raises(TypeError, ArrayList, "foo")
+
def test_staticmethod(self):
import clr
Math = clr.load_cli_class(self.mscorlib, 'System', 'Math')
@@ -263,3 +267,19 @@
import clr
from System.Collections.Generic import List
raises(TypeError, "List[int, int]")
+
+ def test_py2cli_cliobjects(self):
+ from System.IO import StreamReader, MemoryStream
+ mem = MemoryStream(100)
+ sr = StreamReader(mem) # does not raise
+
+ def test_external_assemblies(self):
+ import clr
+ clr.AddReferenceByPartialName('System.Xml')
+ from System.IO import StringReader
+ from System.Xml import XmlReader
+ buffer = StringReader("<foo>test</foo>")
+ xml = XmlReader.Create(buffer)
+ xml.ReadStartElement("foo")
+ assert xml.ReadString() == 'test'
+ xml.ReadEndElement()
Modified: pypy/dist/pypy/module/clr/test/test_importer.py
==============================================================================
--- pypy/dist/pypy/module/clr/test/test_importer.py (original)
+++ pypy/dist/pypy/module/clr/test/test_importer.py Sat Jan 19 17:56:59 2008
@@ -68,8 +68,9 @@
def test_AddReferenceByPartialName(self):
import clr
- def fn():
- import System.Xml.XmlReader
- raises(ImportError, fn)
clr.AddReferenceByPartialName('System.Xml')
- fn() # does not raise
+ import System.Xml.XmlReader # does not raise
+
+ def test_AddReference_early(self):
+ import clr
+ clr.AddReferenceByPartialName('System.Xml')
More information about the Pypy-commit
mailing list