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

argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception #90238

Closed
felixfontein mannequin opened this issue Dec 15, 2021 · 6 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@felixfontein
Copy link
Mannequin

felixfontein mannequin commented Dec 15, 2021

BPO 46080
Nosy @taleinat, @miss-islington, @iritkatriel, @felixfontein
PRs
  • bpo-46080: Fix crash when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help specified #30111
  • [3.10] bpo-46080: fix argparse help generation exception in edge case (GH-30111) #30730
  • [3.9] bpo-46080: fix argparse help generation exception in edge case (GH-30111) #30731
  • 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 = <Date 2022-01-21.08:20:51.001>
    created_at = <Date 2021-12-15.06:29:31.751>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception'
    updated_at = <Date 2022-02-02.16:53:48.653>
    user = 'https://github.com/felixfontein'

    bugs.python.org fields:

    activity = <Date 2022-02-02.16:53:48.653>
    actor = 'paul.j3'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-21.08:20:51.001>
    closer = 'taleinat'
    components = ['Library (Lib)']
    creation = <Date 2021-12-15.06:29:31.751>
    creator = 'felixfontein'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46080
    keywords = ['patch']
    message_count = 6.0
    messages = ['408586', '408588', '411052', '411055', '411057', '411096']
    nosy_count = 5.0
    nosy_names = ['taleinat', 'paul.j3', 'miss-islington', 'iritkatriel', 'felixfontein']
    pr_nums = ['30111', '30730', '30731']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46080'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @felixfontein
    Copy link
    Mannequin Author

    felixfontein mannequin commented Dec 15, 2021

    When argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help is specified, trying to display --help results in a crash.

    Reproducer:

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--test', action=argparse.BooleanOptionalAction, default=argparse.SUPPRESS, help='Foo')
    parser.parse_args()

    Result when running 'python t.py --help':

    Traceback (most recent call last):
      File "/path/to/t.py", line 4, in <module>
        parser.parse_args()
      File "/usr/lib/python3.10/argparse.py", line 1821, in parse_args
        args, argv = self.parse_known_args(args, namespace)
      File "/usr/lib/python3.10/argparse.py", line 1854, in parse_known_args
        namespace, args = self._parse_known_args(args, namespace)
      File "/usr/lib/python3.10/argparse.py", line 2063, in _parse_known_args
        start_index = consume_optional(start_index)
      File "/usr/lib/python3.10/argparse.py", line 2003, in consume_optional
        take_action(action, args, option_string)
      File "/usr/lib/python3.10/argparse.py", line 1931, in take_action
        action(self, namespace, argument_values, option_string)
      File "/usr/lib/python3.10/argparse.py", line 1095, in __call__
        parser.print_help()
      File "/usr/lib/python3.10/argparse.py", line 2551, in print_help
        self._print_message(self.format_help(), file)
      File "/usr/lib/python3.10/argparse.py", line 2535, in format_help
        return formatter.format_help()
      File "/usr/lib/python3.10/argparse.py", line 283, in format_help
        help = self._root_section.format_help()
      File "/usr/lib/python3.10/argparse.py", line 214, in format_help
        item_help = join([func(*args) for func, args in self.items])
      File "/usr/lib/python3.10/argparse.py", line 214, in <listcomp>
        item_help = join([func(*args) for func, args in self.items])
      File "/usr/lib/python3.10/argparse.py", line 214, in format_help
        item_help = join([func(*args) for func, args in self.items])
      File "/usr/lib/python3.10/argparse.py", line 214, in <listcomp>
        item_help = join([func(*args) for func, args in self.items])
      File "/usr/lib/python3.10/argparse.py", line 530, in _format_action
        help_text = self._expand_help(action)
      File "/usr/lib/python3.10/argparse.py", line 627, in _expand_help
        return self._get_help_string(action) % params
    KeyError: 'default'

    The problem is that in

    if help is not None and default is not None:
    , default isn't tested for SUPPRESS as well. I've prepared a patch to fix that and will push it to GitHub soon.

    (I've experienced the crash with Python 3.9 and 3.10; the test I've created also crashes for the current main branch, so I guess that means that Python 3.11 is also affected. I haven't tested older Python versions before 3.9.)

    @felixfontein felixfontein mannequin added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Dec 15, 2021
    @iritkatriel
    Copy link
    Member

    Changing type. Crash is typically segfault or hang in c code rather than an exception.

    @iritkatriel iritkatriel 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 Dec 15, 2021
    @iritkatriel iritkatriel changed the title argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified crashes argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception Dec 15, 2021
    @iritkatriel iritkatriel changed the title argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified crashes argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception Dec 15, 2021
    @taleinat
    Copy link
    Contributor

    New changeset 9e87c0e by Felix Fontein in branch 'main':
    bpo-46080: fix argparse help generation exception in edge case (GH-30111)
    9e87c0e

    @miss-islington
    Copy link
    Contributor

    New changeset e5edc8d by Miss Islington (bot) in branch '3.10':
    bpo-46080: fix argparse help generation exception in edge case (GH-30111)
    e5edc8d

    @miss-islington
    Copy link
    Contributor

    New changeset c6691a7 by Miss Islington (bot) in branch '3.9':
    bpo-46080: fix argparse help generation exception in edge case (GH-30111)
    c6691a7

    @taleinat
    Copy link
    Contributor

    Thanks for the report and the PR, Felix!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants