Skip to content

Commit 4a4aa9f

Browse files
authored
Clean up Python 2 helpers (#13260)
1 parent 9da4fb6 commit 4a4aa9f

File tree

2 files changed

+0
-103
lines changed

2 files changed

+0
-103
lines changed

mypy/stubutil.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Utilities for mypy.stubgen, mypy.stubgenc, and mypy.stubdoc modules."""
22

3-
import json
43
import os.path
54
import re
6-
import subprocess
75
import sys
86
from contextlib import contextmanager
97
from typing import Iterator, List, Optional, Tuple, Union
@@ -23,25 +21,6 @@ def __init__(self, module: str, message: str):
2321
self.message = message
2422

2523

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-
4524
def walk_packages(
4625
inspect: ModuleInspect, packages: List[str], verbose: bool = False
4726
) -> Iterator[str]:
@@ -72,52 +51,6 @@ def walk_packages(
7251
yield from prop.subpackages
7352

7453

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-
12154
def find_module_path_using_sys_path(module: str, sys_path: List[str]) -> Optional[str]:
12255
relative_candidates = (
12356
module.replace(".", "/") + ".py",
@@ -181,18 +114,10 @@ def generate_guarded(
181114
print(f"Created {target}")
182115

183116

184-
PY2_MODULES = {"cStringIO", "urlparse", "collections.UserDict"}
185-
186-
187117
def report_missing(mod: str, message: Optional[str] = "", traceback: str = "") -> None:
188118
if message:
189119
message = " with error: " + message
190120
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")
196121

197122

198123
def fail_missing(mod: str, reason: ModuleNotFoundReason) -> None:

mypy/util.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pathlib
77
import re
88
import shutil
9-
import subprocess
109
import sys
1110
import time
1211
from typing import (
@@ -53,13 +52,6 @@
5352
MINIMUM_WINDOWS_MAJOR_VT100: Final = 10
5453
MINIMUM_WINDOWS_BUILD_VT100: Final = 10586
5554

56-
default_python2_interpreter: Final = [
57-
"python2",
58-
"python",
59-
"/usr/bin/python",
60-
"C:\\Python27\\python.exe",
61-
]
62-
6355
SPECIAL_DUNDERS: Final = frozenset(
6456
("__init__", "__new__", "__call__", "__init_subclass__", "__class_getitem__")
6557
)
@@ -245,26 +237,6 @@ def get_mypy_comments(source: str) -> List[Tuple[int, str]]:
245237
return results
246238

247239

248-
_python2_interpreter: Optional[str] = None
249-
250-
251-
def try_find_python2_interpreter() -> Optional[str]:
252-
global _python2_interpreter
253-
if _python2_interpreter:
254-
return _python2_interpreter
255-
for interpreter in default_python2_interpreter:
256-
try:
257-
retcode = subprocess.Popen(
258-
[interpreter, "-c", "import sys, typing; assert sys.version_info[:2] == (2, 7)"]
259-
).wait()
260-
if not retcode:
261-
_python2_interpreter = interpreter
262-
return interpreter
263-
except OSError:
264-
pass
265-
return None
266-
267-
268240
PASS_TEMPLATE: Final = """<?xml version="1.0" encoding="utf-8"?>
269241
<testsuite errors="0" failures="0" name="mypy" skips="0" tests="1" time="{time:.3f}">
270242
<testcase classname="mypy" file="mypy" line="1" name="mypy-py{ver}-{platform}" time="{time:.3f}">

0 commit comments

Comments
 (0)