Skip to content

Commit 1d9bfd7

Browse files
committed
Make sure pretty_hex works under Python 2 and Python 3.
* Run doctests under tox. * Add pretty_hex doctests for string, bytes and unicode types. Fixes #48
1 parent 99f87a5 commit 1d9bfd7

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

programmer/check_doctests.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
import doctest
3+
import tinyprog
4+
doctest.testmod(tinyprog)

programmer/tinyprog/__init__.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,30 @@
3333

3434

3535
def pretty_hex(data):
36-
output = ""
36+
"""
37+
>>> print(pretty_hex("abc123"))
38+
61 62 63 31 32 33
39+
>>> print(pretty_hex(b"abc123"))
40+
61 62 63 31 32 33
41+
>>> print(pretty_hex(u"abc123"))
42+
61 62 63 31 32 33
43+
>>> print(pretty_hex("\\x00a\\x02"*12))
44+
00 61 02 00 61 02 00 61 02 00 61 02 00 61 02 00
45+
61 02 00 61 02 00 61 02 00 61 02 00 61 02 00 61
46+
02 00 61 02
47+
"""
48+
output = []
3749
for i in range(0, len(data), 16):
38-
output += " ".join(["%02x" % ord(x) for x in data[i:i + 16]]) + "\n"
39-
return output
50+
output.append(" ".join("%02x" % to_int(x) for x in data[i:i + 16]))
51+
return "\n".join(output)
4052

4153

4254
def to_int(value):
4355
"""
4456
>>> to_int('A')
4557
65
4658
>>> to_int(0xff)
47-
256
59+
255
4860
>>> list(to_int(i) for i in ['T', 'i', 'n', 'y', 0xff, 0, 0])
4961
[84, 105, 110, 121, 255, 0, 0]
5062
"""

programmer/tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ commands =
1616
python setup.py --version
1717
check-manifest --ignore tox.ini,tinyprog/full_version.py
1818
python setup.py check -m -s
19+
python check_doctests.py
1920
flake8 --exclude=data_tables.py setup.py tinyprog
2021
python setup.py install
2122
tinyprog --help

0 commit comments

Comments
 (0)