bpo-30152: Reduce the number of imports for argparse. (#1269)
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 5ad7ff6..4c3b80b 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -46,13 +46,10 @@
# 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 @@
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 @@
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 @@
# 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)