Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,8 @@ def __init__(self,

def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
items = _copy_items(items)
if items is self.default:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell this changes the behavior in some corner cases. For example, when parse_known_args() is called with a not empty namespace. The new code will only clone lists when default is set. The old code would have cloned any existing list. This matters if the caller has another reference to the same list.

Something like this

some_existing_list = []
ns = argparse.Namespace()
ns.setattr("foo", some_existing_list)

parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='append')
...

... = parser.parse_known_args(args=['--foo=xxx'], namespace=ns)

# At this point some_existing_list is ['xxx']

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. There are no tests for this (and some other corner cases).

items = _copy_items(items)
items.append(values)
setattr(namespace, self.dest, items)

Expand Down Expand Up @@ -1045,7 +1046,8 @@ def __init__(self,

def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
items = _copy_items(items)
if items is self.default:
items = _copy_items(items)
items.append(self.const)
setattr(namespace, self.dest, items)

Expand Down Expand Up @@ -1238,7 +1240,8 @@ def __call__(self, parser, namespace, values, option_string=None):
class _ExtendAction(_AppendAction):
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
items = _copy_items(items)
if items is self.default:
items = _copy_items(items)
items.extend(values)
setattr(namespace, self.dest, items)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Optimize :mod:`argparse` actions 'append', 'append_const' and 'extend' for
the case when the option is specified many times.
The target list is no longer copied every time the action is taken, only
the default value is copied the first time the action is taken.
Loading