File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ class StaticModule:
62
62
"""Proxy to a module object that avoids executing arbitrary code."""
63
63
64
64
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
66
66
src = strm .read ()
67
67
module = ast .parse (src )
68
68
vars (self ).update (locals ())
Original file line number Diff line number Diff line change @@ -60,6 +60,22 @@ def test_read_files(tmp_path, monkeypatch):
60
60
61
61
62
62
class 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
+ b"\xff \xfe \x00 \x00 # -*- coding: utf-32 -*-\n __version__ = '\xe9 \x00 \x00 \x00 '\n raise SystemExit(1)\n " ,
72
+ ]
73
+ )
74
+ def test_read_attr_encoding_cookie (self , example , tmp_path ):
75
+ (tmp_path / "pkg" / "__init__.py" ).write_text ("" )
76
+ (tmp_path / "pkg" / "mod.py" ).write_bytes (example )
77
+ assert expand .read_attr ('pkg.sub.__version__' , root_dir = tmp_path ) == 'é'
78
+
63
79
def test_read_attr (self , tmp_path , monkeypatch ):
64
80
files = {
65
81
"pkg/__init__.py" : "" ,
You can’t perform that action at this time.
0 commit comments