Callable main module patch: Support Python 2.7

Intentionally left out as a separate commit,
so it can be reverted once Python 2 support is dropped.
This commit is contained in:
Miro Hrončok 2019-12-11 17:30:22 +01:00
parent af087f8054
commit 184dcf3cac
1 changed files with 19 additions and 4 deletions

View File

@ -7,10 +7,10 @@ index 8c0e4c58..76e280e5 100755
import pip._internal.utils.inject_securetransport # noqa
+from pip._internal import main # noqa
diff --git a/src/pip/_internal/main.py b/src/pip/_internal/main.py
index 1e922402..e52e04d8 100644
index 1e922402..d3df58b3 100644
--- a/src/pip/_internal/main.py
+++ b/src/pip/_internal/main.py
@@ -45,3 +45,15 @@ def main(args=None):
@@ -45,3 +45,30 @@ def main(args=None):
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
return command.main(cmd_args)
@ -20,9 +20,24 @@ index 1e922402..e52e04d8 100644
+# This is needed to be able to use this pip in ensurepip of older Pythons
+# without patching all the Pythons.
+
+class _CallableModule(sys.modules[__name__].__class__):
+# In Python 3.5+, we can just inherit, define __call__ and override
+# sys.modules[__name__].__class__, however, that is not possible in 2.7.
+
+class _CallableModule(type(sys.modules[__name__])):
+ def __init__(self):
+ super(_CallableModule, self).__init__(__name__)
+ self._main = sys.modules[__name__]
+ sys.modules[__name__] = self
+ self.__doc__ = self._main.__doc__
+
+ def __call__(self, *args, **kwargs):
+ return main(*args, **kwargs)
+
+ def __dir__(self):
+ return dir(self._main)
+
+sys.modules[__name__].__class__ = _CallableModule
+ def __getattr__(self, attr):
+ return getattr(self._main, attr)
+
+
+_CallableModule()