bpo-29585: optimize site.py startup time by methane · Pull Request #136 · python/cpython
Expand Up
@@ -124,7 +124,7 @@ def removeduppaths():
# if they only differ in case); turn relative paths into absolute
# paths.
dir, dircase = makepath(dir)
if not dircase in known_paths:
if dircase not in known_paths:
L.append(dir)
known_paths.add(dircase)
sys.path[:] = L
Expand Down
Expand Up
@@ -234,6 +234,46 @@ def check_enableusersite():
return True
# NOTE: sysconfig and it's dependencies are relatively large but site module # needs very limited part of them. # To speedup startup time, we have copy of them. # # See https://bugs.python.org/issue29585
# Copy of sysconfig._getuserbase() def _getuserbase(): env_base = os.environ.get("PYTHONUSERBASE", None) if env_base: return env_base
def joinuser(*args): return os.path.expanduser(os.path.join(*args))
if os.name == "nt": base = os.environ.get("APPDATA") or "~" return joinuser(base, "Python")
if sys.platform == "darwin" and sys._framework: return joinuser("~", "Library", sys._framework, "%d.%d" % sys.version_info[:2])
return joinuser("~", ".local")
# Same to sysconfig.get_path('purelib', os.name+'_user') def _get_path(userbase): version = sys.version_info
if os.name == 'nt': return f'{userbase}/Python{version[0]}{version[1]}/site-packages'
if sys.platform == 'darwin' and sys._framework: return f'{userbase}/lib/python/site-packages'
return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
def getuserbase(): """Returns the `user base` directory path.
Expand All @@ -242,33 +282,23 @@ def getuserbase(): it. """ global USER_BASE if USER_BASE is not None: return USER_BASE from sysconfig import get_config_var USER_BASE = get_config_var('userbase') if USER_BASE is None: USER_BASE = _getuserbase() return USER_BASE
def getusersitepackages(): """Returns the user-specific site-packages directory path.
If the global variable ``USER_SITE`` is not initialized yet, this function will also set it. """ global USER_SITE user_base = getuserbase() # this will also set USER_BASE
if USER_SITE is not None: return USER_SITE
from sysconfig import get_path userbase = getuserbase() # this will also set USER_BASE
if sys.platform == 'darwin': from sysconfig import get_config_var if get_config_var('PYTHONFRAMEWORK'): USER_SITE = get_path('purelib', 'osx_framework_user') return USER_SITE if USER_SITE is None: USER_SITE = _get_path(userbase)
USER_SITE = get_path('purelib', '%s_user' % os.name) return USER_SITE
def addusersitepackages(known_paths): Expand Down Expand Up @@ -310,15 +340,11 @@ def getsitepackages(prefixes=None): else: sitepackages.append(prefix) sitepackages.append(os.path.join(prefix, "lib", "site-packages")) if sys.platform == "darwin": # for framework builds *only* we add the standard Apple # locations. from sysconfig import get_config_var framework = get_config_var("PYTHONFRAMEWORK") if framework: sitepackages.append( os.path.join("/Library", framework, '%d.%d' % sys.version_info[:2], "site-packages")) # for framework builds *only* we add the standard Apple locations. if sys.platform == "darwin" and sys._framework: sitepackages.append( os.path.join("/Library", framework, '%d.%d' % sys.version_info[:2], "site-packages")) return sitepackages
def addsitepackages(known_paths, prefixes=None): Expand Down
return True
# NOTE: sysconfig and it's dependencies are relatively large but site module # needs very limited part of them. # To speedup startup time, we have copy of them. # # See https://bugs.python.org/issue29585
# Copy of sysconfig._getuserbase() def _getuserbase(): env_base = os.environ.get("PYTHONUSERBASE", None) if env_base: return env_base
def joinuser(*args): return os.path.expanduser(os.path.join(*args))
if os.name == "nt": base = os.environ.get("APPDATA") or "~" return joinuser(base, "Python")
if sys.platform == "darwin" and sys._framework: return joinuser("~", "Library", sys._framework, "%d.%d" % sys.version_info[:2])
return joinuser("~", ".local")
# Same to sysconfig.get_path('purelib', os.name+'_user') def _get_path(userbase): version = sys.version_info
if os.name == 'nt': return f'{userbase}/Python{version[0]}{version[1]}/site-packages'
if sys.platform == 'darwin' and sys._framework: return f'{userbase}/lib/python/site-packages'
return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
def getuserbase(): """Returns the `user base` directory path.
Expand All @@ -242,33 +282,23 @@ def getuserbase(): it. """ global USER_BASE if USER_BASE is not None: return USER_BASE from sysconfig import get_config_var USER_BASE = get_config_var('userbase') if USER_BASE is None: USER_BASE = _getuserbase() return USER_BASE
def getusersitepackages(): """Returns the user-specific site-packages directory path.
If the global variable ``USER_SITE`` is not initialized yet, this function will also set it. """ global USER_SITE user_base = getuserbase() # this will also set USER_BASE
if USER_SITE is not None: return USER_SITE
from sysconfig import get_path userbase = getuserbase() # this will also set USER_BASE
if sys.platform == 'darwin': from sysconfig import get_config_var if get_config_var('PYTHONFRAMEWORK'): USER_SITE = get_path('purelib', 'osx_framework_user') return USER_SITE if USER_SITE is None: USER_SITE = _get_path(userbase)
USER_SITE = get_path('purelib', '%s_user' % os.name) return USER_SITE
def addusersitepackages(known_paths): Expand Down Expand Up @@ -310,15 +340,11 @@ def getsitepackages(prefixes=None): else: sitepackages.append(prefix) sitepackages.append(os.path.join(prefix, "lib", "site-packages")) if sys.platform == "darwin": # for framework builds *only* we add the standard Apple # locations. from sysconfig import get_config_var framework = get_config_var("PYTHONFRAMEWORK") if framework: sitepackages.append( os.path.join("/Library", framework, '%d.%d' % sys.version_info[:2], "site-packages")) # for framework builds *only* we add the standard Apple locations. if sys.platform == "darwin" and sys._framework: sitepackages.append( os.path.join("/Library", framework, '%d.%d' % sys.version_info[:2], "site-packages")) return sitepackages
def addsitepackages(known_paths, prefixes=None): Expand Down