diff options
Diffstat (limited to 'coverage/backward.py')
-rw-r--r-- | coverage/backward.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index c363f21f..f0a34ac4 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -81,3 +81,30 @@ except AttributeError: """Open a source file the best way.""" return open(fname, "rU") +# Python 3.x is picky about bytes and strings, so provide methods to +# get them right, and make them no-ops in 2.x +if sys.version_info >= (3, 0): + def to_bytes(s): + """Convert string `s` to bytes.""" + return s.encode('utf8') + + def to_string(b): + """Convert bytes `b` to a string.""" + return b.decode('utf8') + +else: + def to_bytes(s): + """Convert string `s` to bytes (no-op in 2.x).""" + return s + + def to_string(b): + """Convert bytes `b` to a string (no-op in 2.x).""" + return b + +# Md5 is available in different places. +try: + import hashlib + md5 = hashlib.md5 +except ImportError: + import md5 + md5 = md5.new |