Skip to content

Commit 1880f4a

Browse files
authored
Merge pull request #11342 from pfmoore/runner_version_check
Add a version check to __pip-runner__.py
2 parents 9473e83 + c69ea02 commit 1880f4a

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ def get_version(rel_path: str) -> str:
8181
],
8282
},
8383
zip_safe=False,
84+
# NOTE: python_requires is duplicated in __pip-runner__.py.
85+
# When changing this value, please change the other copy as well.
8486
python_requires=">=3.7",
8587
)

src/pip/__pip-runner__.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,38 @@
44
an import statement.
55
"""
66

7-
import runpy
7+
# /!\ This version compatibility check section must be Python 2 compatible. /!\
8+
89
import sys
9-
import types
10-
from importlib.machinery import ModuleSpec, PathFinder
11-
from os.path import dirname
12-
from typing import Optional, Sequence, Union
10+
11+
# Copied from setup.py
12+
PYTHON_REQUIRES = (3, 7)
13+
14+
15+
def version_str(version): # type: ignore
16+
return ".".join(str(v) for v in version)
17+
18+
19+
if sys.version_info[:2] < PYTHON_REQUIRES:
20+
raise SystemExit(
21+
"This version of pip does not support python {} (requires >={}).".format(
22+
version_str(sys.version_info[:2]), version_str(PYTHON_REQUIRES)
23+
)
24+
)
25+
26+
# From here on, we can use Python 3 features, but the syntax must remain
27+
# Python 2 compatible.
28+
29+
import runpy # noqa: E402
30+
from importlib.machinery import PathFinder # noqa: E402
31+
from os.path import dirname # noqa: E402
1332

1433
PIP_SOURCES_ROOT = dirname(dirname(__file__))
1534

1635

1736
class PipImportRedirectingFinder:
1837
@classmethod
19-
def find_spec(
20-
self,
21-
fullname: str,
22-
path: Optional[Sequence[Union[bytes, str]]] = None,
23-
target: Optional[types.ModuleType] = None,
24-
) -> Optional[ModuleSpec]:
38+
def find_spec(self, fullname, path=None, target=None): # type: ignore
2539
if fullname != "pip":
2640
return None
2741

0 commit comments

Comments
 (0)