[pypy-svn] r37387 - in pypy/dist/pypy: annotation translator/cli translator/cli/test

antocuni at codespeak.net antocuni at codespeak.net
Fri Jan 26 14:58:01 CET 2007


Author: antocuni
Date: Fri Jan 26 14:57:49 2007
New Revision: 37387

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
Use SomeCliInstance instead of SomeOOInstance not to modify the
behaviour of the latter too much. Allow SomeCliInstance to be None.



Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Fri Jan 26 14:57:49 2007
@@ -708,7 +708,9 @@
 
 # mixing Nones with other objects
 
-def _make_none_union(classname, constructor_args=''):
+def _make_none_union(classname, constructor_args='', glob=None):
+    if glob is None:
+        glob = globals()
     loc = locals()
     source = py.code.Source("""
         class __extend__(pairtype(%(classname)s, SomePBC)):
@@ -725,7 +727,7 @@
                 else:
                     return SomeObject()
     """ % loc)
-    exec source.compile() in globals()
+    exec source.compile() in glob
 
 _make_none_union('SomeInstance',   'classdef=obj.classdef, can_be_None=True')
 _make_none_union('SomeString',      'can_be_None=True')

Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Fri Jan 26 14:57:49 2007
@@ -2,7 +2,8 @@
 
 from pypy.annotation.pairtype import pair, pairtype
 from pypy.annotation.model import SomeObject, SomeInstance, SomeOOInstance, SomeInteger, s_None,\
-     s_ImpossibleValue, lltype_to_annotation, annotation_to_lltype, SomeChar, SomeString
+     s_ImpossibleValue, lltype_to_annotation, annotation_to_lltype, SomeChar, SomeString, SomePBC
+from pypy.annotation.binaryop import _make_none_union
 from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
 from pypy.rpython.error import TyperError
 from pypy.rpython.extregistry import ExtRegistryEntry
@@ -23,7 +24,7 @@
 
     def simple_call(self, *s_args):
         assert self.is_constant()
-        return SomeOOInstance(self.const._INSTANCE)
+        return SomeCliInstance(self.const._INSTANCE)
 
     def rtyper_makerepr(self, rtyper):
         return CliClassRepr(self.const)
@@ -46,10 +47,21 @@
     def rtyper_makekey(self):
         return self.__class__, self.cli_class, self.meth_name
 
-class __extend__(pairtype(SomeOOInstance, SomeInteger)):
+
+class SomeCliInstance(SomeOOInstance):
+    def __init__(self, ootype, can_be_None=False):
+        SomeOOInstance.__init__(self, ootype)
+        self.can_be_None= can_be_None
+
+    def __repr__(self):
+        return '%s(%s, can_be_None=%s)' % (self.__class__.__name__, self.ootype, self.can_be_None)
+
+_make_none_union('SomeCliInstance', 'ootype=obj.ootype, can_be_None=True', globals())
+
+class __extend__(pairtype(SomeCliInstance, SomeInteger)):
     def getitem((ooinst, index)):
         if ooinst.ootype._isArray:
-            return SomeOOInstance(ooinst.ootype._ELEMENT)
+            return SomeCliInstance(ooinst.ootype._ELEMENT)
         return s_ImpossibleValue
 
     def setitem((ooinst, index), s_value):
@@ -137,7 +149,9 @@
     annotation_to_lltype = classmethod(annotation_to_lltype)
 
     def lltype_to_annotation(cls, TYPE):
-        if TYPE is ootype.Char:
+        if isinstance(TYPE, NativeInstance):
+            return SomeCliInstance(TYPE)
+        elif TYPE is ootype.Char:
             return SomeChar()
         elif TYPE is ootype.String:
             return SomeString()
@@ -146,7 +160,6 @@
     lltype_to_annotation = classmethod(lltype_to_annotation)
 
 
-
 class _static_meth(object):
 
     def __init__(self, TYPE):
@@ -316,7 +329,7 @@
     _about_ = box
 
     def compute_result_annotation(self, x_s):
-        return SomeOOInstance(CLR.System.Object._INSTANCE)
+        return SomeCliInstance(CLR.System.Object._INSTANCE)
 
     def specialize_call(self, hop):
         v_obj, = hop.inputargs(*hop.args_r)
