1
1
"""Utilities for mypy.stubgen, mypy.stubgenc, and mypy.stubdoc modules."""
2
2
3
- import json
4
3
import os .path
5
4
import re
6
- import subprocess
7
5
import sys
8
6
from contextlib import contextmanager
9
7
from typing import Iterator , List , Optional , Tuple , Union
@@ -23,25 +21,6 @@ def __init__(self, module: str, message: str):
23
21
self .message = message
24
22
25
23
26
- def default_py2_interpreter () -> str :
27
- """Find a system Python 2 interpreter.
28
-
29
- Return full path or exit if failed.
30
- """
31
- # TODO: Make this do something reasonable in Windows.
32
- for candidate in ("/usr/bin/python2" , "/usr/bin/python" ):
33
- if not os .path .exists (candidate ):
34
- continue
35
- output = subprocess .check_output (
36
- [candidate , "--version" ], stderr = subprocess .STDOUT
37
- ).strip ()
38
- if b"Python 2" in output :
39
- return candidate
40
- raise SystemExit (
41
- "Can't find a Python 2 interpreter -- " "please use the --python-executable option"
42
- )
43
-
44
-
45
24
def walk_packages (
46
25
inspect : ModuleInspect , packages : List [str ], verbose : bool = False
47
26
) -> Iterator [str ]:
@@ -72,52 +51,6 @@ def walk_packages(
72
51
yield from prop .subpackages
73
52
74
53
75
- def find_module_path_and_all_py2 (
76
- module : str , interpreter : str
77
- ) -> Optional [Tuple [Optional [str ], Optional [List [str ]]]]:
78
- """Return tuple (module path, module __all__) for a Python 2 module.
79
-
80
- The path refers to the .py/.py[co] file. The second tuple item is
81
- None if the module doesn't define __all__.
82
-
83
- Raise CantImport if the module can't be imported, or exit if it's a C extension module.
84
- """
85
- cmd_template = f'{ interpreter } -c "%s"'
86
- code = (
87
- "import importlib, json; mod = importlib.import_module('%s'); "
88
- "print(mod.__file__); print(json.dumps(getattr(mod, '__all__', None)))"
89
- ) % module
90
- try :
91
- output_bytes = subprocess .check_output (cmd_template % code , shell = True )
92
- except subprocess .CalledProcessError as e :
93
- path = find_module_path_using_py2_sys_path (module , interpreter )
94
- if path is None :
95
- raise CantImport (module , str (e )) from e
96
- return path , None
97
- output = output_bytes .decode ("ascii" ).strip ().splitlines ()
98
- module_path = output [0 ]
99
- if not module_path .endswith ((".py" , ".pyc" , ".pyo" )):
100
- raise SystemExit ("%s looks like a C module; they are not supported for Python 2" % module )
101
- if module_path .endswith ((".pyc" , ".pyo" )):
102
- module_path = module_path [:- 1 ]
103
- module_all = json .loads (output [1 ])
104
- return module_path , module_all
105
-
106
-
107
- def find_module_path_using_py2_sys_path (module : str , interpreter : str ) -> Optional [str ]:
108
- """Try to find the path of a .py file for a module using Python 2 sys.path.
109
-
110
- Return None if no match was found.
111
- """
112
- out = subprocess .run (
113
- [interpreter , "-c" , "import sys; import json; print(json.dumps(sys.path))" ],
114
- check = True ,
115
- stdout = subprocess .PIPE ,
116
- ).stdout
117
- sys_path = json .loads (out .decode ("utf-8" ))
118
- return find_module_path_using_sys_path (module , sys_path )
119
-
120
-
121
54
def find_module_path_using_sys_path (module : str , sys_path : List [str ]) -> Optional [str ]:
122
55
relative_candidates = (
123
56
module .replace ("." , "/" ) + ".py" ,
@@ -181,18 +114,10 @@ def generate_guarded(
181
114
print (f"Created { target } " )
182
115
183
116
184
- PY2_MODULES = {"cStringIO" , "urlparse" , "collections.UserDict" }
185
-
186
-
187
117
def report_missing (mod : str , message : Optional [str ] = "" , traceback : str = "" ) -> None :
188
118
if message :
189
119
message = " with error: " + message
190
120
print (f"{ mod } : Failed to import, skipping{ message } " )
191
- m = re .search (r"ModuleNotFoundError: No module named '([^']*)'" , traceback )
192
- if m :
193
- missing_module = m .group (1 )
194
- if missing_module in PY2_MODULES :
195
- print ("note: Try --py2 for Python 2 mode" )
196
121
197
122
198
123
def fail_missing (mod : str , reason : ModuleNotFoundReason ) -> None :
0 commit comments