Opened 4 years ago

Closed 4 years ago

#6053 enhancement closed fixed (fixed)

Create `intToBytes` function for Python 3 compatibility with Python 2

Reported by: Itamar Turner-Trauring Owned by: Itamar Turner-Trauring
Priority: normal Milestone: Python 3.3 Minimal
Component: core Keywords:
Cc: Branch: branches/toBytes-py3-6053
branch-diff, diff-cov, branch-cov, buildbot
Author: itamarst

Description (last modified by Itamar Turner-Trauring)

On Python 2, it's possible to do things like:

>>> bytes(20)
'20'

On Python 3 that will not work. Since we use the above idiom quite a bit, at the very least in tests, we should implement a intToBytes function that emulates this behavior.

Change History (7)

comment:1 Changed 4 years ago by Itamar Turner-Trauring

Keywords: review added
Owner: changed from Itamar Turner-Trauring to Jean-Paul Calderone

http://buildbot.twistedmatrix.com/boxes-supported?branch=/branches/toBytes-py3-6053

I couldn't think of a better way to the full compatibility, unfortunately. We're going to need this if we ever do %s support, but it's easy enough to drop as YAGNI. If we do drop it, we should probably make this API private, since it's easy to get it wrong if you use it for anything other than integers.

comment:2 Changed 4 years ago by itamarst

Author: itamarst
Branch: branches/toBytes-py3-6053

(In [35943]) Branching to 'toBytes-py3-6053'

comment:3 Changed 4 years ago by Jean-Paul Calderone

Keywords: review removed
Owner: changed from Jean-Paul Calderone to Itamar Turner-Trauring
  1. Using memoryview for this seems like a bad idea. Exactly three types are supported (exactly three types are unit tested). Wandering into the "buffer API" realm is just asking for trouble. All we really need is a function for getting a bytes-representation of a few very simple types. Who knows what dragging memoryview into the picture is going to do? Let's implement and advertise this as a very simple function of limited capabilities. If it really turns out to be necessary, we can expand that simple functionality later.
  2. intToBytes, listToBytes might be an alternative approach that suggests different (slightly better) names.
  3. __all__ needs to be updated.

comment:4 Changed 4 years ago by itamarst

(In [35971]) Replace toBytes with intToBytes. Refs #6053

comment:5 Changed 4 years ago by Itamar Turner-Trauring

Description: modified (diff)
Keywords: review added
Owner: changed from Itamar Turner-Trauring to Jean-Paul Calderone
Summary: Create `toBytes` function for Python 3 compatibility with Python 2Create `intToBytes` function for Python 3 compatibility with Python 2

comment:6 Changed 4 years ago by Jean-Paul Calderone

Keywords: review removed
Owner: changed from Jean-Paul Calderone to Itamar Turner-Trauring

Looks good, please merge.

comment:7 Changed 4 years ago by itamarst

Resolution: fixed
Status: newclosed

(In [35984]) Merge toBytes-py3-6053.

Author: itamar Review: exarkun Fixes: #6053

intToBytes utility function.

Note: See TracTickets for help on using tickets.