bpo-22385: Support output separators in hex methods. (#13578)
* bpo-22385: Support output separators in hex methods.
Also in binascii.hexlify aka b2a_hex.
The underlying implementation behind all hex generation in CPython uses the
same pystrhex.c implementation. This adds support to bytes, bytearray,
and memoryview objects.
The binascii module functions exist rather than being slated for deprecation
because they return bytes rather than requiring an intermediate step through a
str object.
This change was inspired by MicroPython which supports sep in its binascii
implementation (and does not yet support the .hex methods).
https://bugs.python.org/issue22385
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py
index 572e50c..08de5c9 100644
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -240,6 +240,18 @@
self.assertEqual(binascii.hexlify(self.type2test(s)), t)
self.assertEqual(binascii.unhexlify(self.type2test(t)), u)
+ def test_hex_separator(self):
+ """Test that hexlify and b2a_hex are binary versions of bytes.hex."""
+ # Logic of separators is tested in test_bytes.py. This checks that
+ # arg parsing works and exercises the direct to bytes object code
+ # path within pystrhex.c.
+ s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
+ self.assertEqual(binascii.hexlify(self.type2test(s)), s.hex().encode('ascii'))
+ expected8 = s.hex('.', 8).encode('ascii')
+ self.assertEqual(binascii.hexlify(self.type2test(s), '.', 8), expected8)
+ expected1 = s.hex(':').encode('ascii')
+ self.assertEqual(binascii.b2a_hex(self.type2test(s), ':'), expected1)
+
def test_qp(self):
type2test = self.type2test
a2b_qp = binascii.a2b_qp
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 9502a8f..bbd45c7 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -417,6 +417,63 @@
self.assertEqual(self.type2test(b"\x1a\x2b\x30").hex(), '1a2b30')
self.assertEqual(memoryview(b"\x1a\x2b\x30").hex(), '1a2b30')
+ def test_hex_separator_basics(self):
+ three_bytes = self.type2test(b'\xb9\x01\xef')
+ self.assertEqual(three_bytes.hex(), 'b901ef')
+ with self.assertRaises(ValueError):
+ three_bytes.hex('')
+ with self.assertRaises(ValueError):
+ three_bytes.hex('xx')
+ self.assertEqual(three_bytes.hex(':', 0), 'b901ef')
+ with self.assertRaises(TypeError):
+ three_bytes.hex(None, 0)
+ with self.assertRaises(ValueError):
+ three_bytes.hex('\xff')
+ with self.assertRaises(ValueError):
+ three_bytes.hex(b'\xff')
+ with self.assertRaises(ValueError):
+ three_bytes.hex(b'\x80')
+ with self.assertRaises(ValueError):
+ three_bytes.hex(chr(0x100))
+ self.assertEqual(three_bytes.hex(':', 0), 'b901ef')
+ self.assertEqual(three_bytes.hex(b'\x00'), 'b9\x0001\x00ef')
+ self.assertEqual(three_bytes.hex('\x00'), 'b9\x0001\x00ef')
+ self.assertEqual(three_bytes.hex(b'\x7f'), 'b9\x7f01\x7fef')
+ self.assertEqual(three_bytes.hex('\x7f'), 'b9\x7f01\x7fef')
+ self.assertEqual(three_bytes.hex(':', 3), 'b901ef')
+ self.assertEqual(three_bytes.hex(':', 4), 'b901ef')
+ self.assertEqual(three_bytes.hex(':', -4), 'b901ef')
+ self.assertEqual(three_bytes.hex(':'), 'b9:01:ef')
+ self.assertEqual(three_bytes.hex(b'$'), 'b9$01$ef')
+ self.assertEqual(three_bytes.hex(':', 1), 'b9:01:ef')
+ self.assertEqual(three_bytes.hex(':', -1), 'b9:01:ef')
+ self.assertEqual(three_bytes.hex(':', 2), 'b9:01ef')
+ self.assertEqual(three_bytes.hex(':', 1), 'b9:01:ef')
+ self.assertEqual(three_bytes.hex('*', -2), 'b901*ef')
+
+ value = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
+ self.assertEqual(value.hex('.', 8), '7b7305000000776f.726c646902000000.730500000068656c.6c6f690100000030')
+
+ def test_hex_separator_five_bytes(self):
+ five_bytes = self.type2test(range(90,95))
+ self.assertEqual(five_bytes.hex(), '5a5b5c5d5e')
+
+ def test_hex_separator_six_bytes(self):
+ six_bytes = self.type2test(x*3 for x in range(1, 7))
+ self.assertEqual(six_bytes.hex(), '0306090c0f12')
+ self.assertEqual(six_bytes.hex('.', 1), '03.06.09.0c.0f.12')
+ self.assertEqual(six_bytes.hex(' ', 2), '0306 090c 0f12')
+ self.assertEqual(six_bytes.hex('-', 3), '030609-0c0f12')
+ self.assertEqual(six_bytes.hex(':', 4), '0306:090c0f12')
+ self.assertEqual(six_bytes.hex(':', 5), '03:06090c0f12')
+ self.assertEqual(six_bytes.hex(':', 6), '0306090c0f12')
+ self.assertEqual(six_bytes.hex(':', 95), '0306090c0f12')
+ self.assertEqual(six_bytes.hex('_', -3), '030609_0c0f12')
+ self.assertEqual(six_bytes.hex(':', -4), '0306090c:0f12')
+ self.assertEqual(six_bytes.hex(b'@', -5), '0306090c0f@12')
+ self.assertEqual(six_bytes.hex(':', -6), '0306090c0f12')
+ self.assertEqual(six_bytes.hex(' ', -95), '0306090c0f12')
+
def test_join(self):
self.assertEqual(self.type2test(b"").join([]), b"")
self.assertEqual(self.type2test(b"").join([b""]), b"")
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index f1013f2..5ea18f5 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -665,11 +665,13 @@
True
>>> real_tests = [t for t in tests if len(t.examples) > 0]
>>> len(real_tests) # objects that actually have doctests
- 9
+ 12
>>> for t in real_tests:
... print('{} {}'.format(len(t.examples), t.name))
...
1 builtins.bin
+ 5 builtins.bytearray.hex
+ 5 builtins.bytes.hex
3 builtins.float.as_integer_ratio
2 builtins.float.fromhex
2 builtins.float.hex
@@ -677,6 +679,7 @@
1 builtins.int
3 builtins.int.as_integer_ratio
2 builtins.int.bit_length
+ 5 builtins.memoryview.hex
1 builtins.oct
Note here that 'bin', 'oct', and 'hex' are functions; 'float.as_integer_ratio',