Issue 36267: User input to argparse raises Index_Error: "-a=" on a 'store_true' action (original) (raw)

Created on 2019-03-12 01:36 by paul.j3, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
parse_v1.patch shihai1991,2019-09-02 18:28
Pull Requests
URL Status Linked Edit
PR 15656 open shihai1991,2019-09-03 05:13
Messages (7)
msg337709 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-03-12 01:36
Test case: parser = argparse.ArgumentParser() parser.add_argument("-a", action="store_true") args = parser.parse_args("-a=".split()) raises an Index_Error, which is not caught by the argparse error mechanism. This is an unusual case, the coincidence of several user errors - 'store_true' which shouldn't take an argument and a short optional with a bare =. So it's unlikely to occur. Still, argparse shouldn't ever respond to a command line value with an uncaught error. The traceback shows that it occurs during the handling of the explicit_arg in consume_optional. Traceback (most recent call last): File "argparseRaiseIndexError.py", line 5, in args = parser.parse_args("-a=".split()) File "/usr/lib/python3.6/argparse.py", line 1743, in parse_args args, argv = self.parse_known_args(args, namespace) File "/usr/lib/python3.6/argparse.py", line 1775, in parse_known_args namespace, args = self._parse_known_args(args, namespace) File "/usr/lib/python3.6/argparse.py", line 1981, in _parse_known_args start_index = consume_optional(start_index) File "/usr/lib/python3.6/argparse.py", line 1881, in consume_optional option_string = char + explicit_arg[0] IndexError: string index out of range The issue was raised in a Stackoverflow question, where I and the poster explore why it occurs, and possible fixes. https://stackoverflow.com/questions/54662609/python-argparse-indexerror-for-passing-a
msg351031 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2019-09-02 18:09
I do something such as: p = argparse.ArgumentParser() p.add_argument('-a', action='store_true') p.add_argument('-b', action='store_true') p.parse_args('-ab='.split()) thos code code jump in 1903, and the explicit_arg's value is: 'b=' 1901 action_tuples.append((action, [], option_string)) 1902 char = option_string[0] 1903 option_string = char + explicit_arg[0] IMHO, we should judge this explicit_arg's value before jump this judgment statement.
msg351035 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2019-09-02 18:28
Adding a judgment of explicit_args in judgment statement in the patch.
msg351047 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-09-03 01:07
FWIW, raising an uncaught exception isn't what we normally consider a "crash". We typically save that designation for segfaults. Ideally, a good patch/pr will also have tests. Also, given the rarity of the exception, I would be reserved about backporting, keeping it to 3.8 and 3.9.
msg351048 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2019-09-03 01:13
Thanks, Raymond. This patch not a formal patch, just show what i want to do. if everything agree this behavior, I would add a normal PR.
msg351050 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-09-03 03:52
Why did you switch back to "crash"?
msg351061 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2019-09-03 05:18
oh, sorry, pls ignore it. I misunderstand your opinion.
History
Date User Action Args
2022-04-11 14:59:12 admin set github: 80448
2020-06-06 22:10:34 remi.lapeyre set versions: + Python 3.9, Python 3.10, - Python 2.7
2019-09-03 05🔞27 shihai1991 set type: crash -> behaviormessages: +
2019-09-03 05:13:31 shihai1991 set stage: needs patch -> patch reviewpull_requests: + <pull%5Frequest15323>
2019-09-03 03:52:37 rhettinger set messages: +
2019-09-03 01:13:38 shihai1991 set type: behavior -> crashmessages: +
2019-09-03 01:07:59 rhettinger set nosy: + rhettingertype: crash -> behaviormessages: +
2019-09-02 18:28:32 shihai1991 set files: + parse_v1.patchkeywords: + patchmessages: +
2019-09-02 18:09:49 shihai1991 set nosy: + shihai1991messages: +
2019-03-12 02:49:00 xtreak set nosy: + xtreakversions: + Python 2.7, Python 3.7, Python 3.8
2019-03-12 01:36:10 paul.j3 create