nedbat/coveragepy (original) (raw)

Originally reported by Tom Callaway (Bitbucket: spotrh, GitHub: spotrh)


The annotate command chokes if unicode characters are parsed in the file. Here's a sample file (test.py):

#!python

# -*- coding: utf-8 -*-
#
# é

print "foobar"
[spot@localhost master]$ coverage run test.py
foobar
[spot@localhost master]$ coverage annotate -d covdir
Traceback (most recent call last):
  File "/usr/bin/coverage", line 9, in <module>
    load_entry_point('coverage==4.0a5', 'console_scripts', 'coverage')()
  File "/usr/lib64/python2.7/site-packages/coverage/cmdline.py", line 676, in main
    status = CoverageScript().command_line(argv)
  File "/usr/lib64/python2.7/site-packages/coverage/cmdline.py", line 451, in command_line
    directory=options.directory, **report_args)
  File "/usr/lib64/python2.7/site-packages/coverage/control.py", line 924, in annotate
    reporter.report(morfs, directory=directory)
  File "/usr/lib64/python2.7/site-packages/coverage/annotate.py", line 42, in report
    self.report_files(self.annotate_file, morfs, directory)
  File "/usr/lib64/python2.7/site-packages/coverage/report.py", line 80, in report_files
    report_fn(cu, self.coverage._analyze(cu))
  File "/usr/lib64/python2.7/site-packages/coverage/annotate.py", line 92, in annotate_file
    dest.write(line)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)

It looks like the obvious fix is to force encode the line being written to utf-8, a patch is attached against 4.0a5 which does this. This resolves the issue in my testing, but I wanted to make sure I had it right first.

Note: I do not see this issue in the 3.7 code, only in 4.0.