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.

Title: int.to_bytes and int.from_bytes should default to the system byte order like the struct module does
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: alex.henrie, benjamin.peterson, josh.r
Priority: normal Keywords: patch

Created on 2018-10-04 01:24 by alex.henrie, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9694 closed alex.henrie, 2018-10-04 02:37
Messages (3)
msg327030 - (view) Author: Alex Henrie (alex.henrie) * Date: 2018-10-04 01:24
When serializing a single integer, int.to_bytes and int.from_bytes are more efficient alternatives to struct.pack and struct.unpack. However, struct.pack and struct.unpack currently have the advantage that the byteorder does not have to be specified (because it defaults to sys.byteorder). It would avoid a lot of redundant code to make the byteorder argument default to sys.byteorder in int.to_bytes and int.from_bytes too.
msg327033 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-10-04 02:39
to_bytes and from_bytes aren't remotely related to native primitive types, struct is. If the associated lengths aren't 2, 4 or 8, there is no real correlation with system level primitives, and providing these defaults makes it easy to accidentally write non-portable code.

Providing a default might make sense, but if you do, it should be a fixed default (so output is portable). Making it depend on the system byte order for no real reason aside from "so I can do struct-like things faster in a non-struct way" is not a valid reason to make a behavior both implicit and inconsistent.
msg327034 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-10-04 02:51
It may be acceptable to allow for byteorder="native", but making the default native will make it too easy for people to write code that works great on their machine but not machines with the opposite endianess. Byte order is something you should explicitly think about everything you serialize an integer. So, I'm going to reject this proposal as stated.
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79070
2018-10-04 02:51:41benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg327034

resolution: rejected
stage: patch review -> resolved
2018-10-04 02:39:55josh.rsetnosy: + josh.r
messages: + msg327033
2018-10-04 02:37:02alex.henriesetkeywords: + patch
stage: patch review
pull_requests: + pull_request9081
2018-10-04 01:24:08alex.henriecreate