summaryrefslogtreecommitdiffstats
path: root/Lib/email/utils.py
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-10-12 22:03:20 (GMT)
committerGitHub <noreply@github.com>2023-10-12 22:03:20 (GMT)
commitaa3f419acb38d4901b63a334c51e00c5dcc7ec7d (patch)
tree61de47041c6a924521d9341ba0458a6005d098b7 /Lib/email/utils.py
parente7331365b488382d906ce6733ab1349ded49c928 (diff)
downloadcpython-aa3f419acb38d4901b63a334c51e00c5dcc7ec7d.zip
cpython-aa3f419acb38d4901b63a334c51e00c5dcc7ec7d.tar.gz
cpython-aa3f419acb38d4901b63a334c51e00c5dcc7ec7d.tar.bz2
gh-109653: Improve the import time of `email.utils` (#109824)
Diffstat (limited to 'Lib/email/utils.py')
-rw-r--r--Lib/email/utils.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/Lib/email/utils.py b/Lib/email/utils.py
index 81da539..a49a8fa 100644
--- a/Lib/email/utils.py
+++ b/Lib/email/utils.py
@@ -25,8 +25,6 @@ __all__ = [
import os
import re
import time
-import random
-import socket
import datetime
import urllib.parse
@@ -36,9 +34,6 @@ from email._parseaddr import mktime_tz
from email._parseaddr import parsedate, parsedate_tz, _parsedate_tz
-# Intrapackage imports
-from email.charset import Charset
-
COMMASPACE = ', '
EMPTYSTRING = ''
UEMPTYSTRING = ''
@@ -94,6 +89,8 @@ def formataddr(pair, charset='utf-8'):
name.encode('ascii')
except UnicodeEncodeError:
if isinstance(charset, str):
+ # lazy import to improve module import time
+ from email.charset import Charset
charset = Charset(charset)
encoded_name = charset.header_encode(name)
return "%s <%s>" % (encoded_name, address)
@@ -181,6 +178,11 @@ def make_msgid(idstring=None, domain=None):
portion of the message id after the '@'. It defaults to the locally
defined hostname.
"""
+ # Lazy imports to speedup module import time
+ # (no other functions in email.utils need these modules)
+ import random
+ import socket
+
timeval = int(time.time()*100)
pid = os.getpid()
randint = random.getrandbits(64)