Python extension for MANAGED C++ with VC.NET

cgjunkaddr at attbi.com cgjunkaddr at attbi.com
Wed Jan 22 22:44:22 CET 2003


Mark

Thanks for your positive response. Attached is the extension module. The
code is not in any great shape, so I would be embarassed to release it at
this point. However, I can make a private distro for you if you are
interested.

>>> import anaconda
>>> a = anaconda.new("foofoo1", "PythonExtensionHelper.dll",
"PythonExtensionHelper.TestClass")
C:\Documents and Settings\cgadgil\My Documents\Visual Studio
Projects\testhello\
....
Test Class instantiated!
>>> a.demo("testMethod2", ["yo", "there", 2, ["x", "y"]])
testMethod2 yo there 2[]System.String[]

Explanation:
>>> import anaconda

-- Import the beast

>>> a = anaconda.new("foofoo1", "PythonExtensionHelper.dll",
"PythonExtensionHelper.TestClass")

-- create a new instance - internal instance name is "foofoo1" (need to
figure out a better way to handle this - but due to the way managed c++ does
things, i had to do this)
-- load the assembly "PythonExtensionHelper.dll" and instantiate class
"PythonExtensionHelper.TestClass"

>>> a.demo("testMethod2", ["yo", "there", 2, ["x", "y"]])

-- "demo" is the method invocation entry point. have to rename this
anyway, this invoked the "testMethod2" on the "a" object with the arguments
in the list.
(see the signature of the method below)

TestClass (C#) looks like this:

// DEMO CLASS
// NOT REALLY A HELPER - JUST STRIPPED REUSED THIS CLASS FROM SOME OLDER
CODE
using System;
namespace PythonExtensionHelper
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class TestClass
{
public TestClass()
{
//
// TODO: Add constructor logic here
//
Console.WriteLine("Test Class instantiated!");
}
public void testMethod()
{
Console.WriteLine("Test Method invoked!");
}
public void testMethod2(string x, string y, int z, string[] zz)
{
Console.WriteLine("testMethod2 " + x + " " + y + " " + z + "[]" + zz);
}
public void testMethod3(string[] pp)
{
Console.WriteLine(pp);
}
}
}


Current limitation is that "invoke" method in "MethodInfo" expects concrete
array type - it is not coerced at runtime like python. so if your ..NET
method has a type[] method, it is assumed to be "string[]"
Will fix this.

I have a managed class which holds a lookup of python instances. Whenever
someone in python calls a method on the extension type object, it is routed
to the .net instance.

Had to use the gcroot<> template to hold pinned references. I tried to store
python extension type objects in managed instances, but that caused some
weird errors.


As for being able to call python from .NET, I have some initial thoughts on
that too. We can use Reflection::Emit to create dynamic delegates on special
objects. And simulate inheritance etc by calling python code from the
managed types.
I think this is doable, but have not thought in detail about it...

Regards
Chetan

ps: Sorry for posting the library on the newsgroup, but the total is just
164 kB


"Mark Hammond" <mhammond at skippinet.com.au> wrote in message
news:mX4X9..11777$m47.37794 at news-server.bigpond.net.au...
> Chetan Gadgil wrote:
> > Hi
> >
> > Is anyone working on the above?
> > I had started work on a library I called PyCLR - a module using Mark
> > Hammond's win32all, but later discovered that Mark had already done
> > something similar for Python.NET, but was not too happy with the
results.
> >
> > I have started on VC.NET with managed c++ and have "managed" (no silly
pun
> > intended :)) to load an extension module into Python and call into
Managed
> > Code.
>
> This sounds cool.  How are you doing it?
>
> Another strategy would be a module similar to win32com, but using the
> .NET reflection API natively (you can use the COM interop layers now,
> but probably would get better results using .NET natively).  Then you
> would be able to load and run any .NET assembly, regardless of
> implementation language.
>
> Unfortunately, there is also interest in being able to use Python
> extension from .NET - ie, the other direction.
>
> But keep us posted on the results and where we can find the code!
>
> Mark.
>

LEARNING, n. The kind of ignorance distinguishing the studious.
  - Ambrose Bierce, The Devil's Dictionary



Mark Hammond <mhammond at skippinet.com.au> wrote in message news:<mX4X9.11777$m47.37794 at news-server.bigpond.net.au>...
> Chetan Gadgil wrote:
> > Hi
> > 
> > Is anyone working on the above?
> > I had started work on a library I called PyCLR - a module using Mark
> > Hammond's win32all, but later discovered that Mark had already done
> > something similar for Python.NET, but was not too happy with the results.
> > 
> > I have started on VC.NET with managed c++ and have "managed" (no silly pun
> > intended :)) to load an extension module into Python and call into Managed
> > Code.
> 
> This sounds cool.  How are you doing it?
> 
> Another strategy would be a module similar to win32com, but using the 
> .NET reflection API natively (you can use the COM interop layers now, 
> but probably would get better results using .NET natively).  Then you 
> would be able to load and run any .NET assembly, regardless of 
> implementation language.
> 
> Unfortunately, there is also interest in being able to use Python 
> extension from .NET - ie, the other direction.
> 
> But keep us posted on the results and where we can find the code!
> 
> Mark.




More information about the Python-list mailing list