C extension type gives type error in power operator

P

Paul Moore

I'm trying to implement an extension type with a power operator. The
operator is unusual in that I want to allow my objects to be raised to
an integer power:

p = Pattern()
p3 = p ** 3

I've implemented the code for a nb_power slot, it converts the "other"
argument to a C long using PyInt_AsLong().

static PyObject *
Pattern_pow (PyObject *self, PyObject *other, PyObject *modulo)
{
long n = PyInt_AsLong(other);
...
/* Ignore modulo argument - not meaningful */

if (n == -1 && PyErr_Occurred())
return NULL;
....
}

However, when I try to use the operator, I get the following error:
TypeError: unsupported operand type(s) for ** or pow():
'_ppeg.Pattern' and 'int'

I'm not sure where this error is coming from, as I don't have any type
checks in my code. Is there something else I should add to allow mixed-
type operations? (This is Python 2.5, if it matters).

Oh, and is there a good reference for writing C extensions for Python
anywhere? The manuals aren't bad, but I keep hitting empty sections
(e.g., 10.5 Number Object Structures).

Thanks
Paul.
 
T

Thomas Heller

Paul said:
I'm trying to implement an extension type with a power operator. The
operator is unusual in that I want to allow my objects to be raised to
an integer power:

p = Pattern()
p3 = p ** 3

I've implemented the code for a nb_power slot, it converts the "other"
argument to a C long using PyInt_AsLong().

static PyObject *
Pattern_pow (PyObject *self, PyObject *other, PyObject *modulo)
{
long n = PyInt_AsLong(other);
...
/* Ignore modulo argument - not meaningful */

if (n == -1 && PyErr_Occurred())
return NULL;
...
}

However, when I try to use the operator, I get the following error:
TypeError: unsupported operand type(s) for ** or pow():
'_ppeg.Pattern' and 'int'

Try to set Py_TPFLAGS_CHECKTYPES in your extension type (in the tp_flags slot).

from object.h:
/* PyNumberMethods do their own coercion */
#define Py_TPFLAGS_CHECKTYPES (1L<<4)
I'm not sure where this error is coming from, as I don't have any type
checks in my code. Is there something else I should add to allow mixed-
type operations? (This is Python 2.5, if it matters).

Oh, and is there a good reference for writing C extensions for Python
anywhere? The manuals aren't bad, but I keep hitting empty sections
(e.g., 10.5 Number Object Structures).

I normally look into the newest python reference manual, even if I'm
programming for older versions. But sometimes you have to took into
the header files, too.

Thomas
 
P

Paul Moore

Try to set Py_TPFLAGS_CHECKTYPES in your extension type (in the
tp_flags slot).

from object.h:
  /* PyNumberMethods do their own coercion */
  #define Py_TPFLAGS_CHECKTYPES (1L<<4)

Excellent! That did exactly what I wanted. (I wonder how it affects my
other operations - I'll look into that, it probably helps in ways I
hadn't suspected).
I normally look into the newest python reference manual, even if
I'm programming for older versions.  But sometimes you have to
took into the header files, too.

This particular issue was in the 2.6 manual. I hadn't thought to check
there. Add "browse the 2.6 manuals" to my ever-increasing todo
list :) Or maybe just get round to installing 2.6, and be done with
it.

Or there's always ask the experts on clp :) (I did at least look in
the sources, but never thought of checking the type flags).

Thanks,
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top