8484
8585
8686import collections as _collections
87- import copy as _copy
8887import os as _os
8988import re as _re
9089import sys as _sys
91- import textwrap as _textwrap
9290
9391from gettext import gettext as _ , ngettext
9492
95-
9693SUPPRESS = '==SUPPRESS=='
9794
9895OPTIONAL = '?'
@@ -137,10 +134,16 @@ def _get_args(self):
137134 return []
138135
139136
140- def _ensure_value (namespace , name , value ):
141- if getattr (namespace , name , None ) is None :
142- setattr (namespace , name , value )
143- return getattr (namespace , name )
137+ def _copy_items (items ):
138+ if items is None :
139+ return []
140+ # The copy module is used only in the 'append' and 'append_const'
141+ # actions, and it is needed only when the default value isn't a list.
142+ # Delay its import for speeding up the common case.
143+ if type (items ) is list :
144+ return items [:]
145+ import copy
146+ return copy .copy (items )
144147
145148
146149# ===============
@@ -619,12 +622,17 @@ def _iter_indented_subactions(self, action):
619622
620623 def _split_lines (self , text , width ):
621624 text = self ._whitespace_matcher .sub (' ' , text ).strip ()
622- return _textwrap .wrap (text , width )
625+ # The textwrap module is used only for formatting help.
626+ # Delay its import for speeding up the common usage of argparse.
627+ import textwrap
628+ return textwrap .wrap (text , width )
623629
624630 def _fill_text (self , text , width , indent ):
625631 text = self ._whitespace_matcher .sub (' ' , text ).strip ()
626- return _textwrap .fill (text , width , initial_indent = indent ,
627- subsequent_indent = indent )
632+ import textwrap
633+ return textwrap .fill (text , width ,
634+ initial_indent = indent ,
635+ subsequent_indent = indent )
628636
629637 def _get_help_string (self , action ):
630638 return action .help
@@ -952,7 +960,8 @@ def __init__(self,
952960 metavar = metavar )
953961
954962 def __call__ (self , parser , namespace , values , option_string = None ):
955- items = _copy .copy (_ensure_value (namespace , self .dest , []))
963+ items = getattr (namespace , self .dest , None )
964+ items = _copy_items (items )
956965 items .append (values )
957966 setattr (namespace , self .dest , items )
958967
@@ -978,7 +987,8 @@ def __init__(self,
978987 metavar = metavar )
979988
980989 def __call__ (self , parser , namespace , values , option_string = None ):
981- items = _copy .copy (_ensure_value (namespace , self .dest , []))
990+ items = getattr (namespace , self .dest , None )
991+ items = _copy_items (items )
982992 items .append (self .const )
983993 setattr (namespace , self .dest , items )
984994
@@ -1000,8 +1010,10 @@ def __init__(self,
10001010 help = help )
10011011
10021012 def __call__ (self , parser , namespace , values , option_string = None ):
1003- new_count = _ensure_value (namespace , self .dest , 0 ) + 1
1004- setattr (namespace , self .dest , new_count )
1013+ count = getattr (namespace , self .dest , None )
1014+ if count is None :
1015+ count = 0
1016+ setattr (namespace , self .dest , count + 1 )
10051017
10061018
10071019class _HelpAction (Action ):
0 commit comments