Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User input to argparse raises Index_Error: "-a=" on a 'store_true' action #80448

Closed
paulj3 mannequin opened this issue Mar 12, 2019 · 8 comments
Closed

User input to argparse raises Index_Error: "-a=" on a 'store_true' action #80448

paulj3 mannequin opened this issue Mar 12, 2019 · 8 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@paulj3
Copy link
Mannequin

paulj3 mannequin commented Mar 12, 2019

BPO 36267
Nosy @rhettinger, @tirkarthi, @shihai1991
PRs
  • gh-80448: argparse: Fix IndexError on store_true action #15656
  • Files
  • parse_v1.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-03-12.01:36:10.610>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library']
    title = 'User input to argparse raises Index_Error:  "-a=" on a \'store_true\' action'
    updated_at = <Date 2020-06-06.22:10:34.797>
    user = 'https://bugs.python.org/paulj3'

    bugs.python.org fields:

    activity = <Date 2020-06-06.22:10:34.797>
    actor = 'remi.lapeyre'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-03-12.01:36:10.610>
    creator = 'paul.j3'
    dependencies = []
    files = ['48582']
    hgrepos = []
    issue_num = 36267
    keywords = ['patch']
    message_count = 7.0
    messages = ['337709', '351031', '351035', '351047', '351048', '351050', '351061']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'paul.j3', 'xtreak', 'shihai1991']
    pr_nums = ['15656']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36267'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @paulj3
    Copy link
    Mannequin Author

    paulj3 mannequin commented Mar 12, 2019

    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 <module>
        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

    @paulj3 paulj3 mannequin added type-crash A hard crash of the interpreter, possibly with a core dump stdlib Python modules in the Lib dir labels Mar 12, 2019
    @tirkarthi tirkarthi added 3.7 (EOL) end of life 3.8 only security fixes labels Mar 12, 2019
    @shihai1991
    Copy link
    Member

    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.

    @shihai1991
    Copy link
    Member

    Adding a judgment of explicit_args in judgment statement in the patch.

    @rhettinger
    Copy link
    Contributor

    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.

    @rhettinger rhettinger added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Sep 3, 2019
    @shihai1991
    Copy link
    Member

    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.

    @shihai1991 shihai1991 added type-crash A hard crash of the interpreter, possibly with a core dump and removed type-bug An unexpected behavior, bug, or error labels Sep 3, 2019
    @rhettinger
    Copy link
    Contributor

    Why did you switch back to "crash"?

    @shihai1991
    Copy link
    Member

    oh, sorry, pls ignore it. I misunderstand your opinion.

    @shihai1991 shihai1991 added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Sep 3, 2019
    @remilapeyre remilapeyre mannequin added 3.9 only security fixes 3.10 only security fixes labels Jun 6, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    JelleZijlstra added a commit that referenced this issue Nov 12, 2022
    Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 12, 2022
    …GH-15656)
    
    (cherry picked from commit e02cc6d)
    
    Co-authored-by: Hai Shi <shihai1992@gmail.com>
    Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 12, 2022
    …GH-15656)
    
    (cherry picked from commit e02cc6d)
    
    Co-authored-by: Hai Shi <shihai1992@gmail.com>
    Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    miss-islington added a commit that referenced this issue Nov 12, 2022
    (cherry picked from commit e02cc6d)
    
    Co-authored-by: Hai Shi <shihai1992@gmail.com>
    Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    miss-islington added a commit that referenced this issue Nov 12, 2022
    (cherry picked from commit e02cc6d)
    
    Co-authored-by: Hai Shi <shihai1992@gmail.com>
    Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    @hauntsaninja
    Copy link
    Contributor

    Looks like this has been merged and backported, thanks!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: Doc issues
    Development

    No branches or pull requests

    4 participants