Can't generate xml report if some source file's path has unicode characters

Issue #573 resolved
Dongmin Kim created an issue

If source file's path has any unicode characters in it, the dump into the xml will fail on Python 2.

~/temp/$ coverage xml # No error.
~/temp/테스트/$ coverage xml # Error.
/usr/lib/python2.7/ UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  s1 = min(m)
Traceback (most recent call last):
  File "/usr/local/bin/coverage", line 11, in <module>
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 756, in main
    status = CoverageScript().command_line(argv)
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 517, in command_line
    total = self.coverage.xml_report(outfile=outfile, **report_args)
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 1070, in xml_report
    return, outfile=outfile)
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 76, in report
    self.report_files(self.xml_file, morfs)
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 91, in report_files
    report_fn(fr, self.coverage._analyze(fr))
  File "/usr/local/lib/python2.7/dist-packages/coverage/", line 166, in xml_file
    xclass.setAttribute("name", os.path.relpath(rel_name, dirname))
  File "/usr/lib/python2.7/", line 441, in relpath
    i = len(commonprefix([start_list, path_list]))
  File "/usr/lib/python2.7/", line 71, in commonprefix
    s1 = min(m)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 0: ordinal not in range(128)

Line 168 in coverage/ makes this exception. It seems to problem of unicode handling issue with Python 2.

Anyway I could fix this problem by casting dirname to unicode. This fix is a kind of quick hack, so I hope this suggestion helps to find the root cause of this problem.

        xlines = self.xml_out.createElement("lines")

        # Fix for unicode error.
        if env.PY2:
            dirname = unicode(dirname)

        xclass.setAttribute("name", os.path.relpath(rel_name, dirname))
        xclass.setAttribute("filename", fr.relative_filename().replace("\\", "/"))
        xclass.setAttribute("complexity", "0")

Comments (3)

  1. Log in to comment