[pypy-svn] r50839 - in pypy/dist/pypy: module/clr translator/cli translator/cli/src translator/cli/test
antocuni at codespeak.net
antocuni at codespeak.net
Mon Jan 21 15:56:33 CET 2008
Author: antocuni
Date: Mon Jan 21 15:56:33 2008
New Revision: 50839
Modified:
pypy/dist/pypy/module/clr/interp_clr.py
pypy/dist/pypy/translator/cli/dotnet.py
pypy/dist/pypy/translator/cli/query.py
pypy/dist/pypy/translator/cli/src/query.cs
pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
prebuild the namespaces and types hierarchy, else the annotator gets
confused because it thinks some getattr will fail.
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 Mon Jan 21 15:56:33 2008
@@ -13,10 +13,6 @@
TargetInvocationException = NativeException(CLR.System.Reflection.TargetInvocationException)
AmbiguousMatchException = NativeException(CLR.System.Reflection.AmbiguousMatchException)
-System.Double # force the type to be loaded, else the annotator could think that System has no Double attribute
-System.Boolean # the same
-System.AppDomain
-
def get_method(space, b_type, name, b_paramtypes):
try:
method = b_type.GetMethod(name, b_paramtypes)
Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py (original)
+++ pypy/dist/pypy/translator/cli/dotnet.py Mon Jan 21 15:56:33 2008
@@ -260,33 +260,6 @@
return SomeOOInstance(self.instance._INSTANCE)
-class CliNamespace(object):
- def __init__(self, name):
- self._name = name
-
- def __fullname(self, name):
- if self._name is None:
- return name
- else:
- return '%s.%s' % (self._name, name)
-
- def __getattr__(self, attr):
- from pypy.translator.cli.query import get_cli_class, Types, Namespaces
- from pypy.translator.cli.query import load_assembly, mscorlib
- load_assembly(mscorlib)
-
- fullname = self.__fullname(attr)
- if fullname in Namespaces:
- value = CliNamespace(fullname)
- elif fullname in Types:
- value = get_cli_class(fullname)
- else:
- raise AttributeError, attr
- setattr(self, attr, value)
- return value
-
-CLR = CliNamespace(None)
-
BOXABLE_TYPES = [ootype.Signed, ootype.Unsigned, ootype.SignedLongLong,
ootype.UnsignedLongLong, ootype.Bool, ootype.Float,
ootype.Char, ootype.String]
@@ -525,8 +498,8 @@
_about_ = eventhandler
def compute_result_annotation(self, s_value):
- from pypy.translator.cli.query import load_class_maybe
- cliType = load_class_maybe('System.EventHandler')
+ from pypy.translator.cli.query import get_cli_class
+ cliType = get_cli_class('System.EventHandler')
return SomeOOInstance(cliType._INSTANCE)
def specialize_call(self, hop):
@@ -534,3 +507,7 @@
methodname = hop.args_r[0].methodname
c_methodname = hop.inputconst(ootype.Void, methodname)
return hop.genop('cli_eventhandler', [v_obj, c_methodname], hop.r_result.lowleveltype)
+
+from pypy.translator.cli.query import CliNamespace
+CLR = CliNamespace(None)
+CLR._buildtree()
Modified: pypy/dist/pypy/translator/cli/query.py
==============================================================================
--- pypy/dist/pypy/translator/cli/query.py (original)
+++ pypy/dist/pypy/translator/cli/query.py Mon Jan 21 15:56:33 2008
@@ -8,8 +8,6 @@
from pypy.translator.cli.rte import Query
from pypy.translator.cli.sdk import SDK
from pypy.translator.cli.support import log
-from pypy.translator.cli.dotnet import CLR, CliNamespace, CliClass,\
- NativeInstance, _overloaded_static_meth, _static_meth, OverloadingResolver
Assemblies = set()
Types = {} # TypeName -> ClassDesc
@@ -121,6 +119,9 @@
raise TypeError
def get_cliclass(self):
+ from pypy.translator.cli.dotnet import CliClass, NativeInstance
+ from pypy.translator.cli.dotnet import _overloaded_static_meth, _static_meth
+
if self._cliclass is not None:
return self._cliclass
@@ -152,6 +153,7 @@
return Class
def group_methods(self, methods, overload, meth, Meth):
+ from pypy.translator.cli.dotnet import OverloadingResolver
groups = {}
for name, args, result in methods:
groups.setdefault(name, []).append((args, result))
@@ -168,3 +170,40 @@
ARGS = [get_ootype(arg) for arg in args]
RESULT = get_ootype(result)
return Meth(ARGS, RESULT)
+
+placeholder = object()
+class CliNamespace(object):
+ def __init__(self, name):
+ self._name = name
+ self.__treebuilt = False
+
+ def __fullname(self, name):
+ if self._name is None:
+ return name
+ else:
+ return '%s.%s' % (self._name, name)
+
+ def _buildtree(self):
+ assert self._name is None, '_buildtree can be called only on top-level CLR, not on namespaces'
+ from pypy.translator.cli.support import getattr_ex
+ load_assembly(mscorlib)
+ for fullname in sorted(list(Namespaces)):
+ if '.' in fullname:
+ parent, name = fullname.rsplit('.', 1)
+ parent = getattr_ex(self, parent)
+ setattr(parent, name, CliNamespace(fullname))
+ else:
+ setattr(self, fullname, CliNamespace(fullname))
+
+ for fullname in Types.keys():
+ parent, name = fullname.rsplit('.', 1)
+ parent = getattr_ex(self, parent)
+ setattr(parent, name, placeholder)
+
+ def __getattribute__(self, attr):
+ value = object.__getattribute__(self, attr)
+ if value is placeholder:
+ fullname = self.__fullname(attr)
+ value = get_cli_class(fullname)
+ setattr(self, attr, value)
+ return value
Modified: pypy/dist/pypy/translator/cli/src/query.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/query.cs (original)
+++ pypy/dist/pypy/translator/cli/src/query.cs Mon Jan 21 15:56:33 2008
@@ -143,6 +143,7 @@
&&(t == typeof(System.Array) ||
t.FullName == null ||
t.FullName.StartsWith("System.Array+InternalArray") ||
+ t.IsNested ||
t.IsNotPublic ||
t.IsByRef ||
t.IsPointer ||
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 Jan 21 15:56:33 2008
@@ -10,9 +10,7 @@
native_exc, new_array, init_array, typeof, eventhandler
System = CLR.System
-Math = CLR.System.Math
ArrayList = CLR.System.Collections.ArrayList
-Type = CLR.System.Type
class TestDotnetAnnotation(object):
@@ -38,11 +36,11 @@
def test_class(self):
def fn():
- return Math
+ return System.Math
a = RPythonAnnotator()
s = a.build_types(fn, [])
assert isinstance(s, SomeCliClass)
- assert s.const is Math
+ assert s.const is System.Math
def test_fullname(self):
def fn():
@@ -50,22 +48,22 @@
a = RPythonAnnotator()
s = a.build_types(fn, [])
assert isinstance(s, SomeCliClass)
- assert s.const is Math
+ assert s.const is System.Math
def test_staticmeth(self):
def fn():
- return Math.Abs
+ return System.Math.Abs
a = RPythonAnnotator()
s = a.build_types(fn, [])
assert isinstance(s, SomeCliStaticMethod)
- assert s.cli_class is Math
+ assert s.cli_class is System.Math
assert s.meth_name == 'Abs'
def test_staticmeth_call(self):
def fn1():
- return Math.Abs(42)
+ return System.Math.Abs(42)
def fn2():
- return Math.Abs(42.5)
+ return System.Math.Abs(42.5)
a = RPythonAnnotator()
assert type(a.build_types(fn1, [])) is annmodel.SomeInteger
assert type(a.build_types(fn2, [])) is annmodel.SomeFloat
@@ -172,7 +170,7 @@
def test_can_be_None(self):
def fn():
- ttype = Type.GetType('foo')
+ ttype = System.Type.GetType('foo')
return ttype.get_Namespace()
a = RPythonAnnotator()
s = a.build_types(fn, [])
@@ -186,13 +184,13 @@
def test_staticmeth_call(self):
def fn(x):
- return Math.Abs(x)
+ return System.Math.Abs(x)
assert self.interpret(fn, [-42]) == 42
def test_staticmeth_overload(self):
self._skip_pythonnet('Pythonnet bug!')
def fn(x, y):
- return Math.Abs(x), Math.Abs(y)
+ return System.Math.Abs(x), System.Math.Abs(y)
res = self.interpret(fn, [-42, -42.5])
item0, item1 = self.ll_to_tuple(res)
assert item0 == 42
@@ -349,7 +347,6 @@
assert res.startswith("Index is less than 0")
def test_typeof(self):
- System.Int32 # force Int32 to be loaded
def fn():
x = box(42)
return x.GetType() == typeof(System.Int32)
@@ -409,7 +406,7 @@
return ""
def fn():
- ttype = Type.GetType('Consts, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
+ ttype = System.Type.GetType('Consts, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
namespace = ttype.get_Namespace()
if namespace is not None:
return False
More information about the Pypy-commit
mailing list