[Cython] operator() bug in cython

Ulrich Dobramysl uli-do at gmx.at
Wed Feb 11 09:56:35 CET 2015


Dear all,

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.

>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.

Ulrich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20150211/691d75df/attachment.html>


More information about the cython-devel mailing list