Can't generate xml report if some source file's path has unicode characters · Issue #573 · nedbat/coveragepy (original) (raw)

Originally reported by Dongmin Kim (Bitbucket: kim135797531, GitHub: kim135797531)


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

#!bash
~/temp/$ coverage xml # No error.
~/temp/테스트/$ coverage xml # Error.
#!python

/usr/lib/python2.7/genericpath.py:71: 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>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 756, in main
    status = CoverageScript().command_line(argv)
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 517, in command_line
    total = self.coverage.xml_report(outfile=outfile, **report_args)
  File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 1070, in xml_report
    return reporter.report(morfs, outfile=outfile)
  File "/usr/local/lib/python2.7/dist-packages/coverage/xmlreport.py", line 76, in report
    self.report_files(self.xml_file, morfs)
  File "/usr/local/lib/python2.7/dist-packages/coverage/report.py", line 91, in report_files
    report_fn(fr, self.coverage._analyze(fr))
  File "/usr/local/lib/python2.7/dist-packages/coverage/xmlreport.py", line 166, in xml_file
    xclass.setAttribute("name", os.path.relpath(rel_name, dirname))
  File "/usr/lib/python2.7/posixpath.py", line 441, in relpath
    i = len(commonprefix([start_list, path_list]))
  File "/usr/lib/python2.7/genericpath.py", 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/xmlreport.py 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.

#!python
        xlines = self.xml_out.createElement("lines")
        xclass.appendChild(xlines)

        # 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")