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
commit13782a65a275b6f57a25af1fff235bfa7bdd85b7 (patch)
treef50a8de597a52b2c588d485992fbc5ae17bc7380 /coverage/backward.py
parentf8c5d6f541eac387184c7d3ec830792551fb505f (diff)
downloadpython-coveragepy-13782a65a275b6f57a25af1fff235bfa7bdd85b7.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 f0a34ac..31668e7 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