[Cython] operator() bug in cython

Stefan Behnel stefan_ml at behnel.de
Fri Feb 13 15:05:09 CET 2015


Ulrich Dobramysl schrieb am 11.02.2015 um 09:56:
> I tried to declare an external c++ class that includes an operator()
> function in a pxd file. When I then call a class instance, cython generates
> faulty c++ code. It includes a call to "operator()()", and not a call to
> the instance object. Here is a minimum working example:
> 
> call_operator.pxd:
> ----
> cdef extern from "call_operator.hpp" nogil:
>     cdef cppclass OperatorTest:
>         int operator()()
> ----
> 
> test_call_operator.pyx:
> ----
> from call_operator cimport OperatorTest
> def test():
>     cdef OperatorTest t
>     t()
> ----
> 
> Running "cython --cplus test_call_operator.pyx" generates the following
> code for the test() function:
> 
> ---- (relevant part only)
>   /* "test_call_operator.pyx":4
>  * def test():
>  *     cdef OperatorTest t
>  *     t()             # <<<<<<<<<<<<<<
>  */
>   operator()();
> ----
> As you can see, the code that is generated is a call to "operator()()" and
> not "t()" as it should be.

Thanks for the report, I can confirm this.


> From what I've been able to work out, the problem seems to be that the call
> to "t()" is treated as a NameNode in ExprNodes.py and not an AttributeNode.
> However, I don't know enough about Cython's internals to track where
> exactly this decision is made.
> 
> Curiously, this bug isn't always triggered in more complex situations. I
> had a larger pxd file with multiple external declarations where one class
> operator() was treated correctly, while others weren't. I haven't been able
> to find out why this was the case.

It happens only with stack allocated C++ objects, not with heap objects
(i.e. pointers). That can be used as a work-around, I guess.

Stefan



More information about the cython-devel mailing list