[Cython] assignment to reference argument bug, revisited

Robert Byrnes byrnes at wildpumpkin.net
Thu Jan 16 18:43:58 CET 2014

Are there any plans to fix the "Assignment to reference" bug mentioned here?


> Oh, yes, of course. Good old C++, where local T& x is unassignable but
> assigning to an argument T& x is a common idiom. This is a bug in Cython.

This is still broken in 0.19.2, and a casual inspection of 0.20rc1
suggests that it is broken there as well.

Is the fix as simple as this?

--- ExprNodes.py~       2014-01-16 12:31:08.377573000 -0500
+++ ExprNodes.py        2014-01-16 12:30:56.945501000 -0500
@@ -1605,7 +1605,7 @@ class NameNode(AtomicExprNode):

         if self.type.is_const:
             error(self.pos, "Assignment to const '%s'" % self.name)
-        if self.type.is_reference:
+        if self.type.is_reference and not self.entry.is_arg:
             error(self.pos, "Assignment to reference '%s'" % self.name)
         if not self.is_lvalue():
             error(self.pos, "Assignment to non-lvalue '%s'"

This seems to work for me, but ...

I wonder if it is also correct to set self.entry.cf_unused = True
for the case of assignment to reference arguments, to avoid CYTHON_UNUSED
annotations in the function definitions and unused variable warnings.


More information about the cython-devel mailing list