This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __index__() overrides __bytes__() when bytes() is called
Type: Stage:
Components: None Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: LRN, asvetlov, benjamin.peterson, python-dev
Priority: normal Keywords:

Created on 2012-12-19 05:42 by LRN, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
testb.py LRN, 2012-12-19 05:42 Example
Messages (6)
msg177725 - (view) Author: Руслан Ижбулатов (LRN) Date: 2012-12-19 05:42
If a class implements both __index__() and __bytes__(), the built-in bytes() constructor uses __index__() in place of __bytes__(), including the assumption that __index__() returns a bytes object.

Attaching a simple example (leads to overflow when big integer is casted to bytes).
msg177782 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-12-19 21:12
I'm afraid we can only fix this in 3.4 lest someone is relying on it.
msg177783 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-12-19 21:29
New changeset c744b6f8a09a by Benjamin Peterson in branch '3.3':
try to call __bytes__ before __index__ (closes #16722)
http://hg.python.org/cpython/rev/c744b6f8a09a

New changeset 7c717d423160 by Benjamin Peterson in branch 'default':
merge 3.3 (#16722)
http://hg.python.org/cpython/rev/7c717d423160
msg177786 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-19 22:26
Is it coupled with #15559? 
If true we can relax ipaddress with adding __index__ again for 3.4
msg177788 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-19 22:36
BTW, Руслан, can you change your name to use latin alphabet?
I'm Андрей Светлов, but use Andrew Svetlov for tracker.
Latin transcription is much easier to remember for all python users who don't speak Russian. For that guys your name looks like Chinese one for me.
Thanks.
msg177797 - (view) Author: Руслан Ижбулатов (LRN) Date: 2012-12-20 04:19
Yes, i saw #15559 ( well, actually, i saw the related discussion http://mail.python.org/pipermail/python-dev/2012-August/121241.html ) while looking for an answer. It's more narrow - i.e. manifestation of this issue in ipaddress case where some people actually _needed_ to have different values returned by __index__() and __bytes__(), and had to work around it.

As for my name, my username is used almost everywhere and, unlike my real name, it's easy to read and remember. The only place where usernames aren't mentioned are mails from the Python issue tracker. File a bug for it.
History
Date User Action Args
2022-04-11 14:57:39adminsetgithub: 60926
2012-12-20 04:19:37LRNsetmessages: + msg177797
2012-12-19 22:36:37asvetlovsetmessages: + msg177788
2012-12-19 22:26:12asvetlovsetnosy: + asvetlov
messages: + msg177786
2012-12-19 21:56:30benjamin.petersonsetstatus: open -> closed
resolution: fixed
2012-12-19 21:29:31benjamin.petersonsetstatus: closed -> open
stage: resolved ->
resolution: fixed -> (no value)
versions: + Python 3.4
2012-12-19 21:29:10python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg177783

resolution: fixed
stage: resolved
2012-12-19 21:12:51benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg177782
2012-12-19 05:42:51LRNcreate