The returned string points into static storage; the caller should not modify its
value.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. c:function:: wchar_t* Py_GetPrefix()
script at build time. The value is available to Python code as ``sys.prefix``.
It is only useful on Unix. See also the next function.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. c:function:: wchar_t* Py_GetExecPrefix()
while having :file:`/usr/local/plat` be a different filesystem for each
platform.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. c:function:: wchar_t* Py_GetProgramFullPath()
static storage; the caller should not modify its value. The value is available
to Python code as ``sys.executable``.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. c:function:: wchar_t* Py_GetPath()
can be (and usually is) modified later to change the search path for loading
modules.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
.. XXX should give the exact rules
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. c:function:: void Py_SetPath(const wchar_t *)
:c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
environment variable if it is set.
+ This function should not be called before :c:func:`Py_Initialize`, otherwise
+ it returns ``NULL``.
+
+ .. versionchanged:: 3.10
+ It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
+
.. _threads:
environments variables are ignored, the LC_CTYPE locale is left unchanged and
no signal handler is registred.
+The :c:func:`Py_RunMain` function can be used to write a customized Python
+program.
+
See also :ref:`Initialization, Finalization, and Threads <initialization>`.
.. seealso::
:pep:`587` "Python Initialization Configuration".
+
Example
=======
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* base_executable
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* base_prefix
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int buffered_stdio
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* executable
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int faulthandler
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` input.
+ Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: int import_time
Default: value of the ``PLATLIBDIR`` macro which is set at configure time
by ``--with-platlibdir`` (default: ``"lib"``).
- Part of the :ref:`Path Configuration <init-path-config>` input.
+ Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. versionadded:: 3.9
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` input.
+ Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: PyWideStringList module_search_paths
.. c:member:: int module_search_paths_set
Module search paths: :data:`sys.path`.
If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
- function calculating the :ref:`Path Configuration <init-path-config>`
+ function calculating the :ref:`Python Path Configuration <init-path-config>`
overrides the :c:member:`~PyConfig.module_search_paths` and sets
:c:member:`~PyConfig.module_search_paths_set` to ``1``.
Default: empty list (``module_search_paths``) and ``0``
(``module_search_paths_set``).
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int optimization_level
.. c:member:: int pathconfig_warnings
- On Unix, if non-zero, calculating the :ref:`Path Configuration
+ On Unix, if non-zero, calculating the :ref:`Python Path Configuration
<init-path-config>` can log warnings into ``stderr``. If equals to 0,
suppress these warnings.
Default: ``1`` in Python mode, ``0`` in isolated mode.
- Part of the :ref:`Path Configuration <init-path-config>` input.
+ Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: wchar_t* prefix
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` output.
+ Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* program_name
Default: ``NULL``.
- Part of the :ref:`Path Configuration <init-path-config>` input.
+ Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: wchar_t* pycache_prefix
LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Configuration files are still used with this configuration. Set the
-:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
+:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these
configuration files and avoid the function computing the default path
configuration.
.. _init-path-config:
-Path Configuration
-==================
+Python Path Configuration
+=========================
:c:type:`PyConfig` contains multiple fields for the path configuration:
``unicodedata.ucnhash_CAPI`` has been moved to the internal C API.
(Contributed by Victor Stinner in :issue:`42157`.)
+* :c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
+ :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
+ :c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
+ :c:func:`Py_Initialize` (before Python is initialized). Use the new
+ :ref:`Python Initialization Configuration API <init-config>` to get the
+ :ref:`Python Path Configuration. <init-path-config>`.
+ (Contributed by Victor Stinner in :issue:`42260`.)
+
Deprecated
----------
extern PyStatus _PyConfig_Copy(
PyConfig *config,
const PyConfig *config2);
-extern PyStatus _PyConfig_InitPathConfig(PyConfig *config);
+extern PyStatus _PyConfig_InitPathConfig(
+ PyConfig *config,
+ int compute_path_config);
extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
extern PyStatus _PyConfig_Write(const PyConfig *config,
struct pyruntimestate *runtime);
--- /dev/null
+:c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
+:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
+:c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
+:c:func:`Py_Initialize` (before Python is initialized). Use the new
+:ref:`Python Initialization Configuration API <init-config>` to get the
+:ref:`Python Path Configuration. <init-path-config>`. Patch by Victor
+Stinner.
}
}
- if (compute_path_config && config->_install_importlib) {
- status = _PyConfig_InitPathConfig(config);
+ if (config->_install_importlib) {
+ status = _PyConfig_InitPathConfig(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- _PyPathConfig_Calculate()
*/
static PyStatus
-pathconfig_calculate(_PyPathConfig *pathconfig, const PyConfig *config)
+pathconfig_init(_PyPathConfig *pathconfig, const PyConfig *config,
+ int compute_path_config)
{
PyStatus status;
goto done;
}
- if (_Py_path_config.module_search_path == NULL) {
+ if (compute_path_config) {
status = _PyPathConfig_Calculate(pathconfig, config);
}
- else {
- /* Py_SetPath() has been called: avoid _PyPathConfig_Calculate() */
- }
done:
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
static PyStatus
-config_calculate_pathconfig(PyConfig *config)
+config_init_pathconfig(PyConfig *config, int compute_path_config)
{
_PyPathConfig pathconfig = _PyPathConfig_INIT;
PyStatus status;
- status = pathconfig_calculate(&pathconfig, config);
+ status = pathconfig_init(&pathconfig, config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
- if (!config->module_search_paths_set) {
+ if (!config->module_search_paths_set
+ && pathconfig.module_search_path != NULL)
+ {
status = config_init_module_search_paths(config, &pathconfig);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
#define COPY_ATTR(PATH_ATTR, CONFIG_ATTR) \
- if (config->CONFIG_ATTR == NULL) { \
+ if (config->CONFIG_ATTR == NULL && pathconfig.PATH_ATTR != NULL) { \
if (copy_wstr(&config->CONFIG_ATTR, pathconfig.PATH_ATTR) < 0) { \
goto no_memory; \
} \
PyStatus
-_PyConfig_InitPathConfig(PyConfig *config)
+_PyConfig_InitPathConfig(PyConfig *config, int compute_path_config)
{
/* Do we need to calculate the path? */
if (!config->module_search_paths_set
|| config->prefix == NULL
|| config->exec_prefix == NULL)
{
- PyStatus status = config_calculate_pathconfig(config);
+ PyStatus status = config_init_pathconfig(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
- if (config->base_prefix == NULL) {
+ if (config->base_prefix == NULL && config->prefix != NULL) {
if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
return _PyStatus_NO_MEMORY();
}
}
- if (config->base_exec_prefix == NULL) {
+ if (config->base_exec_prefix == NULL && config->exec_prefix != NULL) {
if (copy_wstr(&config->base_exec_prefix,
config->exec_prefix) < 0) {
return _PyStatus_NO_MEMORY();
}
}
- if (config->base_executable == NULL) {
+ if (config->base_executable == NULL && config->executable != NULL) {
if (copy_wstr(&config->base_executable,
config->executable) < 0) {
return _PyStatus_NO_MEMORY();
}
-static PyStatus
-pathconfig_global_read(_PyPathConfig *pathconfig)
-{
- PyConfig config;
- _PyConfig_InitCompatConfig(&config);
-
- /* Call _PyConfig_InitPathConfig() */
- PyStatus status = PyConfig_Read(&config);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- status = pathconfig_set_from_config(pathconfig, &config);
-
-done:
- PyConfig_Clear(&config);
- return status;
-}
-
-
-static void
-pathconfig_global_init(void)
-{
- PyStatus status;
-
- if (_Py_path_config.module_search_path == NULL) {
- status = pathconfig_global_read(&_Py_path_config);
- if (_PyStatus_EXCEPTION(status)) {
- Py_ExitStatusException(status);
- }
- }
- else {
- /* Global configuration already initialized */
- }
-
- assert(_Py_path_config.program_full_path != NULL);
- assert(_Py_path_config.prefix != NULL);
- assert(_Py_path_config.exec_prefix != NULL);
- assert(_Py_path_config.module_search_path != NULL);
- assert(_Py_path_config.program_name != NULL);
- /* home can be NULL */
-#ifdef MS_WINDOWS
- assert(_Py_path_config.base_executable != NULL);
-#endif
-}
-
-
/* External interface */
static void _Py_NO_RETURN
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
- /* Getting the program full path calls pathconfig_global_init() */
- wchar_t *program_full_path = _PyMem_RawWcsdup(Py_GetProgramFullPath());
-
- PyMem_RawFree(_Py_path_config.program_full_path);
PyMem_RawFree(_Py_path_config.prefix);
PyMem_RawFree(_Py_path_config.exec_prefix);
PyMem_RawFree(_Py_path_config.module_search_path);
- _Py_path_config.program_full_path = program_full_path;
_Py_path_config.prefix = _PyMem_RawWcsdup(L"");
_Py_path_config.exec_prefix = _PyMem_RawWcsdup(L"");
_Py_path_config.module_search_path = _PyMem_RawWcsdup(path);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
- if (_Py_path_config.program_full_path == NULL
- || _Py_path_config.prefix == NULL
+ if (_Py_path_config.prefix == NULL
|| _Py_path_config.exec_prefix == NULL
|| _Py_path_config.module_search_path == NULL)
{
wchar_t *
Py_GetPath(void)
{
- pathconfig_global_init();
return _Py_path_config.module_search_path;
}
wchar_t *
Py_GetPrefix(void)
{
- pathconfig_global_init();
return _Py_path_config.prefix;
}
wchar_t *
Py_GetExecPrefix(void)
{
- pathconfig_global_init();
return _Py_path_config.exec_prefix;
}
wchar_t *
Py_GetProgramFullPath(void)
{
- pathconfig_global_init();
return _Py_path_config.program_full_path;
}
wchar_t*
Py_GetPythonHome(void)
{
- pathconfig_global_init();
return _Py_path_config.home;
}
wchar_t *
Py_GetProgramName(void)
{
- pathconfig_global_init();
return _Py_path_config.program_name;
}
}
// Compute the path configuration
- status = _PyConfig_InitPathConfig(&interp->config);
+ status = _PyConfig_InitPathConfig(&interp->config, 1);
if (_PyStatus_EXCEPTION(status)) {
return status;
}