[pypy-dev] mapping C# Delegates to Python

Antonio Cuni anto.cuni at gmail.com
Thu Dec 6 00:35:36 CET 2007

amit wrote:

> /*
> * Now in Python what I might want to do is
> *
> * import System.Delegate
> *
> * class Deleg(Delegate):
> *      __init__(self, ...):
> *          pass

I don't think we want to do this. In .NET System.Delegate is treated 
specially by the virtual machine, and it needs an exact declaration of 
the signature.

The interoperability between python and delegates is two-ways:

1) having a .NET delegate in a Python variable and being able to call it 
using the usual Python syntax

2) having a .NET method taking a delegate and being able to pass it a 
python callable

Point 1 is quite easy: it's just a matter do map Invoke on __call__.

Point 2 is probably very hard to solve for the general case, because 
.NET delegates are statically typed and Python callables are not; I 
didn't think very deeply about the problem, but I think the solution 
involves the creation of a thin wrapper method on the fly.

I suggest to do only point 1 for now. The hardest part could be to find 
a way to test it properly, because we need to find a place in the 
standard library that returns a delegate we can call; maybe you can find 
something useful here:

ciao Anto

More information about the Pypy-dev mailing list