Skip to content

Commit 1573842

Browse files
committed
Issue python#16854: Fix regrtest.usage() regression introduced in 6e2e5adc0400.
This fixes a regression introduced in the commit for issue python#15302, which switched regrtest from getopt to argparse.
1 parent 2716d53 commit 1573842

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

Lib/test/regrtest.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,20 @@
202202

203203
TEMPDIR = os.path.abspath(tempfile.gettempdir())
204204

205+
class _ArgParser(argparse.ArgumentParser):
206+
207+
def error(self, message):
208+
super().error(message + "\nPass -h or --help for complete help.")
209+
205210
def _create_parser():
206211
# Set prog to prevent the uninformative "__main__.py" from displaying in
207212
# error messages when using "python -m test ...".
208-
parser = argparse.ArgumentParser(prog='regrtest.py',
209-
usage=USAGE,
210-
description=DESCRIPTION,
211-
epilog=EPILOG,
212-
add_help=False,
213-
formatter_class=
214-
argparse.RawDescriptionHelpFormatter)
213+
parser = _ArgParser(prog='regrtest.py',
214+
usage=USAGE,
215+
description=DESCRIPTION,
216+
epilog=EPILOG,
217+
add_help=False,
218+
formatter_class=argparse.RawDescriptionHelpFormatter)
215219

216220
# Arguments with this clause added to its help are described further in
217221
# the epilog's "Additional option details" section.
@@ -301,8 +305,18 @@ def _create_parser():
301305

302306
return parser
303307

308+
# TODO: remove this function as described in issue #16799, for example.
309+
# We use this function since regrtest.main() was originally written to use
310+
# getopt for parsing.
304311
def _convert_namespace_to_getopt(ns):
305-
"""Convert an argparse.Namespace object to a getopt-style (opts, args)."""
312+
"""Convert an argparse.Namespace object to a getopt-style opts list.
313+
314+
The return value of this function mimics the first element of
315+
getopt.getopt()'s (opts, args) return value. In addition, the (option,
316+
value) pairs in the opts list are sorted by option and use the long
317+
option string. The args part of (opts, args) can be mimicked by the
318+
args attribute of the Namespace object we are using in regrtest.
319+
"""
306320
opts = []
307321
args_dict = vars(ns)
308322
for key in sorted(args_dict.keys()):
@@ -319,21 +333,7 @@ def _convert_namespace_to_getopt(ns):
319333
# includes these with value '' in the opts list.
320334
val = ''
321335
opts.append(('--' + key, val))
322-
return opts, ns.args
323-
324-
# This function has a getopt-style return value because regrtest.main()
325-
# was originally written using getopt.
326-
# TODO: switch this to return an argparse.Namespace instance.
327-
def _parse_args(args=None):
328-
"""Parse arguments, and return a getopt-style (opts, args).
329-
330-
This method mimics the return value of getopt.getopt(). In addition,
331-
the (option, value) pairs in opts are sorted by option and use the long
332-
option string.
333-
"""
334-
parser = _create_parser()
335-
ns = parser.parse_args(args=args)
336-
return _convert_namespace_to_getopt(ns)
336+
return opts
337337

338338

339339
def main(tests=None, testdir=None, verbose=0, quiet=False,
@@ -381,7 +381,11 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
381381

382382
support.record_original_stdout(sys.stdout)
383383

384-
opts, args = _parse_args()
384+
parser = _create_parser()
385+
ns = parser.parse_args()
386+
opts = _convert_namespace_to_getopt(ns)
387+
args = ns.args
388+
usage = parser.error
385389

386390
# Defaults
387391
if random_seed is None:

Lib/test/test_regrtest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ def old_parse_args(args):
2323

2424
class ParseArgsTestCase(unittest.TestCase):
2525

26-
"""Test that regrtest._parse_args() matches the prior getopt behavior."""
26+
"""Test that regrtest's parsing code matches the prior getopt behavior."""
2727

2828
def _parse_args(self, args):
29-
return regrtest._parse_args(args=args)
29+
# This is the same logic as that used in regrtest.main()
30+
parser = regrtest._create_parser()
31+
ns = parser.parse_args(args=args)
32+
opts = regrtest._convert_namespace_to_getopt(ns)
33+
return opts, ns.args
3034

3135
def _check_args(self, args, expected=None):
3236
"""

0 commit comments

Comments
 (0)