[IronPython] Events and the += operator

Martin Maly Martin.Maly at microsoft.com
Mon Nov 28 18:30:24 CET 2005


Hi Jonathan,

We have bug in IronPython that prevents static events to be set. Instance events work just fine, it is the static ones (like Application.Idle) that cause trouble. We have the bug on our radar already and hopefully will fix it soon.

Martin

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jonathan Jacobs
Sent: Wednesday, November 23, 2005 5:06 AM
To: Discussion of IronPython
Subject: [IronPython] Events and the += operator

Hi,

(I did a couple of searches on the archives and checked the bug tracker but still found nothing directly relating to this. Hopefully I didn't miss anything.)

I'm having a problem trying to attach an event handler to
System.Windows.Forms.Application.Idle:

 >>> System.Windows.Forms.Application.Idle
<event# Idle on Application>
 >>> def _(sender, e):
...   print 'Zzzz'
...
 >>> System.Windows.Forms.Application.Idle += _ Traceback (most recent call last):
    at <shell>
TypeError: can't set attributes of built-in/extension type 'System.Windows.Forms.Application'

At first glance, it looks like this didn't work. However, creating a Form object and passing it to Application.Run does indeed print 'Zzzz' 
when idle.

Stepping through the code indicates that InPlaceAdd mimics the C# += syntax for events (ie. attaches the event handler) and that AugAssignStmt always emits a "set", which correlates with CPython's behaviour as seen below (I'm not sure whether this is part of the Python specification or not, anyway):

 >>> l = ([], [])
 >>> l[0] += [1, 2]
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: object does not support item assignment

It would seem that IronPython needs a more Pythonic (or less C#ish, if you prefer) way of doing this.

In the meanwhile, one can get around it by either doing:

 >>> Application.Idle.__iadd__(_)

   or

 >>> try:
 >>>   Application.Idle += _
 >>> except TypeError:
 >>>   pass

Regards
--
Jonathan

When you meet a master swordsman,
show him your sword.
When you meet a man who is not a poet,
do not show him your poem.
                 -- Rinzai, ninth century Zen master _______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list