summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStéphane Bidoul <stephane.bidoul@gmail.com>2022-07-10 13:18:33 +0200
committerGitHub <noreply@github.com>2022-07-10 13:18:33 +0200
commitf66b3e8d0168990e35f981e77b6abe6a6974c4ee (patch)
tree57d4522b53f4451f75ac9861dd7f0be50db19bef
parent99818e1e9ac8277fd067ba278f0c569047d25eab (diff)
parent8cbb89b6cc560c072219b36e59f1b35bc495b695 (diff)
downloadpip-f66b3e8d0168990e35f981e77b6abe6a6974c4ee.tar.gz
Merge pull request #11195 from pradyunsg/distutils-replace-fancygetopt-with-getopt
Replace `distutils.fancy_getopt` with `getopt`
-rw-r--r--src/pip/_internal/utils/distutils_args.py51
-rw-r--r--tests/unit/test_utils_distutils_args.py2
2 files changed, 27 insertions, 26 deletions
diff --git a/src/pip/_internal/utils/distutils_args.py b/src/pip/_internal/utils/distutils_args.py
index e4aa5b827..2fd186207 100644
--- a/src/pip/_internal/utils/distutils_args.py
+++ b/src/pip/_internal/utils/distutils_args.py
@@ -1,42 +1,43 @@
-from distutils.errors import DistutilsArgError
-from distutils.fancy_getopt import FancyGetopt
+from getopt import GetoptError, getopt
from typing import Dict, List
_options = [
- ("exec-prefix=", None, ""),
- ("home=", None, ""),
- ("install-base=", None, ""),
- ("install-data=", None, ""),
- ("install-headers=", None, ""),
- ("install-lib=", None, ""),
- ("install-platlib=", None, ""),
- ("install-purelib=", None, ""),
- ("install-scripts=", None, ""),
- ("prefix=", None, ""),
- ("root=", None, ""),
- ("user", None, ""),
+ "exec-prefix=",
+ "home=",
+ "install-base=",
+ "install-data=",
+ "install-headers=",
+ "install-lib=",
+ "install-platlib=",
+ "install-purelib=",
+ "install-scripts=",
+ "prefix=",
+ "root=",
+ "user",
]
-# typeshed doesn't permit Tuple[str, None, str], see python/typeshed#3469.
-_distutils_getopt = FancyGetopt(_options) # type: ignore
-
-
def parse_distutils_args(args: List[str]) -> Dict[str, str]:
- """Parse provided arguments, returning an object that has the
- matched arguments.
+ """Parse provided arguments, returning an object that has the matched arguments.
Any unknown arguments are ignored.
"""
result = {}
for arg in args:
try:
- _, match = _distutils_getopt.getopt(args=[arg])
- except DistutilsArgError:
+ parsed_opt, _ = getopt(args=[arg], shortopts="", longopts=_options)
+ except GetoptError:
# We don't care about any other options, which here may be
# considered unrecognized since our option list is not
# exhaustive.
- pass
- else:
- result.update(match.__dict__)
+ continue
+
+ if not parsed_opt:
+ continue
+
+ option = parsed_opt[0]
+ name_from_parsed = option[0][2:].replace("-", "_")
+ value_from_parsed = option[1] or "true"
+ result[name_from_parsed] = value_from_parsed
+
return result
diff --git a/tests/unit/test_utils_distutils_args.py b/tests/unit/test_utils_distutils_args.py
index e63c565a1..21f31e926 100644
--- a/tests/unit/test_utils_distutils_args.py
+++ b/tests/unit/test_utils_distutils_args.py
@@ -60,4 +60,4 @@ def test_all_value_options_work(name: str, value: str) -> None:
def test_user_option_works() -> None:
result = parse_distutils_args(["--user"])
- assert result["user"] == 1
+ assert result["user"]