Skip to content

Commit

Permalink
Ensure consistent encoding for md5sum generation (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdhiscocks committed Aug 7, 2023
1 parent 2e1e2a8 commit 0d472b7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sphinx_gallery/utils.py
Expand Up @@ -143,11 +143,14 @@ def get_md5sum(src_file, mode='b'):
File mode to open file with. When in text mode, universal line endings
are used to ensure consistency in hashes between platforms.
"""
errors = 'surrogateescape' if mode == 't' else None
with open(src_file, 'r' + mode, errors=errors) as src_data:
if mode == 't':
kwargs = {'errors': 'surrogateescape', 'encoding': 'utf-8'}
else:
kwargs = {}
with open(src_file, 'r' + mode, **kwargs) as src_data:
src_content = src_data.read()
if mode == 't':
src_content = src_content.encode(errors=errors)
src_content = src_content.encode(**kwargs)
return hashlib.md5(src_content).hexdigest()


Expand Down

0 comments on commit 0d472b7

Please sign in to comment.