From 8ddefeab1fd04c74a5638ca1e2cc5dbccfad5dd8 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Sun, 10 Mar 2019 13:06:31 +0100 Subject: Import 'platform' and 'subprocess' as needed Saves around 10 ms of startup time when they aren't, as measured with '-X importtime'. --- kconfiglib.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'kconfiglib.py') diff --git a/kconfiglib.py b/kconfiglib.py index 5401bd8..306d166 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -525,9 +525,7 @@ import errno import glob import importlib import os -import platform import re -import subprocess import sys # File layout: @@ -2114,9 +2112,9 @@ class Kconfig(object): # # The preprocessor functionality changed how # environment variables are referenced, to $(FOO). - val = os.path.expandvars( - s[i + 1:end_i - 1].replace("$UNAME_RELEASE", - platform.uname()[2])) + val = os.path.expandvars(s[i + 1:end_i - 1] + .replace("$UNAME_RELEASE", + _UNAME_RELEASE)) i = end_i @@ -6224,6 +6222,9 @@ def _error_if_fn(kconf, _, cond, msg): return "" def _shell_fn(kconf, _, command): + # Only import as needed, to save some startup time + import subprocess + stdout, stderr = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ).communicate() @@ -6310,6 +6311,13 @@ STR_TO_TRI = { # Are we running on Python 2? _IS_PY2 = sys.version_info[0] < 3 +try: + _UNAME_RELEASE = os.uname()[2] +except AttributeError: + # Only import as needed, to save some startup time + import platform + _UNAME_RELEASE = platform.uname()[2] + # Tokens, with values 1, 2, ... . Avoiding 0 simplifies some checks by making # all tokens except empty strings truthy. ( -- cgit v1.2.3