FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-29585: optimize site.py startup time by methane · Pull Request #136 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension .ac  (1) .c  (1) .in  (1) .py  (2) No extension  (1) All 5 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
58 changes: 41 additions & 17 deletions Lib/site.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
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,27 @@ def check_enableusersite():

return True


def _getuserbase():
# Stripped version of sysconfig._getuserbase()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can you please elaborate this comment? Please explain that the function was duplicated to speedup Python startup and avoid the sysconfig import in the site module. Add a reference to the bpo 29585.

It seems like you modified this change: you moved "if env_base: return env_base" at the top. Please modify also sysconfig._getuserbase(). I would also prefer the sysconfig also uses sys._framework, instead of get_config_var("PYTHONFRAMEWORK").

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")


def getuserbase():
"""Returns the `user base` directory path.

Expand All @@ -242,33 +263,36 @@ 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 _get_path(userbase):
# stripped version of sysconfig.get_path('purelib', os.name + '_user')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Again, please elaborate the comment. (Maybe point to _getuserbase() above?)

version = sys.version_info[:2]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is [:2] really needed?


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 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
3 changes: 3 additions & 0 deletions Lib/sysconfig.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'scripts': '{base}/Scripts',
'data': '{base}',
},
# NOTE: When modifying "purelib" scheme, update site._get_path() too.
'nt_user': {
'stdlib': '{userbase}/Python{py_version_nodot}',
'platstdlib': '{userbase}/Python{py_version_nodot}',
Expand Down Expand Up @@ -177,6 +178,8 @@ def _get_default_scheme():
return os.name


# NOTE: site.py has stripped copy of this function.
# Update it too when modify this function.
def _getuserbase():
env_base = os.environ.get("PYTHONUSERBASE", None)

Expand Down
1 change: 1 addition & 0 deletions Python/sysmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,7 @@ _PySys_BeginInit(void)
SET_SYS_FROM_STRING("_git",
Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(),
_Py_gitversion()));
SET_SYS_FROM_STRING("_framework", PyUnicode_FromString(PYTHONFRAMEWORK));
SET_SYS_FROM_STRING("api_version",
PyLong_FromLong(PYTHON_API_VERSION));
SET_SYS_FROM_STRING("copyright",
Expand Down
20 changes: 19 additions & 1 deletion configure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ infodir
docdir
oldincludedir
includedir
runstatedir
localstatedir
sharedstatedir
sysconfdir
Expand Down Expand Up @@ -896,6 +897,7 @@ datadir='${datarootdir}'
sysconfdir='${prefix}/etc'
sharedstatedir='${prefix}/com'
localstatedir='${prefix}/var'
runstatedir='${localstatedir}/run'
includedir='${prefix}/include'
oldincludedir='/usr/include'
docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
Expand Down Expand Up @@ -1148,6 +1150,15 @@ do
| -silent | --silent | --silen | --sile | --sil)
silent=yes ;;

-runstatedir | --runstatedir | --runstatedi | --runstated \
| --runstate | --runstat | --runsta | --runst | --runs \
| --run | --ru | --r)
ac_prev=runstatedir ;;
-runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \
| --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \
| --run=* | --ru=* | --r=*)
runstatedir=$ac_optarg ;;

-sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
ac_prev=sbindir ;;
-sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
Expand Down Expand Up @@ -1285,7 +1296,7 @@ fi
for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \
datadir sysconfdir sharedstatedir localstatedir includedir \
oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
libdir localedir mandir
libdir localedir mandir runstatedir
do
eval ac_val=\$$ac_var
# Remove trailing slashes.
Expand Down Expand Up @@ -1438,6 +1449,7 @@ Fine tuning of the installation directories:
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
Expand Down Expand Up @@ -3231,6 +3243,12 @@ fi




cat >>confdefs.h <<_ACEOF
#define PYTHONFRAMEWORK "${PYTHONFRAMEWORK}"
_ACEOF


##AC_ARG_WITH(dyld,
## AS_HELP_STRING([--with-dyld],
## [Use (OpenStep|Rhapsody) dynamic linker]))
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ AC_SUBST(FRAMEWORKPYTHONW)
AC_SUBST(FRAMEWORKUNIXTOOLSPREFIX)
AC_SUBST(FRAMEWORKINSTALLAPPSPREFIX)

AC_DEFINE_UNQUOTED(PYTHONFRAMEWORK, "${PYTHONFRAMEWORK}", [framework name])

##AC_ARG_WITH(dyld,
## AS_HELP_STRING([--with-dyld],
## [Use (OpenStep|Rhapsody) dynamic linker]))
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,9 @@
/* Define as the preferred size in bits of long digits */
#undef PYLONG_BITS_IN_DIGIT

/* framework name */
#undef PYTHONFRAMEWORK

/* Define if you want to coerce the C locale to a UTF-8 based locale */
#undef PY_COERCE_C_LOCALE

Expand Down

Back | FazBrowse Home | New Git URL