10BC0 bpo-30152: Reduce the number of imports for argparse. by serhiy-storchaka · Pull Request #1269 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Import copy only when items is not a list.
  • Loading branch information
serhiy-storchaka committed May 4, 2017
commit 578af90a14daa1b6d940500a749941332817835a
27 changes: 14 additions & 13 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ def _get_args(self):
return []


def _copy_items(items):
if items is None:
return []
# The copy module is used only in the 'append' and 'append_const'
# actions, and it is needed only when the default value isn't a list.
# Delay its import for speeding up the common case.
if type(items) is list:
Copy link
Member

Choose a reason for hiding this comment

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

Why not isinstance(items, list)?

Copy link
Member Author

Choose a reason for hiding this comment

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

list subclass can override __copy__ or __reduce__.

return items[:]
Copy link
Member

Choose a reason for hiding this comment

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

Why not items.copy()?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is shorter.

Copy link
Contributor

Choose a reason for hiding this comment

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

And it's faster.

import copy
items = copy.copy(items)
Copy link

Choose a reason for hiding this comment

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

shouldn't that be 'return copy.copy(items)' ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, right!



# ===============
# Formatting Help
# ===============
Expand Down Expand Up @@ -947,14 +959,7 @@ def __init__(self,

def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
if items is None:
items = []
else:
# The copy module is used only in the 'append' and 'append_const'
# actions. Delay its import for speeding up the case when they
# are not used.
import copy
items = copy.copy(items)
items = _copy_items(items)
items.append(values)
setattr(namespace, self.dest, items)

Expand All @@ -981,11 +986,7 @@ def __init__(self,

def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
if items is None:
items = []
else:
import copy
items = copy.copy(items)
items = _copy_items(items)
items.append(self.const)
setattr(namespace, self.dest, items)

Expand Down
0