Skip to content

Commit 9c74dd8

Browse files
CPython Developersyouknowone
authored andcommitted
Update site from CPython 3.10.6
1 parent e5735cd commit 9c74dd8

File tree

1 file changed

+68
-20
lines changed

1 file changed

+68
-20
lines changed

Lib/site.py

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import os
7474
import builtins
7575
import _sitebuiltins
76+
import io
7677

7778
# Prefixes for site-packages; add additional prefixes like /usr/local here
7879
PREFIXES = [sys.prefix, sys.exec_prefix]
@@ -87,6 +88,11 @@
8788
USER_BASE = None
8889

8990

91+
def _trace(message):
92+
if sys.flags.verbose:
93+
print(message, file=sys.stderr)
94+
95+
9096
def makepath(*paths):
9197
dir = os.path.join(*paths)
9298
try:
@@ -99,8 +105,15 @@ def makepath(*paths):
99105
def abs_paths():
100106
"""Set all module __file__ and __cached__ attributes to an absolute path"""
101107
for m in set(sys.modules.values()):
102-
if (getattr(getattr(m, '__loader__', None), '__module__', None) not in
103-
('_frozen_importlib', '_frozen_importlib_external')):
108+
loader_module = None
109+
try:
110+
loader_module = m.__loader__.__module__
111+
except AttributeError:
112+
try:
113+
loader_module = m.__spec__.loader.__module__
114+
except AttributeError:
115+
pass
116+
if loader_module not in {'_frozen_importlib', '_frozen_importlib_external'}:
104117
continue # don't mess with a PEP 302-supplied __file__
105118
try:
106119
m.__file__ = os.path.abspath(m.__file__)
@@ -155,14 +168,19 @@ def addpackage(sitedir, name, known_paths):
155168
else:
156169
reset = False
157170
fullname = os.path.join(sitedir, name)
171+
_trace(f"Processing .pth file: {fullname!r}")
158172
try:
159-
f = open(fullname, "r")
173+
# locale encoding is not ideal especially on Windows. But we have used
174+
# it for a long time. setuptools uses the locale encoding too.
175+
f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
160176
except OSError:
161177
return
162178
with f:
163179
for n, line in enumerate(f):
164180
if line.startswith("#"):
165181
continue
182+
if line.strip() == "":
183+
continue
166184
try:
167185
if line.startswith(("import ", "import\t")):
168186
exec(line)
@@ -189,6 +207,7 @@ def addpackage(sitedir, name, known_paths):
189207
def addsitedir(sitedir, known_paths=None):
190208
"""Add 'sitedir' argument to sys.path if missing and handle .pth files in
191209
'sitedir'"""
210+
_trace(f"Adding directory: {sitedir!r}")
192211
if known_paths is None:
193212
known_paths = _init_pathinfo()
194213
reset = True
@@ -247,6 +266,10 @@ def _getuserbase():
247266
if env_base:
248267
return env_base
249268

269+
# VxWorks has no home directories
270+
if sys.platform == "vxworks":
271+
return None
272+
250273
def joinuser(*args):
251274
return os.path.expanduser(os.path.join(*args))
252275

@@ -265,9 +288,9 @@ def joinuser(*args):
265288
def _get_path(userbase):
266289
version = sys.version_info
267290

268-
# XXX RUSTPYTHON: we replace pythonx.y with rustpythonx.y
269291
if os.name == 'nt':
270-
return f'{userbase}\\RustPython{version[0]}{version[1]}\\site-packages'
292+
ver_nodot = sys.winver.replace('.', '')
293+
return f'{userbase}\\RustPython{ver_nodot}\\site-packages'
271294

