[Cython] Bug(?): chained assignments and tuple unpacking

Stefan Behnel stefan_ml at behnel.de
Fri Apr 12 11:29:18 CEST 2013


Alok Singhal, 10.04.2013 22:36:
> The following code (also attached as a .pyx file) fails in the current
> Cython :
> 
> cdef class test:
>     pass
> 
> def main():
>     cdef test a, b, c, d
>     (a, b) = (c, d) = (None, None)
> 
> When run, I get the following traceback:
> 
> In [1]: import assign_test
> In [2]: assign_test.main()
> Traceback (most recent call last):
>   File "<ipython-input-2-c2f7599d7b34>", line 1, in <module>
>     assign_test.main()
>   File "assign_test.pyx", line 6, in assign_test.main (assign_test.c:669)
>     (a, b) = (c, d) = (None, None)
> TypeError: Cannot convert NoneType to assign_test.test
> 
> I am using the latest Cython development version (from github).  Also
> tested with Cython 0.18.
> 
> $ git rev-parse HEAD
> e8bd1789905a58d8224e4be5019be401f360aa54
> 
> I don't get an error when I change the assignments to either:
> 
> (a, b) = (None, None)
> 
> or
> 
> a = b = None
> 
> Is this a bug in Cython?  I tried doing a git bisect and it seems like
> commit ea6a71acb5c79afb080855be1cb6ca30d283ec25 is when the above code
> started failing (It works with the previous commit).

Thanks for the report and the excellent analysis. Here is a fix:

https://github.com/cython/cython/commit/eb1e7173133a5dcc266203e8928c60b2e3b8446a

Plus, the generated code was horribly redundant. I cleaned it up.

https://github.com/cython/cython/commit/2592064271ea1c1b9ad1844232c5890e06861eb3

Stefan



More information about the cython-devel mailing list