|
6 | 6 | import os |
7 | 7 | import shutil |
8 | 8 | import site |
| 9 | +import sys |
9 | 10 | from optparse import SUPPRESS_HELP, Values |
10 | 11 | from pathlib import Path |
| 12 | +from typing import Any |
11 | 13 |
|
12 | 14 | from pip._vendor.packaging.utils import canonicalize_name |
13 | 15 | from pip._vendor.requests.exceptions import InvalidProxyURL |
|
43 | 45 | InstallRequirement, |
44 | 46 | ) |
45 | 47 | from pip._internal.utils.compat import WINDOWS |
| 48 | +from pip._internal.utils.deprecation import deprecated |
46 | 49 | from pip._internal.utils.filesystem import test_writable_dir |
47 | 50 | from pip._internal.utils.logging import getLogger |
48 | 51 | from pip._internal.utils.misc import ( |
|
63 | 66 | logger = getLogger(__name__) |
64 | 67 |
|
65 | 68 |
|
| 69 | +_IMPORT_AUDIT_HOOK_INSTALLED = False |
| 70 | +_MISSING_MODULES: set[str] = set() |
| 71 | + |
| 72 | +# Non-stdlib modules pip (or its vendored dependencies) may import lazily |
| 73 | +# after installation has started. Importing them eagerly keeps the audit |
| 74 | +# hook from misattributing them to a freshly installed distribution. |
| 75 | +_EAGER_IMPORTS: tuple[str, ...] = ( |
| 76 | + # Used by rich when emitting output to a legacy Windows console. |
| 77 | + "pip._vendor.rich._windows_renderer", |
| 78 | + # Optional stub that packaging imports while probing manylinux support. |
| 79 | + "_manylinux", |
| 80 | +) |
| 81 | + |
| 82 | + |
| 83 | +# Imports of standard library modules are always safe: they cannot be |
| 84 | +# shadowed by a distribution pip has just installed. |
| 85 | +_STDLIB_MODULE_NAMES: frozenset[str] = frozenset(sys.stdlib_module_names) | frozenset( |
| 86 | + sys.builtin_module_names |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | +def _prevent_import_hook(name: str, args: tuple[Any, ...]) -> None: |
| 91 | + if name != "import": |
| 92 | + return |
| 93 | + module = args[0] |
| 94 | + if module in _MISSING_MODULES: |
| 95 | + raise ImportError(f"No module named {module!r}") |
| 96 | + if module.partition(".")[0] in _STDLIB_MODULE_NAMES: |
| 97 | + return |
| 98 | + deprecated( |
| 99 | + reason=f"Unexpected import of {module!r} after pip install started.", |
| 100 | + replacement=None, |
| 101 | + gone_in="26.3", |
| 102 | + issue=13842, |
| 103 | + include_source=True, |
| 104 | + stacklevel=3, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def _eagerly_import_modules() -> None: |
| 109 | + """Import modules pip uses lazily so the audit hook ignores them later.""" |
| 110 | + for module in _EAGER_IMPORTS: |
| 111 | + try: |
| 112 | + __import__(module) |
| 113 | + except ImportError: |
| 114 | + # Record the module as missing so the hook can raise ImportError |
| 115 | + # instead of trying to import it again. |
| 116 | + _MISSING_MODULES.add(module) |
| 117 | + |
| 118 | + |
| 119 | +def _prevent_further_imports() -> None: |
| 120 | + """Install an audit hook that warns on unexpected imports after pip install starts. |
| 121 | +
|
| 122 | + Eagerly pre-imports the known lazy imports first so the hook only fires |
| 123 | + on genuinely unexpected modules. |
| 124 | + """ |
| 125 | + global _IMPORT_AUDIT_HOOK_INSTALLED |
| 126 | + if _IMPORT_AUDIT_HOOK_INSTALLED: |
| 127 | + return |
| 128 | + |
| 129 | + _IMPORT_AUDIT_HOOK_INSTALLED = True |
| 130 | + sys.addaudithook(_prevent_import_hook) |
| 131 | + |
| 132 | + |
66 | 133 | class InstallCommand(RequirementCommand): |
67 | 134 | """ |
68 | 135 | Install packages from: |
@@ -459,6 +526,13 @@ def run(self, options: Values, args: list[str]) -> int: |
459 | 526 | if options.target_dir or options.prefix_path: |
460 | 527 | warn_script_location = False |
461 | 528 |
|
| 529 | + # Warn on late imports so we don't silently pick up a module |
| 530 | + # from a distribution pip is about to install. |
| 531 | + try: |
| 532 | + _eagerly_import_modules() |
| 533 | + finally: |
| 534 | + _prevent_further_imports() |
| 535 | + |
462 | 536 | installed = install_given_reqs( |
463 | 537 | to_install, |
464 | 538 | root=options.root_path, |
|
0 commit comments