From a9d010952f495982adba2a3492cfddf7177b7b66 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 8 Sep 2023 15:10:50 -0700 Subject: [PATCH 1/7] Replace getopt with argparse in pdb --- Lib/pdb.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index c31f608e6d9da5..9de4f2766802e7 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2061,8 +2061,6 @@ def help(): pydoc.pager(__doc__) _usage = """\ -usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ... - Debug the Python program given by pyfile. Alternatively, an executable module or package to debug can be specified using the -m switch. @@ -2077,34 +2075,36 @@ def help(): def main(): - import getopt + import argparse - opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['help', 'command=']) + parser = argparse.ArgumentParser(prog="pdb", + description=_usage, + formatter_class=argparse.RawDescriptionHelpFormatter) - if not args: - print(_usage) - sys.exit(2) + parser.add_argument('-c', '--command', action='append', default=[]) + parser.add_argument('-m', action='store_true') + parser.add_argument('pyfile', nargs=1) + parser.add_argument('args', nargs="*") - if any(opt in ['-h', '--help'] for opt, optarg in opts): - print(_usage) - sys.exit() + if len(sys.argv) == 1: + parser.print_help() + sys.exit(2) - commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']] + opts = parser.parse_args() - module_indicated = any(opt in ['-m'] for opt, optarg in opts) - cls = _ModuleTarget if module_indicated else _ScriptTarget - target = cls(args[0]) + cls = _ModuleTarget if opts.m else _ScriptTarget + target = cls(opts.pyfile[0]) target.check() - sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list + sys.argv[:] = opts.pyfile + opts.args # Hide "pdb.py" and pdb options from argument list # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. There is a "restart" command # which allows explicit specification of command line arguments. pdb = Pdb() - pdb.rcLines.extend(commands) + pdb.rcLines.extend(opts.command) while True: try: pdb._run(target) From 6c5bb11ce4b910055ba9eefc55f3624645c793bb Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 8 Sep 2023 22:26:27 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst diff --git a/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst new file mode 100644 index 00000000000000..b0966ba6fb5315 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst @@ -0,0 +1 @@ +Replace getopt with argparse for parsing arguments in :mod:`pdb` From d51219ce7a84241fa654a9f518f7dfb79a24340c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 9 Sep 2023 22:50:39 -0700 Subject: [PATCH 3/7] Apply suggestions from code review Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/pdb.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 9de4f2766802e7..f859556c4bc8d3 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2079,11 +2079,13 @@ def main(): parser = argparse.ArgumentParser(prog="pdb", description=_usage, - formatter_class=argparse.RawDescriptionHelpFormatter) + formatter_class=argparse.RawDescriptionHelpFormatter, + allow_abbrev=False) - parser.add_argument('-c', '--command', action='append', default=[]) - parser.add_argument('-m', action='store_true') - parser.add_argument('pyfile', nargs=1) + parser.add_argument('-c', '--command', action='append', default=[], metavar='command') + grp = parser.add_mutually_exclusive_group(required=True) + grp.add_argument('-m', metavar='module') + grp.add_argument('pyfile', nargs='?') parser.add_argument('args', nargs="*") if len(sys.argv) == 1: @@ -2092,12 +2094,16 @@ def main(): opts = parser.parse_args() - cls = _ModuleTarget if opts.m else _ScriptTarget - target = cls(opts.pyfile[0]) + if opts.m: + file = opts.m + target = _ModuleTarget(file) + else: + file = opts.pyfile + target = _ScriptTarget(opts.pyfile[0]) target.check() - sys.argv[:] = opts.pyfile + opts.args # Hide "pdb.py" and pdb options from argument list + sys.argv[:] = [file] + opts.args # Hide "pdb.py" and pdb options from argument list # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was From e417507644e45bd84d3d680fecb041427785d243 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 9 Sep 2023 22:57:05 -0700 Subject: [PATCH 4/7] Rename & fix a bug --- Lib/pdb.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index f859556c4bc8d3..7cfd9bcda833c2 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2083,9 +2083,9 @@ def main(): allow_abbrev=False) parser.add_argument('-c', '--command', action='append', default=[], metavar='command') - grp = parser.add_mutually_exclusive_group(required=True) - grp.add_argument('-m', metavar='module') - grp.add_argument('pyfile', nargs='?') + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('-m', metavar='module') + group.add_argument('pyfile', nargs='?') parser.add_argument('args', nargs="*") if len(sys.argv) == 1: @@ -2099,7 +2099,7 @@ def main(): target = _ModuleTarget(file) else: file = opts.pyfile - target = _ScriptTarget(opts.pyfile[0]) + target = _ScriptTarget(file) target.check() From cdf3570a217e19f94e2852e6f34b0737567e52d0 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 15 Sep 2023 18:30:54 -0700 Subject: [PATCH 5/7] Add comments about why we need to check argv --- Lib/pdb.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/pdb.py b/Lib/pdb.py index 7cfd9bcda833c2..2ba2356c60022c 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2089,6 +2089,8 @@ def main(): parser.add_argument('args', nargs="*") if len(sys.argv) == 1: + # If no arguments were given (python -m pdb), print help message + # Without this check, argparse would only complain about missing required arguments parser.print_help() sys.exit(2) From 2defa6be7baa1ed7bcfe951602cb557abb73acd2 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 18 Sep 2023 13:28:45 -0700 Subject: [PATCH 6/7] Update Lib/pdb.py Co-authored-by: Victor Stinner --- Lib/pdb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 2ba2356c60022c..5033204983bf5e 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2089,8 +2089,8 @@ def main(): parser.add_argument('args', nargs="*") if len(sys.argv) == 1: - # If no arguments were given (python -m pdb), print help message - # Without this check, argparse would only complain about missing required arguments + # If no arguments were given (python -m pdb), print the whole help message. + # Without this check, argparse would only complain about missing required arguments. parser.print_help() sys.exit(2) From f14bc0988b39a43505e1488d3802cee35a0d9518 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:27:15 +0100 Subject: [PATCH 7/7] Update changelog entry Co-authored-by: Victor Stinner --- .../next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst index b0966ba6fb5315..b439c14ff535ff 100644 --- a/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst +++ b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst @@ -1 +1 @@ -Replace getopt with argparse for parsing arguments in :mod:`pdb` +:mod:`pdb`: Replace :mod:`getopt` with :mod:`argparse` for parsing command line arguments.