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

fix(python): handle source decoding errors #1164

Closed
wants to merge 1 commit into from

Conversation

P403n1x87
Copy link

As described in #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 #1160.

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.
source = source.decode(source_encoding(source), "replace")
try:
source = source.decode(source_encoding(source), "replace")
except Exception as e:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would much rather catch a more specific error, in a more specific place. The exception is happening inside source_encoding, so why not put the except-clause in that function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This felt like a good place for catching generic decoding errors and not just the one in the linked issue. Of course, I could add a specific exception handling in source_encoding if that's preferable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this is a bad place for the except is that s.decode(..., "replace") can't raise an exception. In that line of code, it's only the source_encoding() call that can fail.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh but this could be fixed by just moving source_encoding on its own line and wrapping it with try...except 😜 . Anyway, I'll have a closer look as soon as I can.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I've had another look and the idea of adding checks to _source_encoding_py2 is not very appealing. Not to mention that they would have to be duplicated in _source_encoding_py3.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, _source_encoding_py2 is gone on master, we've dropped Python 2 support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I've just realised that support for Python 3.5 is also being dropped, which is not ideal for our usage. This prompted me to test on Python 3.7 with the master branch of coveragepy and the problem does not present itself 🙁 .

@P403n1x87
Copy link
Author

This has now been fixed with #1252

@P403n1x87 P403n1x87 closed this Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

get_python_source gets called on shared object file and causes SyntaxError
2 participants