[Cython] Bug in list comprehension

Stefan Behnel stefan_ml at behnel.de
Sat Oct 17 02:18:25 EDT 2015


Leandro Pereira de Lima e Silva schrieb am 16.10.2015 um 16:04:
> I've think I've found a bug in Cython (or is this just a funny feature?).
> The error occurs when the variable name used as source for the iteration is
> also used as a value.
> 
> Given the files test1.pyx (cython) and test2.py (python):
> 
> test1.pyx:
> def test():
>     x = range(10)
>     return [x for x in x]
> 
> test2.py:
> def test():
>     x = range(10)
>     return [x for x in x]

Note that you could just compile test2.py in Cython. No need to change the
suffix.


> The results in running them are:
> >>> import test1, test2
> >>> test1.test()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "test1.pyx", line 4, in test1.test
> (/Users/leandro/.pyxbld/temp.macosx-10.6-intel-3.4/pyrex/test1.c:666)
>     return [x for x in x]
> UnboundLocalError: local variable 'x' referenced before assignment

Yes, that's a known bug. Pops up every once in a while, difficult to fix,
easy to work around.

http://trac.cython.org/ticket/600

Stefan



More information about the cython-devel mailing list