Skip to content

Commit ff0d05c

Browse files
committed
optimize site.py
Skip importing sysconfig when possible. Median +- std dev: [default] 15.8 ms +- 0.0 ms -> [patched] 14.7 ms +- 0.0 ms: 1.07x faster (-7%)
1 parent 2e12d42 commit ff0d05c

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

Lib/site.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def removeduppaths():
124124
# if they only differ in case); turn relative paths into absolute
125125
# paths.
126126
dir, dircase = makepath(dir)
127-
if not dircase in known_paths:
127+
if dircase not in known_paths:
128128
L.append(dir)
129129
known_paths.add(dircase)
130130
sys.path[:] = L
@@ -234,6 +234,27 @@ def check_enableusersite():
234234

235235
return True
236236

237+
238+
def _getuserbase():
239+
# Stripped version of sysconfig._getuserbase()
240+
env_base = os.environ.get("PYTHONUSERBASE", None)
241+
if env_base:
242+
return env_base
243+
244+
def joinuser(*args):
245+
return os.path.expanduser(os.path.join(*args))
246+
247+
if os.name == "nt":
248+
base = os.environ.get("APPDATA") or "~"
249+
return joinuser(base, "Python")
250+
251+
if sys.platform == "darwin" and sys._framework:
252+
return joinuser("~", "Library", sys._framework,
253+
"%d.%d" % sys.version_info[:2])
254+
255+
return joinuser("~", ".local")
256+
257+
237258
def getuserbase():
238259
"""Returns the `user base` directory path.
239260
@@ -242,33 +263,36 @@ def getuserbase():
242263
it.
243264
"""
244265
global USER_BASE
245-
if USER_BASE is not None:
246-
return USER_BASE
247-
from sysconfig import get_config_var
248-
USER_BASE = get_config_var('userbase')
266+
if USER_BASE is None:
267+
USER_BASE = _getuserbase()
249268
return USER_BASE
250269

270+
271+
def _get_path(userbase):
272+
# stripped version of sysconfig.get_path('purelib', os.name + '_user')
273+
version = sys.version_info[:2]
274+
275+
if os.name == 'nt':
276+
return f'{userbase}/Python{version[0]}{version[1]}/site-packages'
277+
278+
if sys.platform == 'darwin' and sys._framework:
279+
return f'{userbase}/lib/python/site-packages'
280+
281+
return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
282+
283+
251284
def getusersitepackages():
252285
"""Returns the user-specific site-packages directory path.
253286
254287
If the global variable ``USER_SITE`` is not initialized yet, this
255288
function will also set it.
256289
"""
257290
global USER_SITE
258-
user_base = getuserbase() # this will also set USER_BASE
259-
260-
if USER_SITE is not None:
261-
return USER_SITE
262-
263-
from sysconfig import get_path
291+
userbase = getuserbase() # this will also set USER_BASE
264292

265-
if sys.platform == 'darwin':
266-
from sysconfig import get_config_var
267-
if get_config_var('PYTHONFRAMEWORK'):
268-
USER_SITE = get_path('purelib', 'osx_framework_user')
269-
return USER_SITE
293+
if USER_SITE is None:
294+
USER_SITE = _get_path(userbase)
270295

271-
USER_SITE = get_path('purelib', '%s_user' % os.name)
272296
return USER_SITE
273297

274298
def addusersitepackages(known_paths):

0 commit comments

Comments
 (0)