[pypy-svn] r41437 - in pypy/dist/pypy: doc module/clr

antocuni at codespeak.net antocuni at codespeak.net
Tue Mar 27 10:59:19 CEST 2007

Author: antocuni
Date: Tue Mar 27 10:59:16 2007
New Revision: 41437

   pypy/dist/pypy/doc/clr-module.txt   (contents, props changed)
Some documentations about the clr module.

Added: pypy/dist/pypy/doc/clr-module.txt
--- (empty file)
+++ pypy/dist/pypy/doc/clr-module.txt	Tue Mar 27 10:59:16 2007
@@ -0,0 +1,101 @@
+The ``clr`` module for PyPy.NET
+PyPy.NET give you access to the sorrounding .NET environment via the
+``clr`` module. This module is still experimental and the its
+interface might change in next versions, but it's still useful to
+experiment a bit with PyPy.NET.
+The main entry-point for the ``clr`` module is the ``load_cli_class``
+function: it takes the names of a .NET namespace and a class and
+returns an object that can be used and instantiated as a normal Python
+class but refers to the .NET one.
+The resulting class tries to behave as much as possible in the
+"expected" way both for the developers used to .NET and for the ones
+used to Python.
+In particular, the following features are mapped one to one because
+they exist in both worlds:
+  - .NET constructors are mapped to the Python __init__ method;
+  - .NET instance methods are mapped to Python methods;
+  - .NET static methods are mapped to Python static methods (belonging
+    to the class);
+  - .NET properties are mapped to property-like Python objects (very
+    similar to the Python ``property`` built-in);
+  - .NET indexers are mapped to Python __getitem__ and __setitem__;
+Moreover, since the object returned by ``load_cli_class`` is a plain
+Python class, all the usual Python features such as bound and unbound
+methods are available as well.
+At the moment the only way to load a .NET class is to explicitly use
+``clr.load_cli_class``; in the future they will be automatically
+loaded when accessing .NET namespaces as they were Python modules, as
+IronPython does.
+Example of usage
+Here is an example of intercative session using the ``clr`` module:
+    >>>> import clr
+    >>>> ArrayList = clr.load_cli_class('System.Collections', 'ArrayList')
+    >>>> obj = ArrayList()
+    >>>> obj.Add(1)
+    0
+    >>>> obj.Add(2)
+    1
+    >>>> obj.Add("foo")
+    2
+    >>>> print obj[0], obj[1], obj[2]
+    1 2 foo
+    >>>> print obj.Count
+    3
+Conversion of parameters
+When calling a .NET method Python objects are converted to .NET
+objects.  Lots of effort have been taken to make the conversion as
+much transparent as possibile; in particular, all the primitive types
+such as int, float and string are converted to the corresponding .NET
+types (e.g., ``System.Int32``, ``System.Float64`` and
+Python objects without a corresponding .NET types (e.g., instances of
+user classes) are passed as "black boxes", for example to be stored in
+some sort of collection.
+The opposite .NET to Python conversions happen for the values returned
+by the methods. Again, primitive types are converted in a
+straightforward way, while objects of non-primitive types are returned
+as "black boxes".
+Overload resolution
+When calling an overloaded method, PyPy.NET tries to find the best
+overload for the given arguments; for example, consider the
+``System.Math.Abs`` method::
+    >>>> import clr
+    >>>> Math = clr.load_cli_class('System', 'Math')
+    >>>> Math.Abs(-42)
+    42
+    >>>> Math.Abs(-42.0)
+    42.0
+``System.Math.Abs`` has got overloadings both for integers and floats:
+in the first case we call the method ``System.Math.Abs(int32)``, while
+in the second one we call the method ``System.Math.Abs(float64)``.
+If the system can't find a best overload for the given parameters, a
+TypeError exception is raised.

Modified: pypy/dist/pypy/doc/getting-started.txt
--- pypy/dist/pypy/doc/getting-started.txt	(original)
+++ pypy/dist/pypy/doc/getting-started.txt	Tue Mar 27 10:59:16 2007
@@ -723,7 +723,7 @@
 Trying the experimental .NET integration
-You can also try the still very experimental ``clr`` module that
+You can also try the still very experimental clr_ module that
 enables integration with the surrounding .NET environment.
 You can dynamically load .NET classes using the ``clr.load_cli_class``
@@ -918,6 +918,7 @@
 .. _`.NET Frameword SDK 2.0`: http://msdn.microsoft.com/netframework/downloads/updates/default.aspx
 .. _Mono: http://www.mono-project.com/Main_Page
 .. _`CLI backend`: cli-backend.html
+.. _clr: clr-module.html
 .. _Dot Graphviz:           http://www.graphviz.org/
 .. _Pygame:                 http://www.pygame.org/

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	Tue Mar 27 10:59:16 2007
@@ -64,6 +64,19 @@
         return cli2py(space, b_res)
 def call_staticmethod(space, typename, methname, w_args):
+    """
+    Call a .NET static method.
+    Parameters:
+      - typename: the fully qualified .NET name of the class
+        containing the method (e.g. ``System.Math``)
+      - methname: the name of the static method to call (e.g. ``Abs``)
+      - args: a list containing the arguments to be passed to the
+        method.
+    """
     b_type = System.Type.GetType(typename) # XXX: cache this!
     return call_method(space, None, b_type, methname, w_args, 0)
 call_staticmethod.unwrap_spec = [ObjSpace, str, str, W_Root]
@@ -160,6 +173,17 @@
 CliClassCache = _CliClassCache()
 def load_cli_class(space, namespace, classname):
+    """
+    Load the given .NET class into the PyPy interpreter and return a
+    Python class referencing to it.
+    Parameters:
+       - namespace: the full name of the namespace containing the
+         class (e.g., ``System.Collections``).
+       - classname: the name of the class in the specified namespace
+         (e.g. ``ArrayList``).    """
     fullname = '%s.%s' % (namespace, classname)
     w_cls = CliClassCache.get(fullname)
     if w_cls is None:

More information about the Pypy-commit mailing list