@@ -335,7 +348,7 @@
     _about_ = unbox
 
     def compute_result_annotation(self, x_s, type_s):
-        assert isinstance(x_s, SomeOOInstance)
+        assert isinstance(x_s, SomeCliInstance)
         assert x_s.ootype == CLR.System.Object._INSTANCE
         assert type_s.is_constant()
         TYPE = type_s.const
@@ -383,7 +396,7 @@
         cls = exc_s.classdef.classdesc.pyobj
         assert issubclass(cls, Exception)
         NATIVE_INSTANCE = cls._rpython_hints['NATIVE_INSTANCE']
-        return SomeOOInstance(NATIVE_INSTANCE)
+        return SomeCliInstance(NATIVE_INSTANCE)
 
     def specialize_call(self, hop):
         v_obj, = hop.inputargs(*hop.args_r)
@@ -406,7 +419,7 @@
         TYPE = type_s.const._INSTANCE
         fullname = '%s.%s[]' % (TYPE._namespace, TYPE._classname)
         cliArray = load_class_maybe(fullname)
-        return SomeOOInstance(cliArray._INSTANCE)
+        return SomeCliInstance(cliArray._INSTANCE)
 
     def specialize_call(self, hop):
         c_type, v_length = hop.inputargs(*hop.args_r)
@@ -427,7 +440,7 @@
                       (i, TYPE, arg_s.ootype)
         fullname = '%s.%s[]' % (TYPE._namespace, TYPE._classname)
         cliArray = load_class_maybe(fullname)
-        return SomeOOInstance(cliArray._INSTANCE)
+        return SomeCliInstance(cliArray._INSTANCE)
 
     def specialize_call(self, hop):
         vlist = hop.inputargs(*hop.args_r)
@@ -453,7 +466,7 @@
         from query import load_class_maybe
         assert cliClass_s.is_constant()
         cliType = load_class_maybe('System.Type')
-        return SomeOOInstance(cliType._INSTANCE)
+        return SomeCliInstance(cliType._INSTANCE)
 
     def specialize_call(self, hop):
         v_type, = hop.inputargs(*hop.args_r)

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 Jan 26 14:57:49 2007
@@ -5,7 +5,7 @@
 from pypy.rpython.ootypesystem.ootype import meth, Meth, Char, Signed, Float, String,\
      ROOT, overload, Instance, new
 from pypy.translator.cli.test.runtest import CliTest
-from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
+from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod, SomeCliInstance,\
      NativeInstance, CLR, box, unbox, OverloadingResolver, NativeException,\
      native_exc, new_array, init_array, typeof
 
@@ -74,7 +74,7 @@
             return ArrayList()
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
-        assert isinstance(s, annmodel.SomeOOInstance)
+        assert isinstance(s, SomeCliInstance)
         assert isinstance(s.ootype, NativeInstance)
         assert s.ootype._name == '[mscorlib]System.Collections.ArrayList'
 
@@ -83,7 +83,7 @@
             return box(42)
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
-        assert isinstance(s, annmodel.SomeOOInstance)
+        assert isinstance(s, SomeCliInstance)
         assert s.ootype._name == '[mscorlib]System.Object'
 
     def test_unbox(self):
@@ -100,7 +100,7 @@
             return x.ToArray()
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
-        assert isinstance(s, annmodel.SomeOOInstance)
+        assert isinstance(s, SomeCliInstance)
         assert s.ootype._isArray
         assert s.ootype._ELEMENT._name == '[mscorlib]System.Object'
 
@@ -110,9 +110,20 @@
             return x[0]
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
-        assert isinstance(s, annmodel.SomeOOInstance)
+        assert isinstance(s, SomeCliInstance)
         assert s.ootype._name == '[mscorlib]System.Object'
 
+    def test_mix_None_and_instance(self):
+        def fn(x):
+            if x:
+                return None
+            else:
+                return box(42)
+        a = RPythonAnnotator()
+        s = a.build_types(fn, [bool])
+        assert isinstance(s, SomeCliInstance)
+        assert s.can_be_None == True
+
 
 class TestDotnetRtyping(CliTest):
     def _skip_pythonnet(self, msg):



More information about the Pypy-commit mailing list