Issue 16722: index() overrides bytes() when bytes() is called (original) (raw)

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:39 admin set github: 60926
2012-12-20 04:19:37 LRN set messages: +
2012-12-19 22:36:37 asvetlov set messages: +
2012-12-19 22:26:12 asvetlov set nosy: + asvetlovmessages: +
2012-12-19 21:56:30 benjamin.peterson set status: open -> closedresolution: fixed
2012-12-19 21:29:31 benjamin.peterson set status: closed -> openstage: resolved -> resolution: fixed -> (no value)versions: + Python 3.4
2012-12-19 21:29:10 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: resolved
2012-12-19 21:12:51 benjamin.peterson set nosy: + benjamin.petersonmessages: +
2012-12-19 05:42:51 LRN create