[pypy-svn] r51935 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Fri Feb 29 10:47:11 CET 2008


Author: antocuni
Date: Fri Feb 29 10:47:10 2008
New Revision: 51935

Modified:
   pypy/dist/pypy/translator/cli/constant.py
   pypy/dist/pypy/translator/cli/database.py
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
another horrible hack to allow classof(FUNCTYPE). That's needed
because in the JIT we really need to know the .NET type of
StaticMethods.



Modified: pypy/dist/pypy/translator/cli/constant.py
==============================================================================
--- pypy/dist/pypy/translator/cli/constant.py	(original)
+++ pypy/dist/pypy/translator/cli/constant.py	Fri Feb 29 10:47:10 2008
@@ -341,7 +341,7 @@
     def push_inline(self, gen, EXPECTED_TYPE):
         if not self.is_null():
             INSTANCE = self.value._INSTANCE
-            gen.ilasm.opcode('ldtoken', self.db.class_name(INSTANCE))
+            gen.ilasm.opcode('ldtoken', self.db.class_or_delegate_name(INSTANCE))
             gen.ilasm.call('class [mscorlib]System.Type class [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)')
             return
         super(CLIClassConst, self).push_inline(gen, EXPECTED_TYPE)

Modified: pypy/dist/pypy/translator/cli/database.py
==============================================================================
--- pypy/dist/pypy/translator/cli/database.py	(original)
+++ pypy/dist/pypy/translator/cli/database.py	Fri Feb 29 10:47:10 2008
@@ -150,3 +150,9 @@
             self.delegates[TYPE] = name
             self.pending_node(Delegate(self, TYPE, name))
             return name
+
+    def class_or_delegate_name(self, TYPE):
+        if isinstance(TYPE, ootype.StaticMethod):
+            return self.record_delegate(TYPE)
+        else:
+            return self.class_name(TYPE)

Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Fri Feb 29 10:47:10 2008
@@ -361,6 +361,9 @@
         else:
             return CLR.System.String(x)
     elif isinstance(x, ootype._class):
+        TYPE = x._INSTANCE
+        if isinstance(TYPE, ootype.StaticMethod):
+            return typeof(TYPE)
         name = '%s.%s' % (x._INSTANCE._namespace, x._INSTANCE._classname)
         t = CLR.System.Type.GetType(name)
         assert t is not None
@@ -560,11 +563,19 @@
     TYPE = cliClass._INSTANCE
     return PythonNet.System.Type.GetType(TYPE._assembly_qualified_name)
 
-
-def classof(cliClass):
-    assert isinstance(cliClass, CliClass)
-    TYPE = cliClass._INSTANCE
-    return ootype.runtimeClass(TYPE)
+def classof(cliClass_or_type):
+    if isinstance(cliClass_or_type, ootype.StaticMethod):
+        try:
+            FUNC = cliClass_or_type
+            return known_delegates_class[FUNC]
+        except KeyError:
+            cls = ootype._class(FUNC)
+            known_delegates_class[FUNC] = cls
+            return cls
+    else:
+        assert isinstance(cliClass_or_type, CliClass)
+        TYPE = cliClass_or_type._INSTANCE
+        return ootype.runtimeClass(TYPE)
 
 class Entry(ExtRegistryEntry):
     _about_ = typeof
@@ -657,3 +668,5 @@
     ootype.StaticMethod([ootype.Signed] * 27, ootype.Signed):   CLR.pypy.test.DelegateType_int__int_27,
     ootype.StaticMethod([ootype.Signed] * 100, ootype.Signed): CLR.pypy.test.DelegateType_int__int_100
     }
+
+known_delegates_class = {}

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	Fri Feb 29 10:47:10 2008
@@ -619,6 +619,17 @@
             return int32_a is int32_b
         assert self.interpret(fn, [])
 
+    def test_classof_functype(self):
+        # this test is overridden in TestPythonnet
+        c = classof(FUNCTYPE)
+        def fn():
+            obj = box(c)
+            t = clidowncast(obj, System.Type)
+            return t.get_Name()
+        res = self.interpret(fn, [])
+        assert res.startswith('StaticMethod__')
+
+
 class TestPythonnet(TestDotnetRtyping):
     # don't interpreter functions but execute them directly through pythonnet
     def interpret(self, f, args, backendopt='ignored'):
@@ -637,6 +648,16 @@
         res = self.interpret(fn, [])
         assert res == 'DelegateType_int__int_2'
 
+    def test_classof_functype(self):
+        # this test is overridden in TestPythonnet
+        c = classof(FUNCTYPE)
+        def fn():
+            obj = box(c)
+            t = clidowncast(obj, System.Type)
+            return t.get_Name()
+        res = self.interpret(fn, [])
+        assert res == 'DelegateType_int__int_2'
+
     def test_fieldinfo_for_const(self):
         pass # it makes sense only during translation
 



More information about the Pypy-commit mailing list