From 7e997ac1f16fd1246f3e89c01b4ea13d7a7304e6 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Tue, 27 Sep 2022 15:56:47 +0200 Subject: [PATCH] expand: Give bytes to ast.parse to let it discover encoding cookie. --- setuptools/config/expand.py | 2 +- setuptools/tests/config/test_expand.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index 384504d879f..693b2dbc300 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 523779a8ed2..67d08dc1463 100644 --- a/setuptools/tests/config/test_expand.py +++ b/setuptools/tests/config/test_expand.py @@ -60,6 +60,22 @@ 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", + b"\xff\xfe\x00\x00# -*- coding: utf-32 -*-\n__version__ = '\xe9\x00\x00\x00'\nraise SystemExit(1)\n", + ] + ) + def test_read_attr_encoding_cookie(self, example, tmp_path): + (tmp_path / "pkg" / "__init__.py").write_text("") + (tmp_path / "pkg" / "mod.py").write_bytes(example) + assert expand.read_attr('pkg.sub.__version__', root_dir=tmp_path) == 'é' + def test_read_attr(self, tmp_path, monkeypatch): files = { "pkg/__init__.py": "",