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

This commit is contained in:
Serhiy Storchaka 2017-09-26 00:55:55 +03:00 committed by GitHub
parent f1502d097c
commit 81108375d9
8 changed files with 59 additions and 49 deletions

View file

@ -46,13 +46,10 @@ internationalized, to the local language and cultural habits.
# find this format documented anywhere.
import copy
import locale
import os
import re
import struct
import sys
from errno import ENOENT
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
@ -342,7 +339,9 @@ class GNUTranslations(NullTranslations):
def _parse(self, fp):
"""Override this method to support alternative .mo formats."""
unpack = struct.unpack
# 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
# bit words.
@ -520,7 +519,9 @@ def translation(domain, localedir=None, languages=None,
if not mofiles:
if fallback:
return NullTranslations()
raise OSError(ENOENT, 'No translation file found for domain', domain)
from errno import ENOENT
raise FileNotFoundError(ENOENT,
'No translation file found for domain', domain)
# Avoid opening, reading, and parsing the .mo file after it's been done
# once.
result = None
@ -533,6 +534,9 @@ 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:
t.set_output_charset(codeset)