473,659 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Supporting << and >> operators in C extension type


Hi list
I'm trying to implement a new type in a C extension and it must support
some binary operators, like &, |, ^, << and >>. With &, | and ^, the
method must receive another object of the same type, perform the
operation with an attribute of both, create a new object with the result
as the attribute and return it.

Everything works perfectly with &, | and ^, but with << and >> I need
to, pass an integer as argument, not an object of the same type. The
method should receive an integer, perform the shift with the attribute,
create the new object and return it. The problem is that it only works
with another object of the same type.

It's strange because obj.__lshift__ returns the method; when I call
obj.__lshift__( a) or obj << a, when a is an object of the same type, the
call suceeds (but the object returned has the wrong value, of course),
but with obj.__lshift__( i) where i is an integer, the call returns
NotImplemented and with obj << i it raises TypeError: unsupported
operand types for <<.

I searched the Objects/ directory in the interpreter source code, but
could not find any solution for this. Seems like it does some kind of
type-checking somewhere when using these binary operators. Do I need to
implement the __coerce__ method or use some wrapper with the method
function ?

Here is the relevant piece of my source code:

The __lshift__ method:

/* Pin.__lshift__( ) */
static PyObject *
Pin_oplshift(Pi nObject *self, PyObject *args){

PinObject *result;
int shift;

if (!PyArg_ParseTu ple(args, "i", &shift))
return NULL;

result = PyObject_NEW(Pi nObject, &PinType);
result->pin = self->pin << shift;

Py_INCREF(resul t);
return (PyObject *) result;
};

The part of the PyNumberMethods struct:

/* Pin number methods */
static PyNumberMethods PinAsNumber[] = {
....

(inquiry)Pin_no nzero, /*nb_nonzero*/
0, /*nb_invert*/
(binaryfunc)Pin _oplshift, /*nb_lshift*/
(binaryfunc)Pin _oprshift, /*nb_rshift*/
(binaryfunc)Pin _and, /*nb_and*/
(binaryfunc)Pin _xor, /*nb_xor*/
(binaryfunc)Pin _or, /*nb_or*/
0, /*nb_coerce*/

....
};
Thanks for any help...

--
Pedro Werneck
Jul 27 '05 #1
0 1111

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10555
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
1
1325
by: PengYu.UT | last post by:
I heard that != >= > <= operators are defined based on the operator == <. Could you tell me which header file defines the four operators? Best wishes, Peng
8
1562
by: Greenhorn | last post by:
Hi, Those relational operators have operands of all numerical type int,char,float etc. They also are working for character arrays. Whats the logic behind their working. Is the length of the array compared first and then each character compared with corresponding character. { const char msg = "msessage}", ch = "Za";
1
2039
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
1
2372
by: Baldoni | last post by:
It's been years since putting together a page. This line in my HTML: <link type="text/css" rel="stylesheet" href="net4801style.css"> gives this problem (via weblint HTML checker): attribute 'TYPE' for <link> is extended markup (use -x <extension> to do this). Where might be found a resource, for simple folk, explaining how to best correct this?
5
2411
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am left to wondering why Stroustrup doesn't mention the *_cast<> operators or operator .* in that particular FAQ. Archived discussions have convinced me that .* _cannot_ be overloaded. I have gathered a notion (perhaps incorrectly) that the *_cast<>...
3
3367
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above functionality without having to use &gt,&lt and such stuff???
3
2937
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
2
1273
by: defn noob | last post by:
What does >and << do? Googling on them and they are just ignored...
0
8337
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.