8000 Update site from CPython v3.12.3 by youknowone · Pull Request #5313 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Update site from CPython v3.12.3 #5313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
3 changes: 1 addition & 2 deletions Lib/_sitebuiltins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import sys


class Quitter(object):
def __init__(self, name, eof):
self.name = name
Expand Down Expand Up @@ -48,7 +47,7 @@ def __setup(self):
data = None
for filename in self.__filenames:
try:
with open(filename, "r") as fp:
with open(filename, encoding='utf-8') as fp:
data = fp.read()
break
except OSError:
Expand Down
44 changes: 26 additions & 18 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import builtins
import _sitebuiltins
import io
import stat

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
Expand Down Expand Up @@ -168,6 +169,14 @@ def addpackage(sitedir, name, known_paths):
else:
reset = False
fullname = os.path.join(sitedir, name)
try:
st = os.lstat(fullname)
except OSError:
return
if ((getattr(st, 'st_flags', 0) & stat.UF_HIDDEN) or
(getattr(st, 'st_file_attributes', 0) & stat.FILE_ATTRIBUTE_HIDDEN)):
_trace(f"Skipping hidden .pth file: {fullname!r}")
return
_trace(f"Processing .pth file: {fullname!r}")
try:
# locale encoding is not ideal especially on Windows. But we have used
Expand All @@ -190,11 +199,11 @@ def addpackage(sitedir, name, known_paths):
if not dircase in known_paths and os.path.exists(dir):
sys.path.append(dir)
known_paths.add(dircase)
except Exception:
except Exception as exc:
print("Error processing line {:d} of {}:\n".format(n+1, fullname),
file=sys.stderr)
import traceback
for record in traceback.format_exception(*sys.exc_info()):
for record in traceback.format_exception(exc):
for line in record.splitlines():
print(' '+line, file=sys.stderr)
print("\nRemainder of file ignored", file=sys.stderr)
Expand All @@ -221,7 +230,8 @@ def addsitedir(sitedir, known_paths=None):
names = os.listdir(sitedir)
except OSError:
return
names = [name for name in names if name.endswith(".pth")]
names = [name for name in names
if name.endswith(".pth") and not name.startswith(".")]
for name in sorted(names):
addpackage(sitedir, name, known_paths)
if reset:
Expand Down Expand Up @@ -406,12 +416,7 @@ def setquit():
def setcopyright():
"""Set 'copyright' and 'credits' in builtins"""
builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright)
if sys.platform[:4] == 'java':
builtins.credits = _sitebuiltins._Printer(
"credits",
"Jython is maintained by the Jython developers (www.jython.org).")
else:
builtins.credits = _sitebuiltins._Printer("credits", """\
builtins.credits = _sitebuiltins._Printer("credits", """\
Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
for supporting Python development. See www.python.org for more information.""")
files, dirs = [], []
Expand Down Expand Up @@ -499,20 +504,23 @@ def venv(known_paths):
executable = sys._base_executable = os.environ['__PYVENV_LAUNCHER__']
else:
executable = sys.executable
exe_dir, _ = os.path.split(os.path.abspath(executable))
exe_dir = os.path.dirname(os.path.abspath(executable))
site_prefix = os.path.dirname(exe_dir)
sys._home = None
conf_basename = 'pyvenv.cfg'
candidate_confs = [
conffile for conffile in (
os.path.join(exe_dir, conf_basename),
os.path.join(site_prefix, conf_basename)
candidate_conf = next(
(
conffile for conffile in (
os.path.join(exe_dir, conf_basename),
os.path.join(site_prefix, conf_basename)
)
if os.path.isfile(conffile)
]
if os.path.isfile(conffile)
),
None
)

if candidate_confs:
virtual_conf = candidate_confs[0]
if candidate_conf:
virtual_conf = candidate_conf
system_site = "true"
# Issue 25185: Use UTF-8, as that's what the venv module uses when
# writing the file.
Expand Down
96 changes: 68 additions & 28 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
from test.support import os_helper
from test.support import socket_helper
from test.support import captured_stderr
from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd
from test.support.os_helper import TESTFN, EnvironmentVarGuard
import ast
import builtins
import encodings
import glob
import io
import os
import re
import shutil
import stat
import subprocess
import sys
import sysconfig
Expand Down Expand Up @@ -195,6 +195,47 @@ def test_addsitedir(self):
finally:
pth_file.cleanup()

def test_addsitedir_dotfile(self):
pth_file = PthFile('.dotfile')
pth_file.cleanup(prep=True)
try:
pth_file.create()
site.addsitedir(pth_file.base_dir, set())
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
self.assertIn(pth_file.base_dir, sys.path)
finally:
pth_file.cleanup()

@unittest.skipUnless(hasattr(os, 'chflags'), 'test needs os.chflags()')
def test_addsitedir_hidden_flags(self):
pth_file = PthFile()
pth_file.cleanup(prep=True)
try:
pth_file.create()
st = os.stat(pth_file.file_path)
os.chflags(pth_file.file_path, st.st_flags | stat.UF_HIDDEN)
site.addsitedir(pth_file.base_dir, set())
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
self.assertIn(pth_file.base_dir, sys.path)
finally:
pth_file.cleanup()

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.skipUnless(sys.platform == 'win32', 'test needs Windows')
@support.requires_subprocess()
def test_addsitedir_hidden_file_attribute(self):
pth_file = PthFile()
pth_file.cleanup(prep=True)
try:
pth_file.create()
subprocess.check_call(['attrib', '+H', pth_file.file_path])
site.addsitedir(pth_file.base_dir, set())
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
self.assertIn(pth_file.base_dir, sys.path)
finally:
pth_file.cleanup()

# This tests _getuserbase, hence the double underline
# to distinguish from a test for getuserbase
def test__getuserbase(self):
Expand Down Expand Up @@ -468,10 +509,10 @@ def test_sitecustomize_executed(self):
else:
self.fail("sitecustomize not imported automatically")

@test.support.requires_resource('network')
@test.support.system_must_validate_cert
@unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
'need SSL support to download license')
@test.support.requires_resource('network')
@test.support.system_must_validate_cert
def test_license_exists_at_url(self):
# This test is a bit fragile since it depends on the format of the
# string displayed by license in the absence of a LICENSE file.
Expand Down Expand Up @@ -581,7 +622,7 @@ def _create_underpth_exe(self, lines, exe_pth=True):
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
else:
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
with open(_pth_file, 'w') as f:
with open(_pth_file, 'w', encoding='utf8') as f:
for line in lines:
print(line, file=f)
return exe_file
Expand All @@ -608,19 +649,33 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
sys_path.append(abs_path)
return sys_path

def _get_pth_lines(self, libpath: str, *, import_site: bool):
pth_lines = ['fake-path-name']
# include 200 lines of `libpath` in _pth lines (or fewer
# if the `libpath` is long enough to get close to 32KB
# see https://github.com/python/cpython/issues/113628)
encoded_libpath_length = len(libpath.encode("utf-8"))
repetitions = min(200, 30000 // encoded_libpath_length)
if repetitions <= 2:
self.skipTest(
f"Python stdlib path is too long ({encoded_libpath_length:,} bytes)")
pth_lines.extend(libpath for _ in range(repetitions))
pth_lines.extend(['', '# comment'])
if import_site:
pth_lines.append('import site')
return pth_lines

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.requires_subprocess()
def test_underpth_basic(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
pth_lines = ['#.', '# ..', *sys.path, '.', '..']
exe_file = self._create_underpth_exe(pth_lines)
sys_path = self._calc_sys_path_for_underpth_nosite(
os.path.dirname(exe_file),
pth_lines)

output = subprocess.check_output([exe_file, '-c',
output = subprocess.check_output([exe_file, '-X', 'utf8', '-c',
'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
], encoding='utf-8', errors='surrogateescape')
actual_sys_path = output.rstrip().split('\n')
Expand All @@ -637,12 +692,7 @@ def test_underpth_basic(self):
def test_underpth_nosite_file(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
pth_lines = [
'fake-path-name',
*[libpath for _ in range(200)],
'',
'# comment',
]
pth_lines = self._get_pth_lines(libpath, import_site=False)
exe_file = self._create_underpth_exe(pth_lines)
sys_path = self._calc_sys_path_for_underpth_nosite(
os.path.dirname(exe_file),
Expand All @@ -668,13 +718,8 @@ def test_underpth_nosite_file(self):
def test_underpth_file(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
exe_file = self._create_underpth_exe([
'fake-path-name',
*[libpath for _ in range(200)],
'',
'# comment',
'import site'
])
exe_file = self._create_underpth_exe(
self._get_pth_lines(libpath, import_site=True))
sys_prefix = os.path.dirname(exe_file)
env = os.environ.copy()
env['PYTHONPATH'] = 'from-env'
Expand All @@ -695,13 +740,8 @@ def test_underpth_file(self):
def test_underpth_dll_file(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
exe_file = self._create_underpth_exe([
'fake-path-name',
*[libpath for _ in range(200)],
'',
'# comment',
'import site'
], exe_pth=False)
exe_file = self._create_underpth_exe(
self._get_pth_lines(libpath, import_site=True), exe_pth=False)
sys_prefix = os.path.dirname(exe_file)
env = os.environ.copy()
env['PYTHONPATH'] = 'from-env'
Expand Down
Loading
0