diff options
author | Benjamin Peterson <benjamin@python.org> | 2015-03-21 09:16:35 -0500 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2015-03-21 09:16:35 -0500 |
commit | 36eed21b82fcac09fbfd85e2c6a7a1e2fb9f909d (patch) | |
tree | 12c989f2716e70ca7e7581c09ced0e2da50a08ce | |
parent | b1dfdd29e9c48abd7bd6e743eebc345ea6fe3861 (diff) | |
download | six-36eed21b82fcac09fbfd85e2c6a7a1e2fb9f909d.tar.gz |
improve performance of int2byte on python 3 (fixes #122)
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | six.py | 9 | ||||
-rw-r--r-- | test_six.py | 2 |
3 files changed, 6 insertions, 7 deletions
@@ -6,6 +6,8 @@ This file lists the changes in each six version. Development version ------------------- +- Issue #122: Improve the performance of `six.int2byte` on Python 3. + - Pull request #55 and issue #99: Don't add the `winreg` module to `six.moves` on non-Windows platforms. @@ -622,12 +622,9 @@ if PY3: def u(s): return s unichr = chr - if sys.version_info[1] <= 1: - def int2byte(i): - return bytes((i,)) - else: - # This is about 2x faster than the implementation above on 3.2+ - int2byte = operator.methodcaller("to_bytes", 1, "big") + import struct + int2byte = struct.Struct(">B").pack + del struct byte2int = operator.itemgetter(0) indexbytes = operator.getitem iterbytes = iter diff --git a/test_six.py b/test_six.py index 060a966..3e57f49 100644 --- a/test_six.py +++ b/test_six.py @@ -511,7 +511,7 @@ def test_unichr(): def test_int2byte(): assert six.int2byte(3) == six.b("\x03") - py.test.raises((OverflowError, ValueError), six.int2byte, 256) + py.test.raises(Exception, six.int2byte, 256) def test_byte2int(): |