summaryrefslogtreecommitdiff
path: root/coverage/backward.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2011-08-17 09:40:21 -0400
committerNed Batchelder <ned@nedbatchelder.com>2011-08-17 09:40:21 -0400
commit4293f61c09034945a852af00b2ac04207e64e82e (patch)
tree9d9a4aecf99c1d118a67ea510501e764ad297628 /coverage/backward.py
parent8e1e857632f8fc9c49c3aaada3b0ae0630fa4fbd (diff)
downloadpython-coveragepy-git-4293f61c09034945a852af00b2ac04207e64e82e.tar.gz
Properly handle Unicode characters in Python source when creating the HTML report (except surrogates come out as pairs of blots in the HTML). Fixes #124 and #144.
Diffstat (limited to 'coverage/backward.py')
-rw-r--r--coverage/backward.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index f0a34ac4..31668e7f 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -101,6 +101,27 @@ else:
"""Convert bytes `b` to a string (no-op in 2.x)."""
return b
+# A few details about writing encoded text are different in 2.x and 3.x.
+if sys.version_info >= (3, 0):
+ def write_encoded(fname, text, encoding='utf8', errors='strict'):
+ '''Write string `text` to file names `fname`, with encoding.'''
+ # Don't use "with", so that this file is still good for old 2.x.
+ f = open(fname, 'w', encoding=encoding, errors=errors)
+ try:
+ f.write(text)
+ finally:
+ f.close()
+else:
+ # It's not clear that using utf8 strings in 2.x is the right thing to do.
+ def write_encoded(fname, text, encoding='utf8', errors='strict'):
+ '''Write utf8 string `text` to file names `fname`, with encoding.'''
+ import codecs
+ f = codecs.open(fname, 'w', encoding=encoding, errors=errors)
+ try:
+ f.write(text.decode('utf8'))
+ finally:
+ f.close()
+
# Md5 is available in different places.
try:
import hashlib