473,383 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C extension type gives type error in power operator

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
2 2757
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffry van de Vuurst | last post by:
Hi, I'm working on an xml schema and I'm running into some problems relating substitutionGroups and extensions. This xsd validates fine: There are three elements and three complex types and...
2
by: Daniel Wilson | last post by:
I have an enumerated type like this: public __value enum MyType{ ABC = 1, DEF = 2, GHI = 3, JKL = 4 }; Later I have a for loop like this: for (ft = MyType::ABC; ft <= MyType::JKL; ft++){
1
by: Erik Cruz | last post by:
Hi. My application has a page where users can click on some links to download documents. When testing the page I noticed an strange behaviour. One of the links points to a .pps file. When I...
4
by: pepcag | last post by:
I used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconalteringsoapmessageusingsoapextensions.asp as a template to create a very simple web method with soap...
0
by: SL33PY | last post by:
Hi again, I feel like I'm spamming the place, but these are all the things that I encounter during work. Anyways: I made a base complex type: <xs:complexType name="BaseLocationType">...
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.