Skip to content

Commit 1365b10

Browse files
authored
Revert "bpo-29585: optimize site.py startup time (GH-136)"
This reverts commit a8f8d5b.
1 parent a8f8d5b commit 1365b10

File tree

8 files changed

+43
-96
lines changed

8 files changed

+43
-96
lines changed

Lib/site.py

Lines changed: 26 additions & 52 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 dircase not in known_paths:
127+
if not dircase in known_paths:
128128
L.append(dir)
129129
known_paths.add(dircase)
130130
sys.path[:] = L
@@ -234,46 +234,6 @@ def check_enableusersite():
234234

235235
return True
236236

237-
238-
# NOTE: sysconfig and it's dependencies are relatively large but site module
239-
# needs very limited part of them.
240-
# To speedup startup time, we have copy of them.
241-
#
242-
# See https://bugs.python.org/issue29585
243-
244-
# Copy of sysconfig._getuserbase()
245-
def _getuserbase():
246-
env_base = os.environ.get("PYTHONUSERBASE", None)
247-
if env_base:
248-
return env_base
249-
250-
def joinuser(*args):
251-
return os.path.expanduser(os.path.join(*args))
252-
253-
if os.name == "nt":
254-
base = os.environ.get("APPDATA") or "~"
255-
return joinuser(base, "Python")
256-
257-
if sys.platform == "darwin" and sys._framework:
258-
return joinuser("~", "Library", sys._framework,
259-
"%d.%d" % sys.version_info[:2])
260-
261-
return joinuser("~", ".local")
262-
263-
264-
# Same to sysconfig.get_path('purelib', os.name+'_user')
265-
def _get_path(userbase):
266-
version = sys.version_info
267-
268-
if os.name == 'nt':
269-
return f'{userbase}/Python{version[0]}{version[1]}/site-packages'
270-
271-
if sys.platform == 'darwin' and sys._framework:
272-
return f'{userbase}/lib/python/site-packages'
273-
274-
return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
275-
276-
277237
def getuserbase():
278238
"""Returns the `user base` directory path.
279239
@@ -282,23 +242,33 @@ def getuserbase():
282242
it.
283243
"""
284244
global USER_BASE
285-
if USER_BASE is None:
286-
USER_BASE = _getuserbase()
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')
287249
return USER_BASE
288250

289-
290251
def getusersitepackages():
291252
"""Returns the user-specific site-packages directory path.
292253
293254
If the global variable ``USER_SITE`` is not initialized yet, this
294255
function will also set it.
295256
"""
296257
global USER_SITE
297-
userbase = getuserbase() # this will also set USER_BASE
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
298264

299-
if USER_SITE is None:
300-
USER_SITE = _get_path(userbase)
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
301270

271+
USER_SITE = get_path('purelib', '%s_user' % os.name)
302272
return USER_SITE
303273

304274
def addusersitepackages(known_paths):
@@ -340,11 +310,15 @@ def getsitepackages(prefixes=None):
340310
else:
341311
sitepackages.append(prefix)
342312
sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
343-
# for framework builds *only* we add the standard Apple locations.
344-
if sys.platform == "darwin" and sys._framework:
345-
sitepackages.append(
346-
os.path.join("/Library", framework,
347-
'%d.%d' % sys.version_info[:2], "site-packages"))
313+
if sys.platform == "darwin":
314+
# for framework builds *only* we add the standard Apple
315+
# locations.
316+
from sysconfig import get_config_var
317+
framework = get_config_var("PYTHONFRAMEWORK")
318+
if framework:
319+
sitepackages.append(
320+
os.path.join("/Library", framework,
321+
'%d.%d' % sys.version_info[:2], "site-packages"))
348322
return sitepackages
349323

350324
def addsitepackages(known_paths, prefixes=None):

Lib/sysconfig.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
'scripts': '{base}/Scripts',
5252
'data': '{base}',
5353
},
54-
# NOTE: When modifying "purelib" scheme, update site._get_path() too.
5554
'nt_user': {
5655
'stdlib': '{userbase}/Python{py_version_nodot}',
5756
'platstdlib': '{userbase}/Python{py_version_nodot}',
@@ -178,25 +177,32 @@ def _get_default_scheme():
178177
return os.name
179178

180179

181-
# NOTE: site.py has copy of this function.
182-
# Sync it when modify this function.
183180
def _getuserbase():
184181
env_base = os.environ.get("PYTHONUSERBASE", None)
185-
if env_base:
186-
return env_base
187182

188183
def joinuser(*args):
189184
return os.path.expanduser(os.path.join(*args))
190185

191186
if os.name == "nt":
192187
base = os.environ.get("APPDATA") or "~"
193-
return joinuser(base, "Python")
188+
if env_base:
189+
return env_base
190+
else:
191+
return joinuser(base, "Python")
194192

195-
if sys.platform == "darwin" and sys._framework:
196-
return joinuser("~", "Library", sys._framework,
197-
"%d.%d" % sys.version_info[:2])
193+
if sys.platform == "darwin":
194+
framework = get_config_var("PYTHONFRAMEWORK")
195+
if framework:
196+
if env_base:
197+
return env_base
198+
else:
199+
return joinuser("~", "Library", framework, "%d.%d" %
200+
sys.version_info[:2])
198201

199-
return joinuser("~", ".local")
202+
if env_base:
203+
return env_base
204+
else:
205+
return joinuser("~", ".local")
200206

201207

202208
def _parse_makefile(filename, vars=None):

Lib/test/test_site.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,6 @@ def test_addsitedir(self):
180180
finally:
181181
pth_file.cleanup()
182182

183-
def test_getuserbase(self):
184-
self.assertEqual(site._getuserbase(), sysconfig._getuserbase())
185-
186-
def test_get_path(self):
187-
self.assertEqual(site._get_path(site._getuserbase()),
188-
sysconfig.get_path('purelib', os.name + '_user'))
189-
190183
@unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 "
191184
"user-site (site.ENABLE_USER_SITE)")
192185
def test_s_option(self):

