diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2016-09-27 14:16:26 -0500 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2016-09-27 14:16:26 -0500 |
commit | f10da56e220c2f06126f2aa60ccfbfdddbfa6075 (patch) | |
tree | 8145b8f80e23eed49f59dc60c3124540462e5fe1 | |
parent | 2b5937f56b9b1d8d91c1247540f41437ba99016e (diff) | |
parent | 69061481e345bfd1f1d07795b3541c07e498d2df (diff) | |
download | external_python_setuptools-f10da56e220c2f06126f2aa60ccfbfdddbfa6075.tar.gz external_python_setuptools-f10da56e220c2f06126f2aa60ccfbfdddbfa6075.tar.bz2 external_python_setuptools-f10da56e220c2f06126f2aa60ccfbfdddbfa6075.zip |
Merge fix for issue #719.
-rw-r--r-- | CHANGES.rst | 3 | ||||
-rw-r--r-- | pkg_resources/__init__.py | 21 |
2 files changed, 16 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 0ce8e52b..7c4ff813 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,6 +13,9 @@ v28.0.0 * #795: Bump certifi. +* #719: Suppress decoding errors and instead log a warning + when metadata cannot be decoded. + v27.3.1 ------- diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 4208c4ec..af986ac3 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -1,3 +1,5 @@ +# coding: utf-8 + """ Package resource API -------------------- @@ -1858,17 +1860,20 @@ class FileMetadata(EmptyProvider): def get_metadata(self, name): if name == 'PKG-INFO': - with io.open(self.path, encoding='utf-8') as f: - try: - metadata = f.read() - except UnicodeDecodeError as exc: - # add path context to error message - tmpl = " in {self.path}" - exc.reason += tmpl.format(self=self) - raise + with io.open(self.path, encoding='utf-8', errors="replace") as f: + metadata = f.read() + self._warn_on_replacement(metadata) return metadata 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): return yield_lines(self.get_metadata(name)) |