Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nedbat opened this issue Apr 12, 2017 · 2 comments
Closed
Labels
bug Something isn't working xml

Comments

@nedbat
Copy link
Owner

nedbat commented Apr 12, 2017

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

@nedbat
Copy link
Owner Author

nedbat commented Apr 30, 2017

Thanks for the report. The fix was even easier: 1da80e5bbdd5 (bb)

@nedbat
Copy link
Owner Author

nedbat commented Apr 30, 2017

Original comment by Dongmin Kim (Bitbucket: kim135797531, GitHub: kim135797531)


Thanks! 👍

@nedbat nedbat closed this as completed Apr 30, 2017
@nedbat nedbat added major bug Something isn't working xml labels Jun 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working xml
Projects
None yet
Development

No branches or pull requests

1 participant