By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

C extension type gives type error in power operator

P: n/a
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.
Nov 20 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Paul Moore schrieb:
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
Nov 20 '08 #2

P: n/a
On 20 Nov, 15:43, Thomas Heller <thel...@python.netwrote:
Paul Moore schrieb:
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)
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).
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.
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
Nov 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.