Skip to content
Closed
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
57 changes: 41 additions & 16 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
import _sitebuiltins
import io

_is_pypy = '__pypy__' in sys.builtin_module_names

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
Expand Down Expand Up @@ -258,6 +260,18 @@ def check_enableusersite():
#
# See https://bugs.python.org/issue29585

# Copy of sysconfig._get_impllibdir()
def _get_impllibdir(os_name):
if not _is_pypy and os_name != 'nt':
return 'python'
elif not _is_pypy and os_name == 'nt':
return 'Python'
elif _is_pypy and os_name != 'nt':
return 'pypy'
elif _is_pypy and os_name == 'nt':
return 'PyPy'


# Copy of sysconfig._getuserbase()
def _getuserbase():
env_base = os.environ.get("PYTHONUSERBASE", None)
Expand All @@ -273,7 +287,7 @@ def joinuser(*args):

if os.name == "nt":
base = os.environ.get("APPDATA") or "~"
return joinuser(base, "Python")
return joinuser(base, _get_impllibdir(os.name))

if sys.platform == "darwin" and sys._framework:
return joinuser("~", "Library", sys._framework,
Expand All @@ -286,14 +300,15 @@ def joinuser(*args):
def _get_path(userbase):
version = sys.version_info

impllib = _get_impllibdir(os.name)
if os.name == 'nt':
ver_nodot = sys.winver.replace('.', '')
return f'{userbase}\\Python{ver_nodot}\\site-packages'
return f'{userbase}\\{impllib}{ver_nodot}\\site-packages'

if sys.platform == 'darwin' and sys._framework:
return f'{userbase}/lib/python/site-packages'
return f'{userbase}/lib/{impllib}/site-packages'

return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
return f'{userbase}/lib/{impllib}{version[0]}.{version[1]}/site-packages'


def getuserbase():
Expand Down Expand Up @@ -405,25 +420,35 @@ 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", """\
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 = [], []
# Not all modules are required to have a __file__ attribute. See
# PEP 420 for more details.
if hasattr(os, '__file__'):
here = os.path.dirname(os.__file__)
files.extend(["LICENSE.txt", "LICENSE"])
dirs.extend([os.path.join(here, os.pardir), here, os.curdir])
builtins.license = _sitebuiltins._Printer(
"license",
"See https://www.python.org/psf/license/",
files, dirs)

if sys.platform[:4] == 'java':
builtins.credits = _sitebuiltins._Printer(
"credits",
"Jython is maintained by the Jython developers (www.jython.org).")
builtins.license = _sitebuiltins._Printer("license",
"See https://www.python.org/psf/license/",
files, dirs)
elif _is_pypy:
builtins.credits = _sitebuiltins._Printer("credits", """\
PyPy is maintained by the PyPy developers and contributors (http://pypy.org)
It is heavily based on CPython and the great work of the Python community.""")
builtins.license = _sitebuiltins._Printer("license",
"See https://foss.heptapod.net/pypy/pypy/src/default/LICENSE",
files, dirs)
else: # CPython
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.""")
builtins.license = _sitebuiltins._Printer("license",
"See https://www.python.org/psf/license/",
files, dirs)


def sethelper():
Expand Down
72 changes: 44 additions & 28 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@

_INSTALL_SCHEMES = {
'posix_prefix': {
'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
'purelib': '{base}/lib/python{py_version_short}/site-packages',
'platlib': '{platbase}/{platlibdir}/python{py_version_short}/site-packages',
'stdlib': '{installed_base}/{platlibdir}/{impllibdir}{py_version_short}',
'platstdlib': '{platbase}/{platlibdir}/{impllibdir}{py_version_short}',
'purelib': '{base}/lib/{impllibdir}{py_version_short}/site-packages',
'platlib': '{platbase}/{platlibdir}/{impllibdir}{py_version_short}/site-packages',
'include':
'{installed_base}/include/python{py_version_short}{abiflags}',
'{installed_base}/include/{impllibdir}{py_version_short}{abiflags}',
'platinclude':
'{installed_platbase}/include/python{py_version_short}{abiflags}',
'{installed_platbase}/include/{impllibdir}{py_version_short}{abiflags}',
'scripts': '{base}/bin',
'data': '{base}',
},
'posix_home': {
'stdlib': '{installed_base}/lib/python',
'platstdlib': '{base}/lib/python',
'purelib': '{base}/lib/python',
'platlib': '{base}/lib/python',
'include': '{installed_base}/include/python',
'platinclude': '{installed_base}/include/python',
'stdlib': '{installed_base}/lib/{impllibdir}',
'platstdlib': '{base}/lib/{impllibdir}',
'purelib': '{base}/lib/{impllibdir}',
'platlib': '{base}/lib/{impllibdir}',
'include': '{installed_base}/include/{impllibdir}',
'platinclude': '{installed_base}/include/{impllibdir}',
'scripts': '{base}/bin',
'data': '{base}',
},
Expand All @@ -59,6 +59,21 @@
}


_is_pypy = '__pypy__' in sys.builtin_module_names

# NOTE: site.py has copy of this function.
# Sync it when modify this function.
def _get_impllibdir(os_name):
if not _is_pypy and os_name != 'nt':
return 'python'
elif not _is_pypy and os_name == 'nt':
return 'Python'
elif _is_pypy and os_name != 'nt':
return 'pypy'
elif _is_pypy and os_name == 'nt':
return 'PyPy'

Comment on lines +62 to +75

@tiran tiran Apr 22, 2021

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.

Is there any reason why you have duplicated the code instead of importing them from site? The site module is almost always imported at startup.

Suggested change
_is_pypy = '__pypy__' in sys.builtin_module_names
# NOTE: site.py has copy of this function.
# Sync it when modify this function.
def _get_impllibdir(os_name):
if not _is_pypy and os_name != 'nt':
return 'python'
elif not _is_pypy and os_name == 'nt':
return 'Python'
elif _is_pypy and os_name != 'nt':
return 'pypy'
elif _is_pypy and os_name == 'nt':
return 'PyPy'
_is_pypy = '__pypy__' in sys.builtin_module_names
# NOTE: site.py has copy of this function.
# Sync it when modify this function.
def _get_impllibdir(os_name):
if _is_pypy:
return 'PyPy' if os_name == 'nt' else 'pypy'
else:
return 'Python' if os_name == 'nt' else 'python'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the design is that the site module can be overridden by the user, so it cannot be the canonical source of information. I think the sysconfig code was originally copied to prevent site.py importing sysconfig.py during startup, which would slow things down. I was just following that usage pattern.

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.

Do you have references for that? sitecustomize is for custom behaviour, but I don’t see that site is meant to be replaced.

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.

I could see a bootstrap problem though, for example during Python build we may need sysconfig but can’t import site (that needs modules such as io and others).


# NOTE: site.py has copy of this function.
# Sync it when modify this function.
def _getuserbase():
Expand All @@ -75,7 +90,7 @@ def joinuser(*args):

if os.name == "nt":
base = os.environ.get("APPDATA") or "~"
return joinuser(base, "Python")
return joinuser(base, _get_impllibdir(os.name))

if sys.platform == "darwin" and sys._framework:
return joinuser("~", "Library", sys._framework,
Expand All @@ -89,28 +104,28 @@ def joinuser(*args):
_INSTALL_SCHEMES |= {
# NOTE: When modifying "purelib" scheme, update site._get_path() too.
'nt_user': {
'stdlib': '{userbase}/Python{py_version_nodot_plat}',
'platstdlib': '{userbase}/Python{py_version_nodot_plat}',
'purelib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
'platlib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
'include': '{userbase}/Python{py_version_nodot_plat}/Include',
'scripts': '{userbase}/Python{py_version_nodot_plat}/Scripts',
'stdlib': '{userbase}/{impllibdir}{py_version_nodot_plat}',
'platstdlib': '{userbase}/{impllibdir}{py_version_nodot_plat}',
'purelib': '{userbase}/{impllibdir}{py_version_nodot_plat}/site-packages',
'platlib': '{userbase}/{impllibdir}{py_version_nodot_plat}/site-packages',
'include': '{userbase}/{impllibdir}{py_version_nodot_plat}/Include',
'scripts': '{userbase}/{impllibdir}{py_version_nodot_plat}/Scripts',
'data': '{userbase}',
},
'posix_user': {
'stdlib': '{userbase}/{platlibdir}/python{py_version_short}',
'platstdlib': '{userbase}/{platlibdir}/python{py_version_short}',
'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
'platlib': '{userbase}/{platlibdir}/python{py_version_short}/site-packages',
'include': '{userbase}/include/python{py_version_short}',
'stdlib': '{userbase}/{platlibdir}/{impllibdir}{py_version_short}',
'platstdlib': '{userbase}/{platlibdir}/{impllibdir}{py_version_short}',
'purelib': '{userbase}/lib/{impllibdir}{py_version_short}/site-packages',
'platlib': '{userbase}/{platlibdir}/{impllibdir}{py_version_short}/site-packages',
'include': '{userbase}/include/{impllibdir}{py_version_short}',
'scripts': '{userbase}/bin',
'data': '{userbase}',
},
'osx_framework_user': {
'stdlib': '{userbase}/lib/python',
'platstdlib': '{userbase}/lib/python',
'purelib': '{userbase}/lib/python/site-packages',
'platlib': '{userbase}/lib/python/site-packages',
'stdlib': '{userbase}/lib/{impllibdir}',
'platstdlib': '{userbase}/lib/{impllibdir}',
'purelib': '{userbase}/lib/{impllibdir}/site-packages',
'platlib': '{userbase}/lib/{impllibdir}/site-packages',
'include': '{userbase}/include',
'scripts': '{userbase}/bin',
'data': '{userbase}',
Expand Down Expand Up @@ -569,6 +584,7 @@ def get_config_vars(*args):
except AttributeError:
# sys.abiflags may not be defined on all platforms.
_CONFIG_VARS['abiflags'] = ''
_CONFIG_VARS['impllibdir'] = _get_impllibdir(os.name)
try:
_CONFIG_VARS['py_version_nodot_plat'] = sys.winver.replace('.', '')
except AttributeError:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extend the ``INSTALL_SCHEMA`` in sysconfig.py to handle additional python implementations (like PyPy)