Skip to content

Commit

Permalink
fix(python): handle source decoding errors
Browse files Browse the repository at this point in the history
As described in nedbat#1160, occasionally some shared object paths make their way into the coverage report. When they get to `get_python_source` the call to `source_encoding` fails because the content of these files is binary. The change allows `get_python_source` to handle these cases gracefully while still reporting the issue.

Fixes nedbat#1160.
  • Loading branch information
P403n1x87 authored May 24, 2021
1 parent c41e884 commit 07058c3
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def get_python_source(filename):

# Replace \f because of http://bugs.python.org/issue19035
source = source.replace(b'\f', b' ')
source = source.decode(source_encoding(source), "replace")
try:
source = source.decode(source_encoding(source), "replace")
except Exception as e:
raise NoSource("Cannot decode source '%s: %s" % (filename, e))

# Python code should always end with a line with a newline.
if source and source[-1] != '\n':
Expand Down

0 comments on commit 07058c3

Please sign in to comment.