Skip to content

Fix TypeError: Strings must be encoded before hashing #1964

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

Closed
wants to merge 1 commit into from

Conversation

apetti1920
Copy link

@apetti1920 apetti1920 commented May 9, 2025

This PR fixes an issue that occurs when using pytest with both coverage and pytest-testmon plugins. The error happens in the combine_parallel_data function when hasher.update(fobj.read()) tries to hash content that might sometimes be a string instead of bytes, despite opening the file in binary mode.

INTERNALERROR> Traceback (most recent call last):
...
INTERNALERROR> File "/opt/venv/lib/python3.10/site-packages/coverage/data.py", line 158, in combine_parallel_data
INTERNALERROR> hasher.update(fobj.read())
INTERNALERROR> TypeError: Strings must be encoded before hashing

This just adds a simple check to ensure the content is always bytes before passing it to the hasher's update method. This is a defensive programming approach that makes the code more robust against unexpected input types.

@nedbat
Copy link
Owner

nedbat commented May 9, 2025

Thanks. I don't understand who is calling what in the wrong way. Can you give me a simple way to reproduce the error?

@nedbat
Copy link
Owner

nedbat commented May 9, 2025

In particular, I am curious how this can be true:

content that might sometimes be a string instead of bytes, despite opening the file in binary mode.

@nedbat
Copy link
Owner

nedbat commented May 9, 2025

In fact, the more I look at this, the less I want to accept this change. On the face of it, a file opened in "rb" mode can only produce bytes. I know you are getting this actual behavior, but I suspect some component here is inappropriately fiddling with the open() builtin, or some other weirdness.

@apetti1920
Copy link
Author

In fact, the more I look at this, the less I want to accept this change. On the face of it, a file opened in "rb" mode can only produce bytes. I know you are getting this actual behavior, but I suspect some component here is inappropriately fiddling with the open() builtin, or some other weirdness.

This is exactly what it was, one of our tests patched out builtin.open and some interaction between coverage, testmon and the patch caused the .coverage file to become corrupted. Sorry for the PR and thanks for all the work on coverage!

@apetti1920 apetti1920 closed this May 11, 2025
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.

2 participants