[pypy-svn] r33609 - in pypy/dist/pypy/translator/cli: . test
antocuni at codespeak.net
antocuni at codespeak.net
Mon Oct 23 17:48:40 CEST 2006
Author: antocuni
Date: Mon Oct 23 17:48:40 2006
New Revision: 33609
Added:
pypy/dist/pypy/translator/cli/dotnetdb.py (contents, props changed)
Modified:
pypy/dist/pypy/translator/cli/dotnet.py
pypy/dist/pypy/translator/cli/support.py
pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
- Make CliClass use the algorithm of ootype._overloaded_mixin for static method annotation
- Move the definition of the CLI classes from dotnet.py to dotnetdb.py
- Allow CliClass to access .NET libraries when running inside python.
Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py (original)
+++ pypy/dist/pypy/translator/cli/dotnet.py Mon Oct 23 17:48:40 2006
@@ -3,6 +3,7 @@
from pypy.rpython.ootypesystem.ootype import meth, overload, Meth, StaticMethod
from pypy.annotation import model as annmodel
from pypy.rpython.rmodel import Repr
+from pypy.translator.cli.support import CLR
## Annotation model
@@ -78,9 +79,7 @@
return hop.genop("direct_call", [cDesc] + vlist, resulttype=resulttype)
-
-## RPython interface definition
-
+## OOType model
class _static_meth(object):
def __init__(self, TYPE):
@@ -110,6 +109,17 @@
return meth._get_desc(ARGS)
+class NativeInstance(ootype.Instance):
+ def __init__(self, assembly, namespace, name, superclass,
+ fields={}, methods={}, _is_root=False, _hints = {}):
+ fullname = '%s%s.%s' % (assembly, namespace, name)
+ self._namespace = namespace
+ self._classname = name
+ ootype.Instance.__init__(self, fullname, superclass, fields, methods, _is_root, _hints)
+
+
+## RPython interface definition
+
class CliClass(object):
def __init__(self, INSTANCE, static_methods):
self._name = INSTANCE._name
@@ -129,10 +139,27 @@
return meth._get_desc(ARGS)
def _ann_static_method(self, meth_name, args_s):
- ARGS = tuple([annmodel.annotation_to_lltype(arg_s) for arg_s in args_s])
- desc = self._lookup(meth_name, ARGS)
- RESULT = desc._TYPE.RESULT
- return annmodel.lltype_to_annotation(RESULT)
+ meth = self._static_methods[meth_name]
+ return meth._annotate_overloading(args_s)
+
+ def _load_class(self):
+ names = self._INSTANCE._namespace.split('.')
+ names.append(self._INSTANCE._classname)
+ obj = CLR
+ for name in names:
+ obj = getattr(obj, name)
+ self._CLR_class = obj
+
+ def __getattr__(self, attr):
+ if attr in self._static_methods:
+ self._load_class()
+ return getattr(self._CLR_class, attr)
+ else:
+ raise AttributeError
+
+ def __call__(self, *args):
+ self._load_class()
+ return self._CLR_class(*args)
class Entry(ExtRegistryEntry):
@@ -142,40 +169,9 @@
return SomeCliClass()
-## OOType model
-
-class NativeInstance(ootype.Instance):
- def __init__(self, assembly, namespace, name, superclass,
- fields={}, methods={}, _is_root=False, _hints = {}):
- fullname = '%s%s.%s' % (assembly, namespace, name)
- ootype.Instance.__init__(self, fullname, superclass, fields, methods, _is_root, _hints)
-
-
-OBJECT = NativeInstance('[mscorlib]', 'System', 'Object', ootype.ROOT, {},
- {'ToString': ootype.meth(ootype.Meth([], ootype.String)),
- })
-Object = CliClass(OBJECT, {})
-
-STRING_BUILDER = NativeInstance('[mscorlib]', 'System.Text', 'StringBuilder', OBJECT, {}, {})
-STRING_BUILDER._add_methods({'Append': meth(Meth([ootype.String], STRING_BUILDER)),
- 'AppendLine': overload(meth(Meth([ootype.String], STRING_BUILDER)),
- meth(Meth([], STRING_BUILDER)))
- })
-StringBuilder = CliClass(STRING_BUILDER, {})
-
-##CONSOLE = NativeInstance('[mscorlib]', 'System', 'Console', ootype.ROOT, {}, {})
-##Console = CliClass(CONSOLE, {'WriteLine': {(ootype.String,): ootype.Void,
-## (ootype.Signed,): ootype.Void}})
-
-MATH = NativeInstance('[mscorlib]', 'System', 'Math', OBJECT, {}, {})
-Math = CliClass(MATH,
- {'Abs': _overloaded_static_meth(_static_meth(StaticMethod([ootype.Signed], ootype.Signed)),
- _static_meth(StaticMethod([ootype.Float], ootype.Float)))
- })
+class CliNamespace(object):
+ def __init__(self, name):
+ self._name = name
-ARRAY_LIST = NativeInstance('[mscorlib]', 'System.Collections', 'ArrayList', OBJECT, {},
- {'Add': meth(Meth([OBJECT], ootype.Signed)),
- 'get_Count': meth(Meth([], ootype.Signed))})
-ArrayList = CliClass(ARRAY_LIST, {})
Added: pypy/dist/pypy/translator/cli/dotnetdb.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/cli/dotnetdb.py Mon Oct 23 17:48:40 2006
@@ -0,0 +1,44 @@
+from pypy.rpython.ootypesystem import ootype
+from pypy.rpython.ootypesystem.ootype import meth, overload, Meth, StaticMethod
+from pypy.translator.cli.dotnet import NativeInstance, CliClass, _overloaded_static_meth, _static_meth, CliNamespace
+
+
+System = CliNamespace('System')
+System.Text = CliNamespace('System.Text')
+System.Collections = CliNamespace('System.Collections')
+
+OBJECT = NativeInstance('[mscorlib]', 'System', 'Object', ootype.ROOT, {},
+ {'ToString': ootype.meth(ootype.Meth([], ootype.String)),
+ })
+System.Object = CliClass(OBJECT, {})
+
+
+MATH = NativeInstance('[mscorlib]', 'System', 'Math', OBJECT, {}, {})
+System.Math = CliClass(MATH,
+ {'Abs': _overloaded_static_meth(_static_meth(StaticMethod([ootype.Signed], ootype.Signed)),
+ _static_meth(StaticMethod([ootype.Float], ootype.Float)))
+ })
+
+
+CONSOLE = NativeInstance('[mscorlib]', 'System', 'Console', OBJECT, {}, {})
+System.Console = CliClass(CONSOLE,
+ {'WriteLine': _overloaded_static_meth(_static_meth(StaticMethod([ootype.String], ootype.Void)),
+ _static_meth(StaticMethod([ootype.Signed], ootype.Void))),
+ })
+
+
+STRING_BUILDER = NativeInstance('[mscorlib]', 'System.Text', 'StringBuilder', OBJECT, {}, {})
+STRING_BUILDER._add_methods({'Append': meth(Meth([ootype.String], STRING_BUILDER)),
+ 'AppendLine': overload(meth(Meth([ootype.String], STRING_BUILDER)),
+ meth(Meth([], STRING_BUILDER)))
+ })
+System.Text.StringBuilder = CliClass(STRING_BUILDER, {})
+
+
+
+
+
+ARRAY_LIST = NativeInstance('[mscorlib]', 'System.Collections', 'ArrayList', OBJECT, {},
+ {'Add': meth(Meth([OBJECT], ootype.Signed)),
+ 'get_Count': meth(Meth([], ootype.Signed))})
+System.Collections.ArrayList = CliClass(ARRAY_LIST, {})
Modified: pypy/dist/pypy/translator/cli/support.py
==============================================================================
--- pypy/dist/pypy/translator/cli/support.py (original)
+++ pypy/dist/pypy/translator/cli/support.py Mon Oct 23 17:48:40 2006
@@ -1,5 +1,15 @@
+import py
from pypy.rpython.ootypesystem import ootype
+try:
+ import CLR
+except ImportError:
+ class _CLR:
+ def __getattr__(self, attr):
+ py.test.skip('Must use pythonnet for being able to access .NET libraries')
+ CLR = _CLR()
+ del _CLR
+
# some code has been stolen from genc
def string_literal(s):
def char_repr(c):
Modified: pypy/dist/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dotnet.py (original)
+++ pypy/dist/pypy/translator/cli/test/test_dotnet.py Mon Oct 23 17:48:40 2006
@@ -2,7 +2,11 @@
from pypy.annotation import model as annmodel
from pypy.translator.cli.test.runtest import CliTest
from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
- NativeInstance, Math, ArrayList, StringBuilder
+ NativeInstance
+from pypy.translator.cli.dotnetdb import System
+Math = System.Math
+ArrayList = System.Collections.ArrayList
+StringBuilder = System.Text.StringBuilder
class TestDotnet(CliTest):
@@ -68,3 +72,12 @@
return x.ToString()
res = self.ll_to_string(self.interpret(fn, []))
assert res == 'foobar'
+
+ def test_pythonnet(self):
+ # this test must be executed with pythonnet
+ assert Math.Abs(-42) == 42
+ mylist = ArrayList()
+ mylist.Add(1)
+ mylist.Add('foo')
+ assert mylist.Count == 2
+
More information about the Pypy-commit
mailing list