From b35394aca15a51ff6fb44943c9f9530c68e685c4 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Tue, 27 Sep 2022 15:56:47 +0200 Subject: [PATCH 1/3] expand: Give bytes to ast.parse to let it discover encoding cookie. --- setuptools/config/expand.py | 2 +- setuptools/tests/config/test_expand.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index 384504d879..693b2dbc30 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -62,7 +62,7 @@ class StaticModule: """Proxy to a module object that avoids executing arbitrary code.""" def __init__(self, name: str, spec: ModuleSpec): - with open(spec.origin) as strm: # type: ignore + with open(spec.origin, mode='rb') as strm: # type: ignore src = strm.read() module = ast.parse(src) vars(self).update(locals()) diff --git a/setuptools/tests/config/test_expand.py b/setuptools/tests/config/test_expand.py index 523779a8ed..39f3b7c70f 100644 --- a/setuptools/tests/config/test_expand.py +++ b/setuptools/tests/config/test_expand.py @@ -60,6 +60,20 @@ def test_read_files(tmp_path, monkeypatch): class TestReadAttr: + @pytest.mark.parametrize( + "example", + [ + # No cookie means UTF-8: + b"__version__ = '\xc3\xa9'\nraise SystemExit(1)\n", + # If a cookie is present, honor it: + b"# -*- coding: utf-8 -*-\n__version__ = '\xc3\xa9'\nraise SystemExit(1)\n", + b"# -*- coding: latin1 -*-\n__version__ = '\xe9'\nraise SystemExit(1)\n", + ] + ) + def test_read_attr_encoding_cookie(self, example, tmp_path): + (tmp_path / "mod.py").write_bytes(example) + assert expand.read_attr('mod.__version__', root_dir=tmp_path) == 'é' + def test_read_attr(self, tmp_path, monkeypatch): files = { "pkg/__init__.py": "", From fb62784d242fdeb38d06c4d1d8277c25775e7fca Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 29 Sep 2022 19:39:22 -0400 Subject: [PATCH 2/3] Use pathlib to open the file --- setuptools/config/expand.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index 693b2dbc30..38eb3db7d8 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -21,6 +21,7 @@ import importlib import io import os +import pathlib import sys import warnings from glob import iglob @@ -62,9 +63,7 @@ class StaticModule: """Proxy to a module object that avoids executing arbitrary code.""" def __init__(self, name: str, spec: ModuleSpec): - with open(spec.origin, mode='rb') as strm: # type: ignore - src = strm.read() - module = ast.parse(src) + module = ast.parse(pathlib.Path(spec.origin).read_bytes()) vars(self).update(locals()) del self.self From 80cce711a61f44357c04c5ce79d1f02ae6c321c6 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 29 Sep 2022 19:43:31 -0400 Subject: [PATCH 3/3] Update changelog --- changelog.d/3613.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3613.misc.rst diff --git a/changelog.d/3613.misc.rst b/changelog.d/3613.misc.rst new file mode 100644 index 0000000000..7e64ed46aa --- /dev/null +++ b/changelog.d/3613.misc.rst @@ -0,0 +1 @@ +Fixed encoding errors in ``expand.StaticModule`` when system default encoding doesn't match expectations for source files.