From d58d6f0e7e6231dd9cb0c675d0b1448971c2cacd Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 21 Jul 2020 21:46:06 +0100 Subject: [PATCH 1/2] bpo-41364: Reduce import overhead of uuid module --- Lib/uuid.py | 14 ++++++++------ .../2020-07-21-21-45-55.bpo-41364.5O-k7A.rst | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-07-21-21-45-55.bpo-41364.5O-k7A.rst diff --git a/Lib/uuid.py b/Lib/uuid.py index 9ddce813fc4692..5d1dcfa685e74f 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -54,10 +54,12 @@ __author__ = 'Ka-Ping Yee ' # The recognized platforms - known behaviors -_AIX = platform.system() == 'AIX' -_DARWIN = platform.system() == 'Darwin' -_LINUX = platform.system() == 'Linux' -_WINDOWS = platform.system() == 'Windows' +if sys.platform in ('win32', 'darwin'): + _AIX = _LINUX = False +else: + _platform_system = platform.system() + _AIX = _platform_system == 'AIX' + _LINUX = _platform_system == 'Linux' _MAC_DELIM = b':' _MAC_OMITS_LEADING_ZEROES = False @@ -618,9 +620,9 @@ def _random_getnode(): # @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, ...) if _LINUX: _OS_GETTERS = [_ip_getnode, _ifconfig_getnode] -elif _DARWIN: +elif sys.platform == 'darwin': _OS_GETTERS = [_ifconfig_getnode, _arp_getnode, _netstat_getnode] -elif _WINDOWS: +elif sys.platform == 'win32': # bpo-40201: _windll_getnode will always succeed, so these are not needed _OS_GETTERS = [] elif _AIX: diff --git a/Misc/NEWS.d/next/Library/2020-07-21-21-45-55.bpo-41364.5O-k7A.rst b/Misc/NEWS.d/next/Library/2020-07-21-21-45-55.bpo-41364.5O-k7A.rst new file mode 100644 index 00000000000000..f136e892ae5fe0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-21-21-45-55.bpo-41364.5O-k7A.rst @@ -0,0 +1 @@ +Reduce import overhead of :mod:`uuid`. From 59bc5d501365f7bdf576e01e9b779dae9df455b7 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 21 Jul 2020 23:09:47 +0100 Subject: [PATCH 2/2] Lazily import platform --- Lib/uuid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 5d1dcfa685e74f..5ae0a3e5fa449d 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -45,7 +45,6 @@ """ import os -import platform import sys from enum import Enum @@ -57,6 +56,7 @@ if sys.platform in ('win32', 'darwin'): _AIX = _LINUX = False else: + import platform _platform_system = platform.system() _AIX = _platform_system == 'AIX' _LINUX = _platform_system == 'Linux'