[New-bugs-announce] [issue12353] argparse cannot handle empty arguments

Bryan Jacobs report at bugs.python.org
Fri Jun 17 17:40:06 CEST 2011

New submission from Bryan Jacobs <bjacobs at woti.com>:

Parsing arguments with argparse fails with an IndexError when one of the arguments is the empty string (''). This is caused by an access to the zero'th element of the argument value, without a preceding length check.

Fixed by the below patch:

Index: Lib/argparse.py
--- Lib/argparse.py
+++ Lib/argparse.py
@@ -1967,7 +1967,7 @@ class ArgumentParser(_AttributeHolder, _
         for arg_string in arg_strings:
             # for regular arguments, just add them back into the list
-            if arg_string[0] not in self.fromfile_prefix_chars:
+            if len(arg_string) == 0 or arg_string[0] not in self.fromfile_prefix_chars:
             # replace arguments referencing files with the file content

components: Library (Lib)
messages: 138515
nosy: bjacobs
priority: normal
severity: normal
status: open
title: argparse cannot handle empty arguments
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list