Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug#1003764: argparse: empty mutually_exclusive_group breaks --help

44 views
Skip to first unread message

Trent W. Buck

unread,
Jan 15, 2022, 6:40:06 AM1/15/22
to
Package: libpython3.9-minimal
Version: 3.9.2-1
Severity: normal
File: /usr/lib/python3.9/argparse.py

This works:

#!/usr/bin/python3
import argparse
parser = argparse.ArgumentParser(description='Demonstrate a dumbness in python 3.9')
parser.add_mutually_exclusive_group().add_argument('--fred')
parser.parse_args(['--help'])

This errors:

#!/usr/bin/python3
import argparse
parser = argparse.ArgumentParser(description='Demonstrate a dumbness in python 3.9')
parser.add_mutually_exclusive_group()
parser.parse_args(['--help'])

This is the error:

Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "/usr/lib/python3.9/argparse.py", line 1830, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.9/argparse.py", line 1863, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python3.9/argparse.py", line 2072, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.9/argparse.py", line 2012, in consume_optional
take_action(action, args, option_string)
File "/usr/lib/python3.9/argparse.py", line 1940, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python3.9/argparse.py", line 1104, in __call__
parser.print_help()
File "/usr/lib/python3.9/argparse.py", line 2560, in print_help
self._print_message(self.format_help(), file)
File "/usr/lib/python3.9/argparse.py", line 2544, in format_help
return formatter.format_help()
File "/usr/lib/python3.9/argparse.py", line 295, in format_help
help = self._root_section.format_help()
File "/usr/lib/python3.9/argparse.py", line 226, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.9/argparse.py", line 226, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.9/argparse.py", line 333, in _format_usage
action_usage = format(optionals + positionals, groups)
File "/usr/lib/python3.9/argparse.py", line 408, in _format_actions_usage
start = actions.index(group._group_actions[0])
IndexError: list index out of range


The problem only affects --help; an empty mutex group (without
required=True) still works in regular (non --help) usage.

I think the default --help provider should just skip empty mutex groups.

I think this (UNTESTED!) patch is probably sufficient:

diff -ud --label /tmp/argparse.py --label \#\<buffer\ argparse.py\> /tmp/argparse.py /tmp/buffer-content-7I3lfM
--- /tmp/argparse.py
+++ #<buffer argparse.py>
@@ -404,6 +404,8 @@
group_actions = set()
inserts = {}
for group in groups:
+ if len(group._group_actions) == 0:
+ continue
try:
start = actions.index(group._group_actions[0])
except ValueError:

Diff finished. Sat Jan 15 22:28:23 2022

I guess this is an upstream issue, but
I haven't time today to compare Debian's python to upstream's python.
Sorry about that.



-- System Information:
Debian Release: 11.2
APT prefers stable-updates
APT policy: (500, 'stable-updates'), (500, 'stable-security'), (500, 'proposed-updates'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 5.14.0-0.bpo.2-amd64 (SMP w/8 CPU threads)
Kernel taint flags: TAINT_PROPRIETARY_MODULE, TAINT_OOT_MODULE, TAINT_UNSIGNED_MODULE
Locale: LANG=en_AU.UTF-8, LC_CTYPE=en_AU.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages libpython3.9-minimal:amd64 depends on:
ii libc6 2.31-13+deb11u2
ii libssl1.1 1.1.1k-1+deb11u1

Versions of packages libpython3.9-minimal:amd64 recommends:
ii libpython3.9-stdlib 3.9.2-1

libpython3.9-minimal:amd64 suggests no packages.

-- no debconf information
0 new messages