[IronPython] bug with closure in coroutine

Ronnie Maor ronnie.maor at gmail.com
Wed Jan 27 17:16:42 CET 2010


Hi IronPython team,

Seems IronPython 2.6 has some problems with compiling coroutines that
contain closures:

tmp.py:

def coroutine():
    x = yield 3
    def inner():
        print 'x=',x
    inner()

c = coroutine()
c.next()
c.send(10)


with CPython:

C:\Temp>python tmp.py
x= 10
Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    c.send(10)
StopIteration



with IronPython 2.6:

C:\Temp>ipy tmp.py
Traceback (most recent call last):
  File "tmp.py", line 7, in tmp.py
TypeError: Unable to cast object of type
'Microsoft.Scripting.Ast.FieldExpression' to type
'Microsoft.Scripting.Ast.BlockExpression'.



workarounds:
1) re-assign the result returned from yield to another variable

def coroutine():
    tmp_x = yield 3
    x = tmp_x
    def inner():
        print 'x=',x
    inner()


2) pass the value explicitly instead of using a closure

def coroutine():
    x = yield 3
    def inner(x):
        print 'x=',x
    inner(x)


hope it's easy to fix

thanks
Ronnie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100127/1686aac2/attachment.html>


More information about the Ironpython-users mailing list