Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand: Give bytes to ast.parse to let it discover encoding cookie. #3613

Merged
merged 3 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions 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.
5 changes: 2 additions & 3 deletions setuptools/config/expand.py
Expand Up @@ -21,6 +21,7 @@
import importlib
import io
import os
import pathlib
import sys
import warnings
from glob import iglob
Expand Down Expand Up @@ -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) 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

Expand Down
14 changes: 14 additions & 0 deletions setuptools/tests/config/test_expand.py
Expand Up @@ -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": "",
Expand Down