Demote the bootstrap code to the end of the `sys.path` so it is found last. :return: The list of un-imported bootstrap modules. :rtype: list of :class:`types.ModuleType`
(self, disable_vendor_importer=True)
| 50 | return self._sys_path_entry |
| 51 | |
| 52 | def demote(self, disable_vendor_importer=True): |
| 53 | # type: (bool) -> List[types.ModuleType] |
| 54 | """Demote the bootstrap code to the end of the `sys.path` so it is found last. |
| 55 | |
| 56 | :return: The list of un-imported bootstrap modules. |
| 57 | :rtype: list of :class:`types.ModuleType` |
| 58 | """ |
| 59 | # Grab a hold of `sys` early since we'll be un-importing our module in this process. |
| 60 | import sys |
| 61 | |
| 62 | # N.B.: We mutate the sys.path before un-importing modules so that any re-imports triggered |
| 63 | # by concurrent code will pull from the desired sys.path ordering. |
| 64 | # See here for how this situation might arise: https://github.com/pex-tool/pex/issues/1272 |
| 65 | |
| 66 | sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath] |
| 67 | sys.path.append(self._sys_path_entry) |
| 68 | |
| 69 | unimported_modules = [] # type: List[types.ModuleType] |
| 70 | for name, module in reversed(sorted(sys.modules.items())): |
| 71 | if "pex.cache.access" == name: |
| 72 | # N.B.: The pex.cache.access module maintains cache lock state which must be |
| 73 | # preserved in the case of a Pex PEX. |
| 74 | module.save_lock_state() |
| 75 | if "pex.third_party" == name and not disable_vendor_importer: |
| 76 | continue |
| 77 | if self.imported_from_bootstrap(module): |
| 78 | unimported_modules.append(sys.modules.pop(name)) |
| 79 | return unimported_modules |
| 80 | |
| 81 | def imported_from_bootstrap(self, module): |
| 82 | # type: (Any) -> bool |
no test coverage detected