Cython: Passing ctuples as arguments to a class function

Created on 14 Jul 2020  Â·  5Comments  Â·  Source: cython/cython

When passing a ctuple as an argument, the compiler complains:

C:\Users\bronx\.ipython\cython\_cython_magic_c30ac3a38886cdf8a43eb529120ef870.c(1300): error C2440: 'type cast': cannot convert from '__pyx_ctuple_int__and_int__and_int' to '__pyx_ctuple_int__and_int__and_int'
C:\Users\bronx\.ipython\cython\_cython_magic_c30ac3a38886cdf8a43eb529120ef870.c(1300): error C2198: '__pyx_pf_46_cython_magic_c30ac3a38886cdf8a43eb529120ef870_9someClass___iadd__': too few arguments for call

Posting here because this feels like a bug. You can pass ctuples as arguments to a function but not when classes are involved.

```%%cython
cdef class someClass:
cdef:
int r
def __iadd__(self, (int,int,int)c):
self.r = self.r + c[0] +c[1] + c[2]
print(self.r)
return self
cdef (int,int,int) p = (1,2,3)
r = someClass()
r+= p

All 5 comments

It seems to work for me with current master branch and 0.29.20. Are you using an older version of Cython? Or might this be an IPython magic issue?

Or an MSVC issue. @harambe623, which version of Python, Cython and Visual Studio / Visual-C / C compiler are you using?

Hmm it compiled on my debian machine. Closing this because MSVC must be the issue, though it is the latest (v142)... I will seek out a different compiler.

Maybe you could also try to investigate this a little more. There might be
a way for us to generate code that also works in MSVC here. Wouldn't be the
first time…

Got it! I was skeptical at first but it turns out that adding _# distutils: language = c++_ at the beginning will compile it without error

```

distutils: language = c++

cdef class someClass:
cdef:
int r
def __iadd__(self, (int,int,int)c):
self.r = self.r + c[0] +c[1] + c[2]
print(self.r)
return self
cdef (int,int,int) p = (1,2,3)
r = someClass()
r+= p

Was this page helpful?
0 / 5 - 0 ratings