diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-26 00:55:55 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-26 00:55:55 +0300 |
commit | 81108375d9b2ccd0add1572da745311d4dfac505 (patch) | |
tree | 54632e1d6ec58850f70941452634c4faf1ea7218 /Lib/argparse.py | |
parent | f1502d097c29b266a5748312ee2451a2d6ac0af6 (diff) | |
download | cpython-git-81108375d9b2ccd0add1572da745311d4dfac505.tar.gz |
bpo-30152: Reduce the number of imports for argparse. (#1269)
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 98bbed0e50..d8bbd352fd 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -84,15 +84,12 @@ __all__ = [ import collections as _collections -import copy as _copy import os as _os import re as _re import sys as _sys -import textwrap as _textwrap from gettext import gettext as _, ngettext - SUPPRESS = '==SUPPRESS==' OPTIONAL = '?' @@ -137,10 +134,16 @@ class _AttributeHolder(object): return [] -def _ensure_value(namespace, name, value): - if getattr(namespace, name, None) is None: - setattr(namespace, name, value) - return getattr(namespace, name) +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: + return items[:] + import copy + return copy.copy(items) # =============== @@ -619,12 +622,17 @@ class HelpFormatter(object): def _split_lines(self, text, width): text = self._whitespace_matcher.sub(' ', text).strip() - return _textwrap.wrap(text, width) + # The textwrap module is used only for formatting help. + # Delay its import for speeding up the common usage of argparse. + import textwrap + return textwrap.wrap(text, width) def _fill_text(self, text, width, indent): text = self._whitespace_matcher.sub(' ', text).strip() - return _textwrap.fill(text, width, initial_indent=indent, - subsequent_indent=indent) + import textwrap + return textwrap.fill(text, width, + initial_indent=indent, + subsequent_indent=indent) def _get_help_string(self, action): return action.help @@ -952,7 +960,8 @@ class _AppendAction(Action): metavar=metavar) def __call__(self, parser, namespace, values, option_string=None): - items = _copy.copy(_ensure_value(namespace, self.dest, [])) + items = getattr(namespace, self.dest, None) + items = _copy_items(items) items.append(values) setattr(namespace, self.dest, items) @@ -978,7 +987,8 @@ class _AppendConstAction(Action): metavar=metavar) def __call__(self, parser, namespace, values, option_string=None): - items = _copy.copy(_ensure_value(namespace, self.dest, [])) + items = getattr(namespace, self.dest, None) + items = _copy_items(items) items.append(self.const) setattr(namespace, self.dest, items) @@ -1000,8 +1010,10 @@ class _CountAction(Action): help=help) def __call__(self, parser, namespace, values, option_string=None): - new_count = _ensure_value(namespace, self.dest, 0) + 1 - setattr(namespace, self.dest, new_count) + count = getattr(namespace, self.dest, None) + if count is None: + count = 0 + setattr(namespace, self.dest, count + 1) class _HelpAction(Action): |