Skip to content

Commit

Permalink
expand: Give bytes to ast.parse to let it discover encoding cookie.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienPalard committed Sep 27, 2022
1 parent badd706 commit 7e997ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setuptools/config/expand.py
Expand Up @@ -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())
Expand Down
16 changes: 16 additions & 0 deletions setuptools/tests/config/test_expand.py
Expand Up @@ -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": "",
Expand Down

0 comments on commit 7e997ac

Please sign in to comment.