diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-28 10:07:55 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-28 10:07:55 -0400 |
commit | a9739749d841818a31ae956fe02e0b3f03a82a31 (patch) | |
tree | 807180ce6c7df203b926d414322c9d0c84b71dff /coverage/backward.py | |
parent | 702d355f9bd2c218a7932e33fe3f587a8f7e3035 (diff) | |
download | python-coveragepy-a9739749d841818a31ae956fe02e0b3f03a82a31.tar.gz |
More abstractions for bytes objects. Cleans up some version checks in the real code.
Diffstat (limited to 'coverage/backward.py')
-rw-r--r-- | coverage/backward.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 4894bac..e782284 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -140,6 +140,19 @@ if sys.version_info >= (3, 0): """Convert bytes `b` to a string.""" return b.decode('utf8') + def binary_bytes(byte_values): + """Produce a byte string with the ints from `byte_values`.""" + return bytes(byte_values) + + def byte_to_int(byte_value): + """Turn an element of a bytes object into an int.""" + return byte_value + + def bytes_to_ints(bytes_value): + """Turn a bytes object into a sequence of ints.""" + # In Py3, iterating bytes gives ints. + return bytes_value + else: def to_bytes(s): """Convert string `s` to bytes (no-op in 2.x).""" @@ -149,6 +162,19 @@ else: """Convert bytes `b` to a string (no-op in 2.x).""" return b + def binary_bytes(byte_values): + """Produce a byte string with the ints from `byte_values`.""" + return "".join(chr(b) for b in byte_values) + + def byte_to_int(byte_value): + """Turn an element of a bytes object into an int.""" + return ord(byte_value) + + def bytes_to_ints(bytes_value): + """Turn a bytes object into a sequence of ints.""" + for byte in bytes_value: + yield ord(byte) + # Md5 is available in different places. try: import hashlib |