473,587 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ot her);
...
/* 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 2782
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(ot her);
...
/* 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_CHEC KTYPES in your extension type (in the tp_flags slot).

from object.h:
/* PyNumberMethods do their own coercion */
#define Py_TPFLAGS_CHEC KTYPES (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_CHEC KTYPES in your extension type (in the
tp_flags slot).

from object.h:
* /* PyNumberMethods do their own coercion */
* #define Py_TPFLAGS_CHEC KTYPES (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
3378
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 every element has the type of some complexType. <?xml version="1.0" encoding="UTF-8"?>
2
1281
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
4067
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 click this link, the download window shows me the file name with a .ppt extension instead of the original .pps extension. I know both are Power Point...
4
6010
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 extension. The code like this: public string HelloWorld() { return "Hello World.";
0
1063
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"> <xs:sequence> <xs:element name="Path" type="xs:string" maxOccurs="1" minOccurs="1" /> <xs:element name="Files" type="xs:string" minOccurs="0"...
6
2792
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
2130
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" targetNamespace="http://swaMaragatoNS" xmlns:tns="http://swaMaragatoNS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
25
3478
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
2093
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 not recognized). I am using the 'standard' using directives using System;using System.Collections.Generic;using System.Diagnostics;using...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.