[pypy-svn] r37651 - pypy/dist/pypy/module/_dotnet

antocuni at codespeak.net antocuni at codespeak.net
Wed Jan 31 11:13:34 CET 2007


Author: antocuni
Date: Wed Jan 31 11:13:32 2007
New Revision: 37651

Added:
   pypy/dist/pypy/module/_dotnet/boxing_rules.py   (contents, props changed)
Modified:
   pypy/dist/pypy/module/_dotnet/__init__.py
   pypy/dist/pypy/module/_dotnet/interp_dotnet.py
Log:
Refactoring of the conversion between wrapped (PyPy) values to boxed
(CLI) values. Instead of testing the wrapped value for each of the
supported types we introduce the method tocli() in the W_Root
hierarchy.

The tocli method is introduced "from the outside" in
boxing_rules.py. Tests and translation pass, but I'm not sure wheter
there are counter-indications.



Modified: pypy/dist/pypy/module/_dotnet/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_dotnet/__init__.py	(original)
+++ pypy/dist/pypy/module/_dotnet/__init__.py	Wed Jan 31 11:13:32 2007
@@ -1,6 +1,8 @@
 # Package initialisation
 from pypy.interpreter.mixedmodule import MixedModule
 
+import boxing_rules # with side effects
+
 class Module(MixedModule):
     """CLR module"""
 
@@ -11,3 +13,4 @@
         'call_staticmethod': 'interp_dotnet.call_staticmethod',
         'load_cli_class': 'interp_dotnet.load_cli_class',
     }
+        

Added: pypy/dist/pypy/module/_dotnet/boxing_rules.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_dotnet/boxing_rules.py	Wed Jan 31 11:13:32 2007
@@ -0,0 +1,24 @@
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.floatobject import W_FloatObject
+from pypy.translator.cli.dotnet import box
+
+def tocli(self):
+    return None
+W_Root.tocli = tocli
+
+def tocli(self):
+    return box(self.intval)
+W_IntObject.tocli = tocli
+
+def tocli(self):
+    return box(self.floatval)
+W_FloatObject.tocli = tocli
+
+
+from pypy.objspace.fake.objspace import W_Object as W_Object_Fake
+from pypy.rlib.nonconst import NonConstant
+
+def tocli(self):
+    return NonConstant(None)
+W_Object_Fake.tocli = tocli

Modified: pypy/dist/pypy/module/_dotnet/interp_dotnet.py
==============================================================================
--- pypy/dist/pypy/module/_dotnet/interp_dotnet.py	(original)
+++ pypy/dist/pypy/module/_dotnet/interp_dotnet.py	Wed Jan 31 11:13:32 2007
@@ -62,14 +62,21 @@
 call_staticmethod.unwrap_spec = [ObjSpace, str, str, W_Root]
 
 def py2cli(space, w_obj):
-    if space.is_true(space.isinstance(w_obj, space.w_int)):
-        return box(space.int_w(w_obj))
-    if space.is_true(space.isinstance(w_obj, space.w_float)):
-        return box(space.float_w(w_obj))
-    else:
+##    if space.is_true(space.isinstance(w_obj, space.w_int)):
+##        return box(space.int_w(w_obj))
+##    if space.is_true(space.isinstance(w_obj, space.w_float)):
+##        return box(space.float_w(w_obj))
+##    else:
+##        typename = space.type(w_obj).getname(space, '?')
+##        msg = "Can't convert type %s to .NET" % typename
+##        raise OperationError(space.w_TypeError, space.wrap(msg))
+    b_result = w_obj.tocli()
+    if b_result is None:
         typename = space.type(w_obj).getname(space, '?')
         msg = "Can't convert type %s to .NET" % typename
         raise OperationError(space.w_TypeError, space.wrap(msg))
+    else:
+        return b_result
 
 def cli2py(space, b_obj):
     b_type = b_obj.GetType()
@@ -95,14 +102,9 @@
     list_w = [space.wrap(s) for s in lst]
     return space.newlist(list_w)
 
-def load_cli_class(space, namespace, classname):
-    fullname = '%s.%s' % (namespace, classname)
-    b_type = System.Type.GetType(fullname)
+def get_methods(space, b_type):
     methods = []
     staticmethods = []
-    properties = []
-    indexers = []
-
     b_methodinfos = b_type.GetMethods()
     for i in range(len(b_methodinfos)):
         b_meth = b_methodinfos[i]
@@ -111,7 +113,13 @@
                 staticmethods.append(str(b_meth.get_Name()))
             else:
                 methods.append(str(b_meth.get_Name()))
+    w_staticmethods = wrap_list_of_strings(space, staticmethods)
+    w_methods = wrap_list_of_strings(space, methods)
+    return w_staticmethods, w_methods
 
+def get_properties(space, b_type):
+    properties = []
+    indexers = []
     b_propertyinfos = b_type.GetProperties()
     for i in range(len(b_propertyinfos)):
         b_prop = b_propertyinfos[i]
@@ -126,13 +134,22 @@
             properties.append((b_prop.get_Name(), get_name, set_name))
         else:
             indexers.append((b_prop.get_Name(), get_name, set_name))
-
-    w_staticmethods = wrap_list_of_strings(space, staticmethods)
-    w_methods = wrap_list_of_strings(space, methods)
     w_properties = wrap_list_of_tuples(space, properties)
     w_indexers = wrap_list_of_tuples(space, indexers)
-    return build_wrapper(space, space.wrap(namespace), space.wrap(classname),
-                         w_staticmethods, w_methods, w_properties, w_indexers)
+    return w_properties, w_indexers
+
+def load_cli_class(space, namespace, classname):
+    fullname = '%s.%s' % (namespace, classname)
+    b_type = System.Type.GetType(fullname)
+    w_staticmethods, w_methods = get_methods(space, b_type)
+    w_properties, w_indexers = get_properties(space, b_type)
+    return build_wrapper(space,
+                         space.wrap(namespace),
+                         space.wrap(classname),
+                         w_staticmethods,
+                         w_methods,
+                         w_properties,
+                         w_indexers)
 load_cli_class.unwrap_spec = [ObjSpace, str, str]
 
 



More information about the Pypy-commit mailing list