mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue #16854: Fix regrtest.usage() regression introduced in 6e2e5adc0400.
This fixes a regression introduced in the commit for issue #15302, which switched regrtest from getopt to argparse.
This commit is contained in:
parent
2716d531a1
commit
15738427f6
2 changed files with 34 additions and 26 deletions
|
@ -202,16 +202,20 @@ RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network',
|
||||||
|
|
||||||
TEMPDIR = os.path.abspath(tempfile.gettempdir())
|
TEMPDIR = os.path.abspath(tempfile.gettempdir())
|
||||||
|
|
||||||
|
class _ArgParser(argparse.ArgumentParser):
|
||||||
|
|
||||||
|
def error(self, message):
|
||||||
|
super().error(message + "\nPass -h or --help for complete help.")
|
||||||
|
|
||||||
def _create_parser():
|
def _create_parser():
|
||||||
# Set prog to prevent the uninformative "__main__.py" from displaying in
|
# Set prog to prevent the uninformative "__main__.py" from displaying in
|
||||||
# error messages when using "python -m test ...".
|
# error messages when using "python -m test ...".
|
||||||
parser = argparse.ArgumentParser(prog='regrtest.py',
|
parser = _ArgParser(prog='regrtest.py',
|
||||||
usage=USAGE,
|
usage=USAGE,
|
||||||
description=DESCRIPTION,
|
description=DESCRIPTION,
|
||||||
epilog=EPILOG,
|
epilog=EPILOG,
|
||||||
add_help=False,
|
add_help=False,
|
||||||
formatter_class=
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
argparse.RawDescriptionHelpFormatter)
|
|
||||||
|
|
||||||
# Arguments with this clause added to its help are described further in
|
# Arguments with this clause added to its help are described further in
|
||||||
# the epilog's "Additional option details" section.
|
# the epilog's "Additional option details" section.
|
||||||
|
@ -301,8 +305,18 @@ def _create_parser():
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
# TODO: remove this function as described in issue #16799, for example.
|
||||||
|
# We use this function since regrtest.main() was originally written to use
|
||||||
|
# getopt for parsing.
|
||||||
def _convert_namespace_to_getopt(ns):
|
def _convert_namespace_to_getopt(ns):
|
||||||
"""Convert an argparse.Namespace object to a getopt-style (opts, args)."""
|
"""Convert an argparse.Namespace object to a getopt-style opts list.
|
||||||
|
|
||||||
|
The return value of this function mimics the first element of
|
||||||
|
getopt.getopt()'s (opts, args) return value. In addition, the (option,
|
||||||
|
value) pairs in the opts list are sorted by option and use the long
|
||||||
|
option string. The args part of (opts, args) can be mimicked by the
|
||||||
|
args attribute of the Namespace object we are using in regrtest.
|
||||||
|
"""
|
||||||
opts = []
|
opts = []
|
||||||
args_dict = vars(ns)
|
args_dict = vars(ns)
|
||||||
for key in sorted(args_dict.keys()):
|
for key in sorted(args_dict.keys()):
|
||||||
|
@ -319,21 +333,7 @@ def _convert_namespace_to_getopt(ns):
|
||||||
# includes these with value '' in the opts list.
|
# includes these with value '' in the opts list.
|
||||||
val = ''
|
val = ''
|
||||||
opts.append(('--' + key, val))
|
opts.append(('--' + key, val))
|
||||||
return opts, ns.args
|
return opts
|
||||||
|
|
||||||
# This function has a getopt-style return value because regrtest.main()
|
|
||||||
# was originally written using getopt.
|
|
||||||
# TODO: switch this to return an argparse.Namespace instance.
|
|
||||||
def _parse_args(args=None):
|
|
||||||
"""Parse arguments, and return a getopt-style (opts, args).
|
|
||||||
|
|
||||||
This method mimics the return value of getopt.getopt(). In addition,
|
|
||||||
the (option, value) pairs in opts are sorted by option and use the long
|
|
||||||
option string.
|
|
||||||
"""
|
|
||||||
parser = _create_parser()
|
|
||||||
ns = parser.parse_args(args=args)
|
|
||||||
return _convert_namespace_to_getopt(ns)
|
|
||||||
|
|
||||||
|
|
||||||
def main(tests=None, testdir=None, verbose=0, quiet=False,
|
def main(tests=None, testdir=None, verbose=0, quiet=False,
|
||||||
|
@ -381,7 +381,11 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
|
||||||
|
|
||||||
support.record_original_stdout(sys.stdout)
|
support.record_original_stdout(sys.stdout)
|
||||||
|
|
||||||
opts, args = _parse_args()
|
parser = _create_parser()
|
||||||
|
ns = parser.parse_args()
|
||||||
|
opts = _convert_namespace_to_getopt(ns)
|
||||||
|
args = ns.args
|
||||||
|
usage = parser.error
|
||||||
|
|
||||||
# Defaults
|
# Defaults
|
||||||
if random_seed is None:
|
if random_seed is None:
|
||||||
|
|
|
@ -23,10 +23,14 @@ def old_parse_args(args):
|
||||||
|
|
||||||
class ParseArgsTestCase(unittest.TestCase):
|
class ParseArgsTestCase(unittest.TestCase):
|
||||||
|
|
||||||
"""Test that regrtest._parse_args() matches the prior getopt behavior."""
|
"""Test that regrtest's parsing code matches the prior getopt behavior."""
|
||||||
|
|
||||||
def _parse_args(self, args):
|
def _parse_args(self, args):
|
||||||
return regrtest._parse_args(args=args)
|
# This is the same logic as that used in regrtest.main()
|
||||||
|
parser = regrtest._create_parser()
|
||||||
|
ns = parser.parse_args(args=args)
|
||||||
|
opts = regrtest._convert_namespace_to_getopt(ns)
|
||||||
|
return opts, ns.args
|
||||||
|
|
||||||
def _check_args(self, args, expected=None):
|
def _check_args(self, args, expected=None):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue