diff --git a/Lib/argparse.py b/Lib/argparse.py index f688c38d0d1ae5..a05ee4396891cc 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2363,6 +2363,8 @@ def _read_args_from_files(self, arg_strings): errors=_sys.getfilesystemencodeerrors()) as args_file: arg_strings = [] for arg_line in args_file.read().splitlines(): + if not arg_line.strip(): + continue for arg in self.convert_arg_line_to_args(arg_line): arg_strings.append(arg) arg_strings = self._read_args_from_files(arg_strings) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 58853ba4eb3674..c120775d8a5ea3 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1715,6 +1715,7 @@ def setUp(self): b'@hello'), ('invalid', b'@no-such-path\n'), ('undecodable', self.undecodable + b'\n'), + ('blanklines', b'spam\n\nham\n') ] for path, text in file_texts: with open(path, 'wb') as file: @@ -1736,6 +1737,7 @@ def setUp(self): ('-a B @recursive Y Z', NS(a='A', x=hello, y=['Y', 'Z'])), ('X @recursive Z -a B', NS(a='B', x='X', y=[hello, 'Z'])), (["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])), + ('@blanklines', NS(a=None, x='spam', y=['ham'])), ] if os_helper.TESTFN_UNDECODABLE: undecodable = os_helper.TESTFN_UNDECODABLE.lstrip(b'@') diff --git a/Misc/NEWS.d/next/Library/2025-05-16-21-54-00.gh-issue-54732.asfjdf.rst b/Misc/NEWS.d/next/Library/2025-05-16-21-54-00.gh-issue-54732.asfjdf.rst new file mode 100644 index 00000000000000..d1bb8481a95331 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-16-21-54-00.gh-issue-54732.asfjdf.rst @@ -0,0 +1 @@ +Fix issue in :mod:`argparse` where an args file with blank lines could not be parsed.