[Python-Dev] buglet in unary ops on classes.

Thomas Wouters thomas@xs4all.net
Fri, 28 Jul 2000 18:45:12 +0200


While poking around looking for something else, I found this little buglet:

>>> class X:
...     def __getattr__(self, val):
...             print "getattr:", val
...             raise AttributeError

>>> x = X()
>>> ~x
getattr: __invert__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in __getattr__
AttributeError

The unary ops all do this (raising AttributeError), but the binary ops do
not:

>>> x+1
getattr: __coerce__
getattr: __add__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __add__ nor __radd__ defined for these operands

Shouldn't this translation be done for unary ops as well ? Is it safe to
assume that instance_getattr() only returns NULL if the attribute is not
found ? PyInstance_DoBinOp() takes this assumption wrt. PyObject_GetAttr(),
and I don't really see a problem with it.

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!