Skip to content

Commit 8cbb89b

Browse files
committed
Replace distutils.fancy_getopt with getopt
This eliminates one location where distutils may be imported on Python 3.12+, by replacing the logic with mostly equivalent logic.
1 parent 6c2013c commit 8cbb89b

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed
+26-25
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
from distutils.errors import DistutilsArgError
2-
from distutils.fancy_getopt import FancyGetopt
1+
from getopt import GetoptError, getopt
32
from typing import Dict, List
43

54
_options = [
6-
("exec-prefix=", None, ""),
7-
("home=", None, ""),
8-
("install-base=", None, ""),
9-
("install-data=", None, ""),
10-
("install-headers=", None, ""),
11-
("install-lib=", None, ""),
12-
("install-platlib=", None, ""),
13-
("install-purelib=", None, ""),
14-
("install-scripts=", None, ""),
15-
("prefix=", None, ""),
16-
("root=", None, ""),
17-
("user", None, ""),
5+
"exec-prefix=",
6+
"home=",
7+
"install-base=",
8+
"install-data=",
9+
"install-headers=",
10+
"install-lib=",
11+
"install-platlib=",
12+
"install-purelib=",
13+
"install-scripts=",
14+
"prefix=",
15+
"root=",
16+
"user",
1817
]
1918

2019

21-
# typeshed doesn't permit Tuple[str, None, str], see python/typeshed#3469.
22-
_distutils_getopt = FancyGetopt(_options) # type: ignore
23-
24-
2520
def parse_distutils_args(args: List[str]) -> Dict[str, str]:
26-
"""Parse provided arguments, returning an object that has the
27-
matched arguments.
21+
"""Parse provided arguments, returning an object that has the matched arguments.
2822
2923
Any unknown arguments are ignored.
3024
"""
3125
result = {}
3226
for arg in args:
3327
try:
34-
_, match = _distutils_getopt.getopt(args=[arg])
35-
except DistutilsArgError:
28+
parsed_opt, _ = getopt(args=[arg], shortopts="", longopts=_options)
29+
except GetoptError:
3630
# We don't care about any other options, which here may be
3731
# considered unrecognized since our option list is not
3832
# exhaustive.
39-
pass
40-
else:
41-
result.update(match.__dict__)
33+
continue
34+
35+
if not parsed_opt:
36+
continue
37+
38+
option = parsed_opt[0]
39+
name_from_parsed = option[0][2:].replace("-", "_")
40+
value_from_parsed = option[1] or "true"
41+
result[name_from_parsed] = value_from_parsed
42+
4243
return result

tests/unit/test_utils_distutils_args.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def test_all_value_options_work(name: str, value: str) -> None:
6060

6161
def test_user_option_works() -> None:
6262
result = parse_distutils_args(["--user"])
63-
assert result["user"] == 1
63+
assert result["user"]

0 commit comments

Comments
 (0)