|
4 | 4 | import sys |
5 | 5 | import runpy |
6 | 6 | import tempfile |
| 7 | +import subprocess |
7 | 8 |
|
8 | 9 |
|
9 | 10 | __all__ = ["version", "bootstrap"] |
10 | 11 |
|
11 | 12 |
|
12 | 13 | _SETUPTOOLS_VERSION = "49.2.1" |
13 | 14 |
|
14 | | -_PIP_VERSION = "20.2.1" |
| 15 | +_PIP_VERSION = "20.2.3" |
15 | 16 |
|
16 | 17 | _PROJECTS = [ |
17 | 18 | ("setuptools", _SETUPTOOLS_VERSION, "py3"), |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | def _run_pip(args, additional_paths=None): |
23 | | - # Add our bundled software to the sys.path so we can import it |
24 | | - if additional_paths is not None: |
25 | | - sys.path = additional_paths + sys.path |
26 | | - |
27 | | - # Invoke pip as if it's the main module, and catch the exit. |
28 | | - backup_argv = sys.argv[:] |
29 | | - sys.argv[1:] = args |
30 | | - try: |
31 | | - # run_module() alters sys.modules and sys.argv, but restores them at exit |
32 | | - runpy.run_module("pip", run_name="__main__", alter_sys=True) |
33 | | - except SystemExit as exc: |
34 | | - return exc.code |
35 | | - finally: |
36 | | - sys.argv[:] = backup_argv |
37 | | - |
38 | | - raise SystemError("pip did not exit, this should never happen") |
| 24 | + # Run the bootstraping in a subprocess to avoid leaking any state that happens |
| 25 | + # after pip has executed. Particulary, this avoids the case when pip holds onto |
| 26 | + # the files in *additional_paths*, preventing us to remove them at the end of the |
| 27 | + # invocation. |
| 28 | + code = f""" |
| 29 | +import runpy |
| 30 | +import sys |
| 31 | +sys.path = {additional_paths or []} + sys.path |
| 32 | +sys.argv[1:] = {args} |
| 33 | +runpy.run_module("pip", run_name="__main__", alter_sys=True) |
| 34 | +""" |
| 35 | + return subprocess.run([sys.executable, "-c", code], check=True).returncode |
39 | 36 |
|
40 | 37 |
|
41 | 38 | def version(): |
|
0 commit comments