272295
if sys.platform == 'darwin' and sys._framework:
273296
return f'{userbase}/lib/rustpython/site-packages'
@@ -294,11 +317,14 @@ def getusersitepackages():
294317
If the global variable ``USER_SITE`` is not initialized yet, this
295318
function will also set it.
296319
"""
297-
global USER_SITE
320+
global USER_SITE, ENABLE_USER_SITE
298321
userbase = getuserbase() # this will also set USER_BASE
299322

300323
if USER_SITE is None:
301-
USER_SITE = _get_path(userbase)
324+
if userbase is None:
325+
ENABLE_USER_SITE = False # disable user site and return None
326+
else:
327+
USER_SITE = _get_path(userbase)
302328

303329
return USER_SITE
304330

@@ -310,6 +336,7 @@ def addusersitepackages(known_paths):
310336
"""
311337
# get the per user site-package path
312338
# this call will also make sure USER_BASE and USER_SITE are set
339+
_trace("Processing user site-packages")
313340
user_site = getusersitepackages()
314341

315342
if ENABLE_USER_SITE and os.path.isdir(user_site):
@@ -334,18 +361,27 @@ def getsitepackages(prefixes=None):
334361
continue
335362
seen.add(prefix)
336363

364+
libdirs = [sys.platlibdir]
365+
if sys.platlibdir != "lib":
366+
libdirs.append("lib")
367+
337368
if os.sep == '/':
338-
sitepackages.append(os.path.join(prefix, "lib",
339-
# XXX changed for RustPython
340-
"rustpython%d.%d" % sys.version_info[:2],
341-
"site-packages"))
369+
for libdir in libdirs:
370+
path = os.path.join(prefix, libdir,
371+
"python%d.%d" % sys.version_info[:2],
372+
"site-packages")
373+
sitepackages.append(path)
342374
else:
343375
sitepackages.append(prefix)
344-
sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
376+
377+
for libdir in libdirs:
378+
path = os.path.join(prefix, libdir, "site-packages")
379+
sitepackages.append(path)
345380
return sitepackages
346381

347382
def addsitepackages(known_paths, prefixes=None):
348383
"""Add site-packages to sys.path"""
384+
_trace("Processing global site-packages")
349385
for sitedir in getsitepackages(prefixes):
350386
if os.path.isdir(sitedir):
351387
addsitedir(sitedir, known_paths)
@@ -441,7 +477,16 @@ def register_readline():
441477
readline.read_history_file(history)
442478
except OSError:
443479
pass
444-
atexit.register(readline.write_history_file, history)
480+
481+
def write_history():
482+
try:
483+
readline.write_history_file(history)
484+
except OSError:
485+
# bpo-19891, bpo-41193: Home directory does not exist
486+
# or is not writable, or the filesystem is read-only.
487+
pass
488+
489+
atexit.register(write_history)
445490

446491
sys.__interactivehook__ = register_readline
447492

@@ -450,7 +495,7 @@ def venv(known_paths):
450495

451496
env = os.environ
452497
if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env:
453-
executable = os.environ['__PYVENV_LAUNCHER__']
498+
executable = sys._base_executable = os.environ['__PYVENV_LAUNCHER__']
454499
else:
455500
executable = sys.executable
456501
exe_dir, _ = os.path.split(os.path.abspath(executable))
@@ -582,7 +627,7 @@ def _script():
582627
Exit codes with --user-base or --user-site:
583628
0 - user site directory is enabled
584629
1 - user site directory is disabled by user
585-
2 - uses site directory is disabled by super user
630+
2 - user site directory is disabled by super user
586631
or for security reasons
587632
>2 - unknown error
588633
"""
@@ -594,11 +639,14 @@ def _script():
594639
for dir in sys.path:
595640
print(" %r," % (dir,))
596641
print("]")
597-
print("USER_BASE: %r (%s)" % (user_base,
598-
"exists" if os.path.isdir(user_base) else "doesn't exist"))
599-
print("USER_SITE: %r (%s)" % (user_site,
600-
"exists" if os.path.isdir(user_site) else "doesn't exist"))
601-
print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE)
642+
def exists(path):
643+
if path is not None and os.path.isdir(path):
644+
return "exists"
645+
else:
646+
return "doesn't exist"
647+
print(f"USER_BASE: {user_base!r} ({exists(user_base)})")
648+
print(f"USER_SITE: {user_site!r} ({exists(user_site)})")
649+
print(f"ENABLE_USER_SITE: {ENABLE_USER_SITE!r}")
602650
sys.exit(0)
603651

604652
buffer = []

0 commit comments

Comments
 (0)