8000 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

bpo-30152: Reduce the number of imports for argparse. #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
Add the comments explaining imports inside functions.
  • Loading branch information
serhiy-storchaka committed Apr 28, 2017
commit d8a622c6cf4d0f52d257052152a3a20485cd1d25
5 changes: 5 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,8 @@ def _iter_indented_subactions(self, action):

def _split_lines(self, text, width):
text = self._whitespace_matcher.sub(' ', text).strip()
# 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)

Expand Down Expand Up @@ -948,6 +950,9 @@ def __call__(self, parser, namespace, values, option_string=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.append(values)
Expand Down
4 changes: 4 additions & 0 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ def _get_versions(self, version):

def _parse(self, fp):
"""Override this method to support alternative .mo formats."""
# Delay struct import for speeding up gettext import when .mo files
# are not used.
from struct import unpack
filename = getattr(fp, 'name', '')
# Parse the .mo file header, which consists of 5 little endian 32
Expand Down Expand Up @@ -526,6 +528,8 @@ def translation(domain, localedir=None, languages=None,
# Copy the translation object to allow setting fallbacks and
# output charset. All other instance data is shared with the
# cached object.
# Delay copy import for speeding up gettext import when .mo files
# are not used.
import copy
t = copy.copy(t)
if codeset:
Expand Down
1 change: 1 addition & 0 deletions Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def coroutine(func):
# return generator-like objects (for instance generators
# compiled with Cython).

# Delay functools and _collections_abc import for speeding up types import.
import functools
import _collections_abc
@functools.wraps(func)
Expand Down
0