summaryrefslogtreecommitdiff
path: root/setuptools
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2022-09-29 19:46:43 -0400
committerGitHub <noreply@github.com>2022-09-29 19:46:43 -0400
commit646c71f9af7e9ba6677203acde4ccd0a478dfbf8 (patch)
treef447c5f81ebc744cbf49a5c01950c002dc921e12 /setuptools
parent401c184fcf9c82977a926bcf655d73dd6c12284c (diff)
parent80cce711a61f44357c04c5ce79d1f02ae6c321c6 (diff)
downloadpython-setuptools-git-646c71f9af7e9ba6677203acde4ccd0a478dfbf8.tar.gz
Merge pull request #3613 from JulienPalard/mdk-expand
expand: Give bytes to ast.parse to let it discover encoding cookie.
Diffstat (limited to 'setuptools')
-rw-r--r--setuptools/config/expand.py5
-rw-r--r--setuptools/tests/config/test_expand.py14
2 files changed, 16 insertions, 3 deletions
diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py
index 384504d8..38eb3db7 100644
--- a/setuptools/config/expand.py
+++ b/setuptools/config/expand.py
@@ -21,6 +21,7 @@ import ast
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) 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
diff --git a/setuptools/tests/config/test_expand.py b/setuptools/tests/config/test_expand.py
index 523779a8..39f3b7c7 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": "",