mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
GH-142267: Cache formatter to avoid repeated _set_color calls (#142268)
This commit is contained in:
parent
4b14529730
commit
4085ff7b32
2 changed files with 15 additions and 4 deletions
|
|
@ -1568,8 +1568,8 @@ def add_argument(self, *args, **kwargs):
|
||||||
f'instance of it must be passed')
|
f'instance of it must be passed')
|
||||||
|
|
||||||
# raise an error if the metavar does not match the type
|
# raise an error if the metavar does not match the type
|
||||||
if hasattr(self, "_get_formatter"):
|
if hasattr(self, "_get_validation_formatter"):
|
||||||
formatter = self._get_formatter()
|
formatter = self._get_validation_formatter()
|
||||||
try:
|
try:
|
||||||
formatter._format_args(action, None)
|
formatter._format_args(action, None)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
|
@ -1763,8 +1763,8 @@ def _handle_conflict_resolve(self, action, conflicting_actions):
|
||||||
action.container._remove_action(action)
|
action.container._remove_action(action)
|
||||||
|
|
||||||
def _check_help(self, action):
|
def _check_help(self, action):
|
||||||
if action.help and hasattr(self, "_get_formatter"):
|
if action.help and hasattr(self, "_get_validation_formatter"):
|
||||||
formatter = self._get_formatter()
|
formatter = self._get_validation_formatter()
|
||||||
try:
|
try:
|
||||||
formatter._expand_help(action)
|
formatter._expand_help(action)
|
||||||
except (ValueError, TypeError, KeyError) as exc:
|
except (ValueError, TypeError, KeyError) as exc:
|
||||||
|
|
@ -1919,6 +1919,9 @@ def __init__(self,
|
||||||
self.suggest_on_error = suggest_on_error
|
self.suggest_on_error = suggest_on_error
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
||||||
|
# Cached formatter for validation (avoids repeated _set_color calls)
|
||||||
|
self._cached_formatter = None
|
||||||
|
|
||||||
add_group = self.add_argument_group
|
add_group = self.add_argument_group
|
||||||
self._positionals = add_group(_('positional arguments'))
|
self._positionals = add_group(_('positional arguments'))
|
||||||
self._optionals = add_group(_('options'))
|
self._optionals = add_group(_('options'))
|
||||||
|
|
@ -2750,6 +2753,13 @@ def _get_formatter(self):
|
||||||
formatter._set_color(self.color)
|
formatter._set_color(self.color)
|
||||||
return formatter
|
return formatter
|
||||||
|
|
||||||
|
def _get_validation_formatter(self):
|
||||||
|
# Return cached formatter for read-only validation operations
|
||||||
|
# (_expand_help and _format_args). Avoids repeated slow _set_color calls.
|
||||||
|
if self._cached_formatter is None:
|
||||||
|
self._cached_formatter = self._get_formatter()
|
||||||
|
return self._cached_formatter
|
||||||
|
|
||||||
# =====================
|
# =====================
|
||||||
# Help-printing methods
|
# Help-printing methods
|
||||||
# =====================
|
# =====================
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Improve :mod:`argparse` performance by caching the formatter used for argument validation.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue