[docs] [issue17576] PyNumber_Index() is not int-subclass friendly (or operator.index() docos lie)

Serhiy Storchaka report at bugs.python.org
Wed Dec 11 21:02:12 CET 2013

Serhiy Storchaka added the comment:

I first committed safe part of patch, which doesn't change behavior, only adds warnings and tests. Here is other part (very simple), which change behavior (in sum they are equal to issue17576_v3.patch with minor changes).

* PyNumber_Index() now calls __index__() for int subclasses.

>>> class A(int):
...     def __index__(self): return 1
>>> import operator
>>> operator.index(A())

Returns 0 without patch and 1 with patch.

* PyNumber_Long() now calls __int__() on result of __trunc__() if it is int subclass instance.

>>> class A:
...     def __trunc__(self): return True
>>> int(A())

Returns True without patch and 1 with patch.

* PyLong_As*() functions (but not all) call __int__() for int subclasses (I'm not sure this is right).

>>> class A(int):
...     def __int__(self): return 42
>>> chr(A(43))

Returns '+' without patch and '*' with patch.

Added file: http://bugs.python.org/file33093/issue17576_v4.patch

Python tracker <report at bugs.python.org>

More information about the docs mailing list