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: int.to_bytes docs logic error
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Ofekmeister, docs@python, mark.dickinson, methane, python-dev
Priority: normal Keywords: easy, patch

Created on 2016-10-29 00:08 by Ofekmeister, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
28553.patch methane, 2016-10-30 18:59 review
Messages (4)
msg279641 - (view) Author: Ofek Lev (Ofekmeister) * Date: 2016-10-29 00:08
https://docs.python.org/3/library/stdtypes.html#int.to_bytes

To convert an int to the exact number of bytes required, the docs recommend "x.to_bytes((x.bit_length() // 8) + 1, ...)". This is incorrect when length is a multiple of 8, e.g. 296.

The correct way is "x.to_bytes((x.bit_length() + 7) // 8, ...)".
msg279741 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2016-10-30 18:59
Make sense.
msg279770 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-10-31 08:05
Patch LGTM
msg279772 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-31 08:42
New changeset 32d8c89e90d1 by INADA Naoki in branch '3.5':
Issue #28553: Fix logic error in example code of int.to_bytes doc.
https://hg.python.org/cpython/rev/32d8c89e90d1

New changeset 2ae3f1599c34 by INADA Naoki in branch '3.6':
Issue #28553: Fix logic error in example code of int.to_bytes doc.
https://hg.python.org/cpython/rev/2ae3f1599c34

New changeset 66f255754ce9 by INADA Naoki in branch 'default':
Issue #28553: Fix logic error in example code of int.to_bytes doc.
https://hg.python.org/cpython/rev/66f255754ce9
History
Date User Action Args
2022-04-11 14:58:38adminsetgithub: 72739
2016-10-31 08:43:15methanesetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2016-10-31 08:42:32python-devsetnosy: + python-dev
messages: + msg279772
2016-10-31 08:05:08mark.dickinsonsetmessages: + msg279770
2016-10-30 19:14:38serhiy.storchakasetnosy: + mark.dickinson
2016-10-30 18:59:41methanesetfiles: + 28553.patch

versions: - Python 3.3, Python 3.4
keywords: + easy, patch
nosy: + methane

messages: + msg279741
stage: commit review
2016-10-29 00:08:04Ofekmeistercreate