[Pyrex] pyrexc error: Python object cannot be converted to __mpz_struct (?)

Greg Ewing greg at cosc.canterbury.ac.nz
Fri May 28 06:27:36 CEST 2004


Heiko Wundram <heikowu at ceosg.de>:

> cdef class GMP:
> 	cdef mpz_t mpznumber
> 
> 	def __add__(self,other):

You need to read the section on Arithmetic Methods in the
Special Methods of Extension Types page. The first
argument of __add__ is *not* necessarily 'self', and
Pyrex makes no assumptions about the type of either
argument. You shouldn't, either, because if someone
does

   3 + some_gmp

then your __add__ will be called with 3 as the first
argument and some_gmp as the second argument. So you
will need to do some type testing to find out whether
the first or second argument is of type GMP and
coerce accordingly.

> I've not found a sane way (except "if isinstance(...)", which expands
> to huge amounts of runtime python code, whereas a simple PyInt_Check()
> should suffice)

You should be able to call PyInt_Check() given a suitable
external declaration for it. Also PyObject_IsInstance()
for testing whether you have a GMP instance.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list