Misc/NEWS.d/next/Library/2017-06-29-00-17-38.bpo-29585.x2V0my.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Python/sysmodule.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,6 @@ _PySys_BeginInit(void)
19651965
SET_SYS_FROM_STRING("_git",
19661966
Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(),
19671967
_Py_gitversion()));
1968-
SET_SYS_FROM_STRING("_framework", PyUnicode_FromString(PYTHONFRAMEWORK));
19691968
SET_SYS_FROM_STRING("api_version",
19701969
PyLong_FromLong(PYTHON_API_VERSION));
19711970
SET_SYS_FROM_STRING("copyright",

configure

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,6 @@ infodir
783783
docdir
784784
oldincludedir
785785
includedir
786-
runstatedir
787786
localstatedir
788787
sharedstatedir
789788
sysconfdir
@@ -897,7 +896,6 @@ datadir='${datarootdir}'
897896
sysconfdir='${prefix}/etc'
898897
sharedstatedir='${prefix}/com'
899898
localstatedir='${prefix}/var'
900-
runstatedir='${localstatedir}/run'
901899
includedir='${prefix}/include'
902900
oldincludedir='/usr/include'
903901
docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
@@ -1150,15 +1148,6 @@ do
11501148
| -silent | --silent | --silen | --sile | --sil)
11511149
silent=yes ;;
11521150

1153-
-runstatedir | --runstatedir | --runstatedi | --runstated \
1154-
| --runstate | --runstat | --runsta | --runst | --runs \
1155-
| --run | --ru | --r)
1156-
ac_prev=runstatedir ;;
1157-
-runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \
1158-
| --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \
1159-
| --run=* | --ru=* | --r=*)
1160-
runstatedir=$ac_optarg ;;
1161-
11621151
-sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
11631152
ac_prev=sbindir ;;
11641153
-sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
@@ -1296,7 +1285,7 @@ fi
12961285
for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \
12971286
datadir sysconfdir sharedstatedir localstatedir includedir \
12981287
oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
1299-
libdir localedir mandir runstatedir
1288+
libdir localedir mandir
13001289
do
13011290
eval ac_val=\$$ac_var
13021291
# Remove trailing slashes.
@@ -1449,7 +1438,6 @@ Fine tuning of the installation directories:
14491438
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
14501439
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
14511440
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
1452-
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
14531441
--libdir=DIR object code libraries [EPREFIX/lib]
14541442
--includedir=DIR C header files [PREFIX/include]
14551443
--oldincludedir=DIR C header files for non-gcc [/usr/include]
@@ -3243,12 +3231,6 @@ fi
32433231

32443232

32453233

3246-
3247-
cat >>confdefs.h <<_ACEOF
3248-
#define PYTHONFRAMEWORK "${PYTHONFRAMEWORK}"
3249-
_ACEOF
3250-
3251-
32523234
##AC_ARG_WITH(dyld,
32533235
## AS_HELP_STRING([--with-dyld],
32543236
## [Use (OpenStep|Rhapsody) dynamic linker]))

configure.ac

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ AC_SUBST(FRAMEWORKPYTHONW)
355355
AC_SUBST(FRAMEWORKUNIXTOOLSPREFIX)
356356
AC_SUBST(FRAMEWORKINSTALLAPPSPREFIX)
357357

358-
AC_DEFINE_UNQUOTED(PYTHONFRAMEWORK, "${PYTHONFRAMEWORK}", [framework name])
359-
360358
##AC_ARG_WITH(dyld,
361359
## AS_HELP_STRING([--with-dyld],
362360
## [Use (OpenStep|Rhapsody) dynamic linker]))

pyconfig.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,6 @@
12471247
/* Define as the preferred size in bits of long digits */
12481248
#undef PYLONG_BITS_IN_DIGIT
12491249

1250-
/* framework name */
1251-
#undef PYTHONFRAMEWORK
1252-
12531250
/* Define if you want to coerce the C locale to a UTF-8 based locale */
12541251
#undef PY_COERCE_C_LOCALE
12551252

0 commit comments

Comments
 (0)