Skip to content
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

bpo-43312: Functions returning default and preferred sysconfig schemes #24644

Merged
merged 6 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions Doc/library/sysconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ identifier. Python currently uses eight paths:
:mod:`sysconfig`.


.. function:: get_default_scheme()

Return the default scheme name for the current platform.

.. versionadded:: 3.10


.. function:: get_preferred_scheme(key)

Return a preferred scheme name for an installation layout specified by *key*.

*key* must be either ``"prefix"``, ``"home"``, or ``"user"``.

The return value is a scheme name listed in :func:`get_scheme_names`. It
can be passed to :mod:`sysconfig` functions that take a *scheme* argument,
such as :func:`get_paths`.

.. versionadded:: 3.10


.. function:: get_path_names()

Return a tuple containing all path names currently supported in
Expand Down
33 changes: 29 additions & 4 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,38 @@ def _expand_vars(scheme, vars):
return res


def _get_default_scheme():
def get_default_scheme():
if os.name == 'posix':
# the default scheme for posix is posix_prefix
return 'posix_prefix'
return os.name
uranusjr marked this conversation as resolved.
Show resolved Hide resolved


def _get_preferred_schemes():
if os.name == 'nt':
return {
'prefix': 'nt',
'home': 'posix_home',
'user': 'nt_user',
}
if sys.platform == 'darwin' and sys._framework:
return {
'prefix': 'posix_prefix',
'home': 'posix_home',
'user': 'osx_framework_user',
}
return {
'prefix': 'posix_prefix',
'home': 'posix_home',
'user': 'posix_user',
}


def get_preferred_scheme(key):
scheme = _get_preferred_schemes()[key]
if scheme not in _INSTALL_SCHEMES:
raise KeyError(key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the above lookup not raise already? Should this be done before calling _get_preferred_schemes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main intention is to guard against incorrect implementation, since _get_preferred_schemes() is expected to be modified by redistributors and implementers. Say an implementer makes a typo:

def _get_preferred_schemes():
    return {
        "prefix": "posix_prefix",
        "home": "posix_home",
        "user": "posix_uesr",  # Oops!
    }

And when the value is later used:

scheme = sysconfig.get_preferred_scheme("user")

...  # Much later...

paths = sysconfig.get_paths(scheme)

This check would catch that posix_uesr is not actually a valid scheme and error early, instead of later when the user actually tries to use the (invalid) scheme.

Copy link
Member

Choose a reason for hiding this comment

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

Can we raise a ValueError with a better message then? "Key '{}' returned scheme '{}' which is not valid on this platform" (or something more correct)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn’t think of a better message and implemented yours for now, any ideas are very welcomed. Ideally I want to point users to the implementer/redistributor instead of reporting the error to CPython, but that’s probably not really feasible.

return scheme


def _parse_makefile(filename, vars=None):
Expand Down Expand Up @@ -517,7 +542,7 @@ def get_path_names():
return _SCHEME_KEYS


def get_paths(scheme=_get_default_scheme(), vars=None, expand=True):
def get_paths(scheme=get_default_scheme(), vars=None, expand=True):
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to call this function every time? Wouldn't be better to call it just once and save it into a private global variable, and use that as the default value for all these methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s a separate issue already presented in the previous implementation. I’m not sure this is the best plac eto discuss the possible implications in e.g. multiprocessing settings (I don’t even have enough expertise to comment on it).

As it currently stands, the function is only called twice (get_paths and get_path) on import time, so the performance difference is pretty close to none anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, let's keep the change to the minimal required.

"""Return a mapping containing an install scheme.

``scheme`` is the install scheme name. If not provided, it will
Expand All @@ -529,7 +554,7 @@ def get_paths(scheme=_get_default_scheme(), vars=None, expand=True):
return _INSTALL_SCHEMES[scheme]


def get_path(name, scheme=_get_default_scheme(), vars=None, expand=True):
def get_path(name, scheme=get_default_scheme(), vars=None, expand=True):
"""Return a path corresponding to the scheme.

``scheme`` is the install scheme name.
Expand Down Expand Up @@ -731,7 +756,7 @@ def _main():
return
print('Platform: "%s"' % get_platform())
print('Python version: "%s"' % get_python_version())
print('Current installation scheme: "%s"' % _get_default_scheme())
print('Current installation scheme: "%s"' % get_default_scheme())
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be a good opportunity to migrate this to f-strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably, I’ll combine this to other needed changes, if there are any.

print()
_print_dict('Paths', get_paths())
print()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sysconfig
from sysconfig import (get_paths, get_platform, get_config_vars,
get_path, get_path_names, _INSTALL_SCHEMES,
_get_default_scheme, _expand_vars,
get_default_scheme, _expand_vars,
get_scheme_names, get_config_var, _main)
import _osx_support

Expand Down Expand Up @@ -94,7 +94,7 @@ def test_get_path_names(self):

def test_get_paths(self):
scheme = get_paths()
default_scheme = _get_default_scheme()
default_scheme = get_default_scheme()
wanted = _expand_vars(default_scheme, None)
wanted = sorted(wanted.items())
scheme = sorted(scheme.items())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New functions :func:`sysconfig.get_preferred_scheme` and
:func:`sysconfig.get_default_scheme` are added to query a platform for its
preferred "user", "home", and "prefix" (default) scheme names.