From tony at pyxll.com Wed Oct 9 11:27:42 2013 From: tony at pyxll.com (Tony Roberts) Date: Wed, 9 Oct 2013 10:27:42 +0100 Subject: [Python.NET] subclassing managed types in python Message-ID: Hi, am I right in thinking that currently when managed types are subclassed in python the python methods can't override the base class methods when called from .net? It would be useful to be able to do this for something I'm working on at the moment, and before I started looking at it I wondered if anyone has done any work on this before? I think it should be possible to achieve by creating new managed types using System.Reflection.Emit.TypeBuilder and replace any virtual methods with ones that first look for a method on the python object with the same name before falling back to the base class method - the same way SWIG wrappers work for C++ classes basically but done dynamically. In the case where there are multiple methods with the same name but different signatures the python method would override them all and have to check what it had been passed to do the right thing. Any thoughts? thanks, Tony -------------- next part -------------- An HTML attachment was scrubbed... URL: From jmsachs at gmail.com Tue Oct 15 18:48:18 2013 From: jmsachs at gmail.com (Jason Sachs) Date: Tue, 15 Oct 2013 09:48:18 -0700 Subject: [Python.NET] trapping errors Message-ID: Is there a way to trap errors in python.net? I have an instance of try: doSomethingInDotNetThatMayFail() except: logger.exception("Something really wrong happened") and when the .NET library I'm using fails, it prints out an error message but never gets to the except: clause, and Python crashes. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Thu Oct 17 11:42:08 2013 From: tony at pyxll.com (Tony Roberts) Date: Thu, 17 Oct 2013 10:42:08 +0100 Subject: [Python.NET] subclassing managed types in python In-Reply-To: References: Message-ID: Hi, I went ahead and implemented this as I didn't get any response from my last mail. The code is in my github repo if anyone wants it (would be great if this feature could be integrated back into the main project??) https://github.com/tonyroberts/pythonnet The tests in test_subclass.py (see also subclasstest.cs) show how you can now subclass a managed type in python, instantiate that type both in python and in managed code and then call the virtual methods overridden in the subclass from managed code (as well as from python) and it will call back out to the python code. It works by constructing new managed types on the fly using System.Reflection.Emit (see new file classderived.cs). There are a couple of issues which aren't that important to me right now, but do cause a couple of the unit tests to fail: -- subclass constructors can no longer take extra arguments (as the __init__ method is called from the managed subclass ctor) -- __init__ gets called twice when instantiating sub classes from python (related to the above, it's because __init__ needs to be called when the object is instantiated from managed code); doesn't cause a test to fail but thought I'd mention it. -- sub-classing of array types isn't working anymore I'll probably get round to fixing these, but for now for my purposes I'm not worried about them for now. If this is useful for anyone else and someone comes up with some fixes please feel free to send me a pull request :) One other thing that's not working is using super to call the base class method from the subclass. I've got a fix so that the base class method can be called directly (eg SubClass.x(self), where self is an instance of DerivedClass) but I've not checked that in as I'd really prefer to get super() working. If anyone has any ideas about how to get that working please let me know... cheers, Tony On Wed, Oct 9, 2013 at 10:27 AM, Tony Roberts wrote: > Hi, > > am I right in thinking that currently when managed types are subclassed in > python the python methods can't override the base class methods when called > from .net? > > It would be useful to be able to do this for something I'm working on at > the moment, and before I started looking at it I wondered if anyone has > done any work on this before? > > I think it should be possible to achieve by creating new managed types > using System.Reflection.Emit.TypeBuilder and replace any virtual methods > with ones that first look for a method on the python object with the same > name before falling back to the base class method - the same way SWIG > wrappers work for C++ classes basically but done dynamically. In the case > where there are multiple methods with the same name but different > signatures the python method would override them all and have to check what > it had been passed to do the right thing. > > Any thoughts? > > thanks, > Tony > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kyle.rocha at gmail.com Thu Oct 17 19:15:11 2013 From: kyle.rocha at gmail.com (Kyle Rocha) Date: Thu, 17 Oct 2013 10:15:11 -0700 Subject: [Python.NET] subclassing managed types in python In-Reply-To: References: Message-ID: I was also looking into implementing the monodevelop document and project interfaces in python in order to provide better .Net project development in sublime. Didn't get too far unfortunately. On Oct 9, 2013 2:52 AM, "Tony Roberts" wrote: > Hi, > > am I right in thinking that currently when managed types are subclassed in > python the python methods can't override the base class methods when called > from .net? > > It would be useful to be able to do this for something I'm working on at > the moment, and before I started looking at it I wondered if anyone has > done any work on this before? > > I think it should be possible to achieve by creating new managed types > using System.Reflection.Emit.TypeBuilder and replace any virtual methods > with ones that first look for a method on the python object with the same > name before falling back to the base class method - the same way SWIG > wrappers work for C++ classes basically but done dynamically. In the case > where there are multiple methods with the same name but different > signatures the python method would override them all and have to check what > it had been passed to do the right thing. > > Any thoughts? > > thanks, > Tony > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: