summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2015-03-21 09:16:35 -0500
committerBenjamin Peterson <benjamin@python.org>2015-03-21 09:16:35 -0500
commit36eed21b82fcac09fbfd85e2c6a7a1e2fb9f909d (patch)
tree12c989f2716e70ca7e7581c09ced0e2da50a08ce
parentb1dfdd29e9c48abd7bd6e743eebc345ea6fe3861 (diff)
downloadsix-36eed21b82fcac09fbfd85e2c6a7a1e2fb9f909d.tar.gz
improve performance of int2byte on python 3 (fixes #122)
-rw-r--r--CHANGES2
-rw-r--r--six.py9
-rw-r--r--test_six.py2
3 files changed, 6 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index 259bb29..cbf4c47 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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.
diff --git a/six.py b/six.py
index baf1c3b..a104cb8 100644
--- a/six.py
+++ b/six.py
@@ -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():