File tree Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Original file line number Diff line number Diff line change 1+ Fixed encoding errors in ``expand.StaticModule `` when system default encoding doesn't match expectations for source files.
Original file line number Diff line number Diff line change 2121import importlib
2222import io
2323import os
24+ import pathlib
2425import sys
2526import warnings
2627from glob import iglob
@@ -62,9 +63,7 @@ class StaticModule:
6263 """Proxy to a module object that avoids executing arbitrary code."""
6364
6465 def __init__ (self , name : str , spec : ModuleSpec ):
65- with open (spec .origin ) as strm : # type: ignore
66- src = strm .read ()
67- module = ast .parse (src )
66+ module = ast .parse (pathlib .Path (spec .origin ).read_bytes ())
6867 vars (self ).update (locals ())
6968 del self .self
7069
Original file line number Diff line number Diff line change @@ -60,6 +60,20 @@ def test_read_files(tmp_path, monkeypatch):
6060
6161
6262class TestReadAttr :
63+ @pytest .mark .parametrize (
64+ "example" ,
65+ [
66+ # No cookie means UTF-8:
67+ b"__version__ = '\xc3 \xa9 '\n raise SystemExit(1)\n " ,
68+ # If a cookie is present, honor it:
69+ b"# -*- coding: utf-8 -*-\n __version__ = '\xc3 \xa9 '\n raise SystemExit(1)\n " ,
70+ b"# -*- coding: latin1 -*-\n __version__ = '\xe9 '\n raise 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" : "" ,
You can’t perform that action at this time.
0 commit comments