#6053 enhancement closed fixed (fixed)

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

Reported by: itamar Owned by: itamar
Priority: normal Milestone: Python 3.3 Minimal
Component: core Keywords:
Cc: Branch: branches/toBytes-py3-6053
(diff, github, buildbot, log)
Author: itamarst Launchpad Bug:

Description (last modified by itamar)

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 22 months ago by itamar

  • Keywords review added
  • Owner changed from itamar to exarkun

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 22 months ago by itamarst

  • Author set to itamarst
  • Branch set to branches/toBytes-py3-6053

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

comment:3 Changed 22 months ago by exarkun

  • Keywords review removed
  • Owner changed from exarkun to itamar
  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 22 months ago by itamarst

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

comment:5 Changed 22 months ago by itamar

  • Description modified (diff)
  • Keywords review added
  • Owner changed from itamar to exarkun
  • Summary changed from Create `toBytes` function for Python 3 compatibility with Python 2 to Create `intToBytes` function for Python 3 compatibility with Python 2

comment:6 Changed 22 months ago by exarkun

  • Keywords review removed
  • Owner changed from exarkun to itamar

Looks good, please merge.

comment:7 Changed 22 months ago by itamarst

  • Resolution set to fixed
  • Status changed from new to closed

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

Author: itamar
Review: exarkun
Fixes: #6053

intToBytes utility function.

Note: See TracTickets for help on using tickets.