[Python.NET] Adding new attributes to .Net classes is unstable

Torget Øystein Idema <[email protected]> Wed, 23 Nov 2016 13:19:59 +0000
Newsgroups gmane.comp.python.dotnet
Message-ID <AM3PR05MB07909D75657C12DD84E35465EBB70@AM3PR05MB0790.eurprd05.prod.outlook.com>
Hi,

I have a problem with adding attributes to .Net classes being unstable. E.g.

from System import TimeSpan

TimeSpan.my_method = lambda self: self.TotalMinutes
ts = TimeSpan.FromHours(1)
ts.my_method()

This code will sometimes work and other times fail with the exception 'TimeSpan' object has no attribute 'my_method'.

The failure rate is variable and for some scripts/program it will work all the time while for others it will not work at all. It seems to fail a lot more often on Python 3.5 than on Python 3.4.

If I look at either dir(ts) or ts.__class__.__dict__ the method is there even when the call to my_method fails.

I have tried to look into the PythonNet code for clues, but as far as I can see the new attribute is registered by MetaType. tp_setattro without error, but the method is still not found.

What I want to ask you is


1.       Is adding new attributes to a .Net class supported behaviour or does it work more or less by accident?

2.       Any pointers to how the attribute lookup is handled for .Net classes is appreciated. Currently I have not found if and where PythonNet is involved in the actual attribute lookup process.

Cheers,

Øystein Idema Torget

_________________________________________________
Python.NET mailing list - [email protected]
https://mail.python.org/mailman/listinfo/pythondotnet