[Cython] C methods broken?
Robert Bradshaw
robertwb at math.washington.edu
Wed Dec 3 06:55:29 CET 2008
On Dec 2, 2008, at 9:48 PM, Kay Schluehr wrote:
> Dear Cython list,
>
> while investigating the type semantics and constraints of Cython I
> stumbled over a problem I consider a bug. Here is some brief
> description
> using an example
>
> # point.pyx
> #
> # defines an extension type and a conversion between a C struct and an
> extension type
> #
> import random
>
> cdef struct CPoint:
> int x
> int y
>
> cdef class ExtPoint:
> cdef int x
> cdef int y
>
> cdef convert(self, CPoint pt):
> self.x = pt.x
> self.y = pt.y
>
> cdef convert(self, CPoint pt):
> self.x = pt.x
> self.y = pt.y
>
> cpdef test():
> cdef CPoint pt
> pt.x = random.randrange(100)
> pt.y = random.randrange(100)
> ep = ExtPoint()
> convert(ep, pt) # o.k - compiles
> ep.convert(pt) # n.o.k - fails to compile
> return ep
>
> # end of point.pyx
>
> The standalone convert() function equals the convert() C-method of the
> ExtPoint extension type. However the compiler accepts the function
> call
> but rejects the method call:
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
> cdef CPoint pt
> pt.x = random.randrange(100)
> pt.y = random.randrange(100)
> ep = ExtPoint()
> convert(ep, pt) # o.k - compiles
> ep.convert(pt) # n.o.k - fails to compile
> ^
> ------------------------------------------------------------
>
> point.pyx:91:17: Cannot convert 'CPoint' to Python object
>
> There is no indication why the compiler tries to perform an automatic
> type conversion since the C method isn't exposed to Python anyway.
You need to declare ep to be of type ExtPoint, i.e.
cdef ExtPoint ep = ExtPoint()
otherwise, it thinks it's just an ordinary Python object and an
ordinary python method to be looked up at runtime.
- Robert
More information about the Cython-dev
mailing list