From c8e2d55e218309800a3f1dcc38c1290a0be960c0 Mon Sep 17 00:00:00 2001 From: blhsing Date: Wed, 26 Jun 2024 15:41:51 +0800 Subject: [PATCH 1/2] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019) (cherry picked from commit 0654336dd5138aec04e3017e15ccbb90a44e053d) Co-authored-by: blhsing --- Lib/argparse.py | 6 ++++-- Lib/test/test_argparse.py | 3 +++ .../Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index 55bf8cdd875a8d..64893b53efbfee 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1871,8 +1871,10 @@ def _get_positional_actions(self): def parse_args(self, args=None, namespace=None): args, argv = self.parse_known_args(args, namespace) if argv: - msg = _('unrecognized arguments: %s') - self.error(msg % ' '.join(argv)) + msg = _('unrecognized arguments: %s') % ' '.join(argv) + if self.exit_on_error: + self.error(msg) + raise ArgumentError(None, msg) return args def parse_known_args(self, args=None, namespace=None): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 02b499145f6c43..374852949e3e25 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -6096,6 +6096,9 @@ def test_exit_on_error_with_bad_args(self): with self.assertRaises(argparse.ArgumentError): self.parser.parse_args('--integers a'.split()) + def test_exit_on_error_with_unrecognized_args(self): + with self.assertRaises(argparse.ArgumentError): + self.parser.parse_args('--foo bar'.split()) def tearDownModule(): # Remove global references to avoid looking like we have refleaks. diff --git a/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst new file mode 100644 index 00000000000000..f311077d6bfc41 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst @@ -0,0 +1,2 @@ +Fixed an issue where :func:`argparse.ArgumentParser.parses_args` did not honor ``exit_on_error=False`` when given unrecognized arguments. +Patch by Ben Hsing From 959a93b37ccfc0025f5304b190bd28beafaab6f9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 26 Jun 2024 12:37:00 +0300 Subject: [PATCH 2/2] Update 2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst --- .../Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst index f311077d6bfc41..fdc3c5f9e459af 100644 --- a/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst +++ b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst @@ -1,2 +1,2 @@ -Fixed an issue where :func:`argparse.ArgumentParser.parses_args` did not honor ``exit_on_error=False`` when given unrecognized arguments. -Patch by Ben Hsing +Fixed an issue where :meth:`!argparse.ArgumentParser.parses_args` did not honor ``exit_on_error=False`` when given unrecognized arguments. +Patch by Ben Hsing.