Skip to content

Commit

Permalink
Make default scheme function public
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Mar 29, 2021
1 parent c8b5738 commit a0d613f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
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


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)
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):
"""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())
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.

0 comments on commit a0d613f

Please sign in to comment.