From aa3f419acb38d4901b63a334c51e00c5dcc7ec7d Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 13 Oct 2023 00:03:20 +0200 Subject: [PATCH 1/2] gh-109653: Improve the import time of `email.utils` (#109824) --- Lib/email/utils.py | 12 +++++++----- .../2023-09-25-10-47-22.gh-issue-109653.TUHrId.rst | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-09-25-10-47-22.gh-issue-109653.TUHrId.rst diff --git a/Lib/email/utils.py b/Lib/email/utils.py index 81da5394ea1695..a49a8fa986ce0c 100644 --- a/Lib/email/utils.py +++ b/Lib/email/utils.py @@ -25,8 +25,6 @@ import os import re import time -import random -import socket import datetime import urllib.parse @@ -36,9 +34,6 @@ 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) diff --git a/Misc/NEWS.d/next/Library/2023-09-25-10-47-22.gh-issue-109653.TUHrId.rst b/Misc/NEWS.d/next/Library/2023-09-25-10-47-22.gh-issue-109653.TUHrId.rst new file mode 100644 index 00000000000000..b61f0bdd019ee1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-25-10-47-22.gh-issue-109653.TUHrId.rst @@ -0,0 +1,4 @@ +Reduce the import time of :mod:`email.utils` by around 43%. This results in +the import time of :mod:`email.message` falling by around 18%, which in turn +reduces the import time of :mod:`importlib.metadata` by around 6%. Patch by +Alex Waygood. From 2c472a87c7173d9fcdd52b1f35c05bc68d6c541e Mon Sep 17 00:00:00 2001 From: InSync <122007197+InSyncWithFoo@users.noreply.github.com> Date: Fri, 13 Oct 2023 05:04:36 +0700 Subject: [PATCH 2/2] Remove unnecessary escape in Doc/library/enum.rst (GH-110780) --- Doc/library/enum.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 487dace78c7288..d5f46ed58e8d24 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -322,7 +322,7 @@ Data Types >>> PowersOfThree.SECOND.value 9 - .. method:: Enum.__init_subclass__(cls, \**kwds) + .. method:: Enum.__init_subclass__(cls, **kwds) A *classmethod* that is used to further configure subsequent subclasses. By default, does nothing.