Skip to content

Commit 7594ef5

Browse files
committed
upgrade site to 3.14.2
1 parent ea1e60e commit 7594ef5

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

Lib/site.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
import os
7474
import builtins
7575
import _sitebuiltins
76-
import io
76+
import _io as io
7777
import stat
7878
import errno
7979

@@ -95,6 +95,12 @@ def _trace(message):
9595
print(message, file=sys.stderr)
9696

9797

98+
def _warn(*args, **kwargs):
99+
import warnings
100+
101+
warnings.warn(*args, **kwargs)
102+
103+
98104
def makepath(*paths):
99105
dir = os.path.join(*paths)
100106
try:
@@ -444,9 +450,9 @@ def setcopyright():
444450
"""Set 'copyright' and 'credits' in builtins"""
445451
builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright)
446452
builtins.credits = _sitebuiltins._Printer("credits", """\
447-
Thanks to CWI, CNRI, BeOpen, Zope Corporation, the Python Software
448-
Foundation, and a cast of thousands for supporting Python
449-
development. See www.python.org for more information.""")
453+
Thanks to CWI, CNRI, BeOpen, Zope Corporation, the Python Software
454+
Foundation, and a cast of thousands for supporting Python
455+
development. See www.python.org for more information.""")
450456
files, dirs = [], []
451457
# Not all modules are required to have a __file__ attribute. See
452458
# PEP 420 for more details.
@@ -574,7 +580,7 @@ def register_readline():
574580
def write_history():
575581
try:
576582
readline_module.write_history_file(history)
577-
except (FileNotFoundError, PermissionError):
583+
except FileNotFoundError, PermissionError:
578584
# home directory does not exist or is not writable
579585
# https://bugs.python.org/issue19891
580586
pass
@@ -626,17 +632,17 @@ def venv(known_paths):
626632
elif key == 'home':
627633
sys._home = value
628634

629-
sys.prefix = sys.exec_prefix = site_prefix
635+
if sys.prefix != site_prefix:
636+
_warn(f'Unexpected value in sys.prefix, expected {site_prefix}, got {sys.prefix}', RuntimeWarning)
637+
if sys.exec_prefix != site_prefix:
638+
_warn(f'Unexpected value in sys.exec_prefix, expected {site_prefix}, got {sys.exec_prefix}', RuntimeWarning)
630639

631640
# Doing this here ensures venv takes precedence over user-site
632641
addsitepackages(known_paths, [sys.prefix])
633642

634-
# addsitepackages will process site_prefix again if its in PREFIXES,
635-
# but that's ok; known_paths will prevent anything being added twice
636643
if system_site == "true":
637-
PREFIXES.insert(0, sys.prefix)
644+
PREFIXES += [sys.base_prefix, sys.base_exec_prefix]
638645
else:
639-
PREFIXES = [sys.prefix]
640646
ENABLE_USER_SITE = False
641647

642648
return known_paths
@@ -646,7 +652,7 @@ def execsitecustomize():
646652
"""Run custom site specific code, if available."""
647653
try:
648654
try:
649-
import sitecustomize
655+
import sitecustomize # noqa: F401
650656
except ImportError as exc:
651657
if exc.name == 'sitecustomize':
652658
pass
@@ -666,7 +672,7 @@ def execusercustomize():
666672
"""Run custom user specific code, if available."""
667673
try:
668674
try:
669-
import usercustomize
675+
import usercustomize # noqa: F401
670676
except ImportError as exc:
671677
if exc.name == 'usercustomize':
672678
pass

Lib/test/test_site.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import test.support
99
from test import support
1010
from test.support.script_helper import assert_python_ok
11+
from test.support import import_helper
1112
from test.support import os_helper
1213
from test.support import socket_helper
1314
from test.support import captured_stderr
@@ -308,8 +309,7 @@ def test_getuserbase(self):
308309

309310
with EnvironmentVarGuard() as environ:
310311
environ['PYTHONUSERBASE'] = 'xoxo'
311-
self.assertTrue(site.getuserbase().startswith('xoxo'),
312-
site.getuserbase())
312+
self.assertTrue(site.getuserbase().startswith('xoxo'))
313313

314314
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
315315
def test_getusersitepackages(self):
@@ -319,7 +319,7 @@ def test_getusersitepackages(self):
319319

320320
# the call sets USER_BASE *and* USER_SITE
321321
self.assertEqual(site.USER_SITE, user_site)
322-
self.assertTrue(user_site.startswith(site.USER_BASE), user_site)
322+
self.assertTrue(user_site.startswith(site.USER_BASE))
323323
self.assertEqual(site.USER_BASE, site.getuserbase())
324324

325325
def test_getsitepackages(self):
@@ -362,11 +362,10 @@ def test_no_home_directory(self):
362362
environ.unset('PYTHONUSERBASE', 'APPDATA')
363363

364364
user_base = site.getuserbase()
365-
self.assertTrue(user_base.startswith('~' + os.sep),
366-
user_base)
365+
self.assertTrue(user_base.startswith('~' + os.sep))
367366

368367
user_site = site.getusersitepackages()
369-
self.assertTrue(user_site.startswith(user_base), user_site)
368+
self.assertTrue(user_site.startswith(user_base))
370369

371370
with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \
372371
mock.patch.object(site, 'addsitedir') as mock_addsitedir, \
@@ -515,7 +514,7 @@ def test_sitecustomize_executed(self):
515514
# If sitecustomize is available, it should have been imported.
516515
if "sitecustomize" not in sys.modules:
517516
try:
518-
import sitecustomize
517+
import sitecustomize # noqa: F401
519518
except ImportError:
520519
pass
521520
else:
@@ -578,6 +577,17 @@ def test_license_exists_at_url(self):
578577
code = e.code
579578
self.assertEqual(code, 200, msg="Can't find " + url)
580579

580+
@support.cpython_only
581+
def test_lazy_imports(self):
582+
import_helper.ensure_lazy_imports("site", [
583+
"io",
584+
"locale",
585+
"traceback",
586+
"atexit",
587+
"warnings",
588+
"textwrap",
589+
])
590+
581591

582592
class StartupImportTests(unittest.TestCase):
583593

@@ -843,12 +853,15 @@ def get_excepted_output(self, *args):
843853
return 10, None
844854

845855
def invoke_command_line(self, *args):
846-
args = ["-m", "site", *args]
856+
cmd_args = []
857+
if sys.flags.no_user_site:
858+
cmd_args.append("-s")
859+
cmd_args.extend(["-m", "site", *args])
847860

848861
with EnvironmentVarGuard() as env:
849862
env["PYTHONUTF8"] = "1"
850863
env["PYTHONIOENCODING"] = "utf-8"
851-
proc = spawn_python(*args, text=True, env=env,
864+
proc = spawn_python(*cmd_args, text=True, env=env,
852865
encoding='utf-8', errors='replace')
853866

854867
output = kill_python(proc)

0 commit comments

Comments
 (0)