Skip to content

Commit b35394a

Browse files
committed
expand: Give bytes to ast.parse to let it discover encoding cookie.
1 parent badd706 commit b35394a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

setuptools/config/expand.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StaticModule:
6262
"""Proxy to a module object that avoids executing arbitrary code."""
6363

6464
def __init__(self, name: str, spec: ModuleSpec):
65-
with open(spec.origin) as strm: # type: ignore
65+
with open(spec.origin, mode='rb') as strm: # type: ignore
6666
src = strm.read()
6767
module = ast.parse(src)
6868
vars(self).update(locals())

setuptools/tests/config/test_expand.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def test_read_files(tmp_path, monkeypatch):
6060

6161

6262
class TestReadAttr:
63+
@pytest.mark.parametrize(
64+
"example",
65+
[
66+
# No cookie means UTF-8:
67+
b"__version__ = '\xc3\xa9'\nraise SystemExit(1)\n",
68+
# If a cookie is present, honor it:
69+
b"# -*- coding: utf-8 -*-\n__version__ = '\xc3\xa9'\nraise SystemExit(1)\n",
70+
b"# -*- coding: latin1 -*-\n__version__ = '\xe9'\nraise SystemExit(1)\n",
71+
]
72+
)
73+
def test_read_attr_encoding_cookie(self, example, tmp_path):
74+
(tmp_path / "mod.py").write_bytes(example)
75+
assert expand.read_attr('mod.__version__', root_dir=tmp_path) == 'é'
76+
6377
def test_read_attr(self, tmp_path, monkeypatch):
6478
files = {
6579
"pkg/__init__.py": "",

0 commit comments

Comments
 (0)