Supporting << and >> operators in C extension type

Pedro Werneck pedro.werneck at terra.com.br
Wed Jul 27 15:35:22 EDT 2005


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(PinObject *self, PyObject *args){

  PinObject *result;
  int shift;

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

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

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

The part of the PyNumberMethods struct:

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

  (inquiry)Pin_nonzero,      /*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



More information about the Python-list mailing list