From 21efdb5615f98a60cfa0b6130a1c2d93e5faa28e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 4 Aug 2016 13:14:45 -0400 Subject: Allow an environment to suppress errors when reading metadata by setting PKG_RESOURCES_METADATA_ERRORS='replace'. Ref #719. --- pkg_resources/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'pkg_resources/__init__.py') diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 2a053b50..b402ddd6 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -1859,7 +1859,9 @@ class FileMetadata(EmptyProvider): def get_metadata(self, name): if name=='PKG-INFO': - with io.open(self.path, encoding='utf-8') as f: + env_key = 'PKG_RESOURCES_METADATA_ERRORS' + errors = os.environ.get(env_key, 'strict') + with io.open(self.path, encoding='utf-8', errors=errors) as f: try: metadata = f.read() except UnicodeDecodeError as exc: -- cgit v1.2.3 From 9d60e5d491298f36a5de33c60c462d1a900844e8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 5 Aug 2016 10:30:46 -0400 Subject: Forget the environment variable, and just log a warning when a metadata can't be decoded. Ref #719. --- pkg_resources/__init__.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'pkg_resources/__init__.py') diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index b402ddd6..7dbd13a6 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -1,3 +1,5 @@ +# coding: utf-8 + """ Package resource API -------------------- @@ -1859,19 +1861,19 @@ class FileMetadata(EmptyProvider): def get_metadata(self, name): if name=='PKG-INFO': - env_key = 'PKG_RESOURCES_METADATA_ERRORS' - errors = os.environ.get(env_key, 'strict') - with io.open(self.path, encoding='utf-8', errors=errors) 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): + replacement_char = '�' + 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)) -- cgit v1.2.3 From 09cbf5e63f5c9e84438cf69711a7a6b767106506 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 5 Aug 2016 10:41:12 -0400 Subject: Restore Python 2 compatibility. Ref #719. --- pkg_resources/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pkg_resources/__init__.py') diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 7dbd13a6..94afaaf7 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -1868,7 +1868,8 @@ class FileMetadata(EmptyProvider): raise KeyError("No metadata except PKG-INFO is available") def _warn_on_replacement(self, metadata): - replacement_char = '�' + # 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()) -- cgit v1.2.3