Commit f10da56e authored by Jason R. Coombs's avatar Jason R. Coombs

Merge fix for issue #719.

parents 2b5937f5 69061481
...@@ -13,6 +13,9 @@ v28.0.0 ...@@ -13,6 +13,9 @@ v28.0.0
* #795: Bump certifi. * #795: Bump certifi.
* #719: Suppress decoding errors and instead log a warning
when metadata cannot be decoded.
v27.3.1 v27.3.1
------- -------
......
# coding: utf-8
""" """
Package resource API Package resource API
-------------------- --------------------
...@@ -1858,17 +1860,20 @@ class FileMetadata(EmptyProvider): ...@@ -1858,17 +1860,20 @@ class FileMetadata(EmptyProvider):
def get_metadata(self, name): def get_metadata(self, name):
if name == 'PKG-INFO': if name == 'PKG-INFO':
with io.open(self.path, encoding='utf-8') as f: with io.open(self.path, encoding='utf-8', errors="replace") as f:
try: metadata = f.read()
metadata = f.read() self._warn_on_replacement(metadata)
except UnicodeDecodeError as exc:
# add path context to error message
tmpl = " in {self.path}"
exc.reason += tmpl.format(self=self)
raise
return metadata return metadata
raise KeyError("No metadata except PKG-INFO is available") raise KeyError("No metadata except PKG-INFO is available")
def _warn_on_replacement(self, metadata):
# Python 2.6 and 3.2 compat for: replacement_char = '�'
replacement_char = b'\xef\xbf\xbd'.decode('utf-8')
if replacement_char in metadata:
tmpl = "{self.path} could not be properly decoded in UTF-8"
msg = tmpl.format(**locals())
warnings.warn(msg)
def get_metadata_lines(self, name): def get_metadata_lines(self, name):
return yield_lines(self.get_metadata(name)) return yield_lines(self.get_metadata(name))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment