diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-05-27 14:39:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-27 14:39:22 (GMT) |
commit | 331a6a56e9a9c72f3e4605987fabdaec72677702 (patch) | |
tree | 49d20cedd9df4371f2410b2fb24255535ca02c50 /Include | |
parent | 8cd5165ba05ff57cfdbbc71c393bddad1ce1ab87 (diff) | |
download | cpython-331a6a56e9a9c72f3e4605987fabdaec72677702.zip cpython-331a6a56e9a9c72f3e4605987fabdaec72677702.tar.gz cpython-331a6a56e9a9c72f3e4605987fabdaec72677702.tar.bz2 |
bpo-36763: Implement the PEP 587 (GH-13592)
* Add a whole new documentation page:
"Python Initialization Configuration"
* PyWideStringList_Append() return type is now PyStatus,
instead of int
* PyInterpreterState_New() now calls PyConfig_Clear() if
PyConfig_InitPythonConfig() fails.
* Rename files:
* Python/coreconfig.c => Python/initconfig.c
* Include/cpython/coreconfig.h => Include/cpython/initconfig.h
* Include/internal/: pycore_coreconfig.h => pycore_initconfig.h
* Rename structures
* _PyCoreConfig => PyConfig
* _PyPreConfig => PyPreConfig
* _PyInitError => PyStatus
* _PyWstrList => PyWideStringList
* Rename PyConfig fields:
* use_module_search_paths => module_search_paths_set
* module_search_path_env => pythonpath_env
* Rename PyStatus field: _func => func
* PyInterpreterState: rename core_config field to config
* Rename macros and functions:
* _PyCoreConfig_SetArgv() => PyConfig_SetBytesArgv()
* _PyCoreConfig_SetWideArgv() => PyConfig_SetArgv()
* _PyCoreConfig_DecodeLocale() => PyConfig_SetBytesString()
* _PyInitError_Failed() => PyStatus_Exception()
* _Py_INIT_ERROR_TYPE_xxx enums => _PyStatus_TYPE_xxx
* _Py_UnixMain() => Py_BytesMain()
* _Py_ExitInitError() => Py_ExitStatusException()
* _Py_PreInitializeFromArgs() => Py_PreInitializeFromBytesArgs()
* _Py_PreInitializeFromWideArgs() => Py_PreInitializeFromArgs()
* _Py_PreInitialize() => Py_PreInitialize()
* _Py_RunMain() => Py_RunMain()
* _Py_InitializeFromConfig() => Py_InitializeFromConfig()
* _Py_INIT_XXX() => _PyStatus_XXX()
* _Py_INIT_FAILED() => _PyStatus_EXCEPTION()
* Rename 'err' PyStatus variables to 'status'
* Convert RUN_CODE() macro to config_run_code() static inline function
* Remove functions:
* _Py_InitializeFromArgs()
* _Py_InitializeFromWideArgs()
* _PyInterpreterState_GetCoreConfig()
Diffstat (limited to 'Include')
-rw-r--r-- | Include/Python.h | 2 | ||||
-rw-r--r-- | Include/cpython/initconfig.h (renamed from Include/cpython/coreconfig.h) | 139 | ||||
-rw-r--r-- | Include/cpython/pylifecycle.h | 30 | ||||
-rw-r--r-- | Include/cpython/pystate.h | 4 | ||||
-rw-r--r-- | Include/internal/pycore_coreconfig.h | 163 | ||||
-rw-r--r-- | Include/internal/pycore_initconfig.h | 168 | ||||
-rw-r--r-- | Include/internal/pycore_pathconfig.h | 20 | ||||
-rw-r--r-- | Include/internal/pycore_pylifecycle.h | 38 | ||||
-rw-r--r-- | Include/internal/pycore_pystate.h | 14 |
9 files changed, 287 insertions, 291 deletions
diff --git a/Include/Python.h b/Include/Python.h index 17ad5b3..d6e5b13 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -129,7 +129,7 @@ #include "codecs.h" #include "pyerrors.h" -#include "cpython/coreconfig.h" +#include "cpython/initconfig.h" #include "pystate.h" #include "context.h" diff --git a/Include/cpython/coreconfig.h b/Include/cpython/initconfig.h index ef5fde2..a98b91a 100644 --- a/Include/cpython/coreconfig.h +++ b/Include/cpython/initconfig.h @@ -5,56 +5,49 @@ extern "C" { #endif -/* --- _PyInitError ----------------------------------------------- */ +/* --- PyStatus ----------------------------------------------- */ typedef struct { enum { - _Py_INIT_ERR_TYPE_OK=0, - _Py_INIT_ERR_TYPE_ERROR=1, - _Py_INIT_ERR_TYPE_EXIT=2 + _PyStatus_TYPE_OK=0, + _PyStatus_TYPE_ERROR=1, + _PyStatus_TYPE_EXIT=2 } _type; - const char *_func; + const char *func; const char *err_msg; int exitcode; -} _PyInitError; +} PyStatus; -PyAPI_FUNC(_PyInitError) _PyInitError_Ok(void); -PyAPI_FUNC(_PyInitError) _PyInitError_Error(const char *err_msg); -PyAPI_FUNC(_PyInitError) _PyInitError_NoMemory(void); -PyAPI_FUNC(_PyInitError) _PyInitError_Exit(int exitcode); -PyAPI_FUNC(int) _PyInitError_IsError(_PyInitError err); -PyAPI_FUNC(int) _PyInitError_IsExit(_PyInitError err); -PyAPI_FUNC(int) _PyInitError_Failed(_PyInitError err); +PyAPI_FUNC(PyStatus) PyStatus_Ok(void); +PyAPI_FUNC(PyStatus) PyStatus_Error(const char *err_msg); +PyAPI_FUNC(PyStatus) PyStatus_NoMemory(void); +PyAPI_FUNC(PyStatus) PyStatus_Exit(int exitcode); +PyAPI_FUNC(int) PyStatus_IsError(PyStatus err); +PyAPI_FUNC(int) PyStatus_IsExit(PyStatus err); +PyAPI_FUNC(int) PyStatus_Exception(PyStatus err); -/* --- _PyWstrList ------------------------------------------------ */ +/* --- PyWideStringList ------------------------------------------------ */ typedef struct { /* If length is greater than zero, items must be non-NULL and all items strings must be non-NULL */ Py_ssize_t length; wchar_t **items; -} _PyWstrList; +} PyWideStringList; +PyAPI_FUNC(PyStatus) PyWideStringList_Append(PyWideStringList *list, + const wchar_t *item); -/* --- _PyPreConfig ----------------------------------------------- */ - -#define _Py_CONFIG_VERSION 1 - -typedef enum { - /* Py_Initialize() API: backward compatibility with Python 3.6 and 3.7 */ - _PyConfig_INIT_COMPAT = 1, - _PyConfig_INIT_PYTHON = 2, - _PyConfig_INIT_ISOLATED = 3 -} _PyConfigInitEnum; +/* --- PyPreConfig ----------------------------------------------- */ typedef struct { int _config_version; /* Internal configuration version, used for ABI compatibility */ int _config_init; /* _PyConfigInitEnum value */ - /* Parse _Py_PreInitializeFromArgs() arguments? - See _PyCoreConfig.parse_argv */ + /* Parse Py_PreInitializeFromBytesArgs() arguments? + See PyConfig.parse_argv */ int parse_argv; /* If greater than 0, enable isolated mode: sys.path contains @@ -124,22 +117,22 @@ typedef struct { /* Memory allocator: PYTHONMALLOC env var. See PyMemAllocatorName for valid values. */ int allocator; -} _PyPreConfig; +} PyPreConfig; -PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config); +PyAPI_FUNC(void) PyPreConfig_InitPythonConfig(PyPreConfig *config); +PyAPI_FUNC(void) PyPreConfig_InitIsolatedConfig(PyPreConfig *config); -/* --- _PyCoreConfig ---------------------------------------------- */ +/* --- PyConfig ---------------------------------------------- */ typedef struct { int _config_version; /* Internal configuration version, used for ABI compatibility */ int _config_init; /* _PyConfigInitEnum value */ - int isolated; /* Isolated mode? see _PyPreConfig.isolated */ - int use_environment; /* Use environment variables? see _PyPreConfig.use_environment */ - int dev_mode; /* Development mode? See _PyPreConfig.dev_mode */ + int isolated; /* Isolated mode? see PyPreConfig.isolated */ + int use_environment; /* Use environment variables? see PyPreConfig.use_environment */ + int dev_mode; /* Development mode? See PyPreConfig.dev_mode */ /* Install signal handlers? Yes by default. */ int install_signal_handlers; @@ -207,7 +200,7 @@ typedef struct { If argv is empty, an empty string is added to ensure that sys.argv always exists and is never empty. */ - _PyWstrList argv; + PyWideStringList argv; /* Program name: @@ -219,8 +212,8 @@ typedef struct { - Use "python" on Windows, or "python3 on other platforms. */ wchar_t *program_name; - _PyWstrList xoptions; /* Command line -X options */ - _PyWstrList warnoptions; /* Warnings options */ + PyWideStringList xoptions; /* Command line -X options */ + PyWideStringList warnoptions; /* Warnings options */ /* If equal to zero, disable the import of the module site and the site-dependent manipulations of sys.path that it entails. Also disable @@ -347,17 +340,37 @@ typedef struct { int legacy_windows_stdio; #endif + /* Value of the --check-hash-based-pycs command line option: + + - "default" means the 'check_source' flag in hash-based pycs + determines invalidation + - "always" causes the interpreter to hash the source file for + invalidation regardless of value of 'check_source' bit + - "never" causes the interpreter to always assume hash-based pycs are + valid + + The default value is "default". + + See PEP 552 "Deterministic pycs" for more details. */ + wchar_t *check_hash_pycs_mode; + /* --- Path configuration inputs ------------ */ - wchar_t *module_search_path_env; /* PYTHONPATH environment variable */ + /* If greater than 0, suppress _PyPathConfig_Calculate() warnings on Unix. + The parameter has no effect on Windows. + + If set to -1 (default), inherit !Py_FrozenFlag value. */ + int pathconfig_warnings; + + wchar_t *pythonpath_env; /* PYTHONPATH environment variable */ wchar_t *home; /* PYTHONHOME environment variable, see also Py_SetPythonHome(). */ /* --- Path configuration outputs ----------- */ - int use_module_search_paths; /* If non-zero, use module_search_paths */ - _PyWstrList module_search_paths; /* sys.path paths. Computed if - use_module_search_paths is equal + int module_search_paths_set; /* If non-zero, use module_search_paths */ + PyWideStringList module_search_paths; /* sys.path paths. Computed if + module_search_paths_set is equal to zero. */ wchar_t *executable; /* sys.executable */ @@ -384,48 +397,28 @@ typedef struct { Needed by freeze_importlib. */ int _install_importlib; - /* Value of the --check-hash-based-pycs command line option: - - - "default" means the 'check_source' flag in hash-based pycs - determines invalidation - - "always" causes the interpreter to hash the source file for - invalidation regardless of value of 'check_source' bit - - "never" causes the interpreter to always assume hash-based pycs are - valid - - The default value is "default". - - See PEP 552 "Deterministic pycs" for more details. */ - wchar_t *check_hash_pycs_mode; - - /* If greater than 0, suppress _PyPathConfig_Calculate() warnings on Unix. - The parameter has no effect on Windows. - - If set to -1 (default), inherit !Py_FrozenFlag value. */ - int pathconfig_warnings; - /* If equal to 0, stop Python initialization before the "main" phase */ int _init_main; -} _PyCoreConfig; +} PyConfig; -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config); -PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString( - _PyCoreConfig *config, +PyAPI_FUNC(PyStatus) PyConfig_InitPythonConfig(PyConfig *config); +PyAPI_FUNC(PyStatus) PyConfig_InitIsolatedConfig(PyConfig *config); +PyAPI_FUNC(void) PyConfig_Clear(PyConfig *); +PyAPI_FUNC(PyStatus) PyConfig_SetString( + PyConfig *config, wchar_t **config_str, const wchar_t *str); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale( - _PyCoreConfig *config, +PyAPI_FUNC(PyStatus) PyConfig_SetBytesString( + PyConfig *config, wchar_t **config_str, const char *str); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetArgv( - _PyCoreConfig *config, +PyAPI_FUNC(PyStatus) PyConfig_Read(PyConfig *config); +PyAPI_FUNC(PyStatus) PyConfig_SetBytesArgv( + PyConfig *config, Py_ssize_t argc, char * const *argv); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config, +PyAPI_FUNC(PyStatus) PyConfig_SetArgv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv); diff --git a/Include/cpython/pylifecycle.h b/Include/cpython/pylifecycle.h index ba56664..2f3a0db 100644 --- a/Include/cpython/pylifecycle.h +++ b/Include/cpython/pylifecycle.h @@ -14,14 +14,14 @@ PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding, /* PEP 432 Multi-phase initialization API (Private while provisional!) */ -PyAPI_FUNC(_PyInitError) _Py_PreInitialize( - const _PyPreConfig *src_config); -PyAPI_FUNC(_PyInitError) _Py_PreInitializeFromArgs( - const _PyPreConfig *src_config, +PyAPI_FUNC(PyStatus) Py_PreInitialize( + const PyPreConfig *src_config); +PyAPI_FUNC(PyStatus) Py_PreInitializeFromBytesArgs( + const PyPreConfig *src_config, Py_ssize_t argc, char **argv); -PyAPI_FUNC(_PyInitError) _Py_PreInitializeFromWideArgs( - const _PyPreConfig *src_config, +PyAPI_FUNC(PyStatus) Py_PreInitializeFromArgs( + const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv); @@ -30,22 +30,22 @@ PyAPI_FUNC(int) _Py_IsCoreInitialized(void); /* Initialization and finalization */ -PyAPI_FUNC(_PyInitError) _Py_InitializeFromConfig( - const _PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _Py_InitializeFromArgs( - const _PyCoreConfig *config, +PyAPI_FUNC(PyStatus) Py_InitializeFromConfig( + const PyConfig *config); +PyAPI_FUNC(PyStatus) _Py_InitializeFromArgs( + const PyConfig *config, Py_ssize_t argc, char * const *argv); -PyAPI_FUNC(_PyInitError) _Py_InitializeFromWideArgs( - const _PyCoreConfig *config, +PyAPI_FUNC(PyStatus) _Py_InitializeFromWideArgs( + const PyConfig *config, Py_ssize_t argc, wchar_t * const *argv); -PyAPI_FUNC(_PyInitError) _Py_InitializeMain(void); +PyAPI_FUNC(PyStatus) _Py_InitializeMain(void); -PyAPI_FUNC(int) _Py_RunMain(void); +PyAPI_FUNC(int) Py_RunMain(void); -PyAPI_FUNC(void) _Py_NO_RETURN _Py_ExitInitError(_PyInitError err); +PyAPI_FUNC(void) _Py_NO_RETURN Py_ExitStatusException(PyStatus err); /* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level * exit functions. diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index 94331f3..6b7663f 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -6,13 +6,11 @@ extern "C" { #endif -#include "cpython/coreconfig.h" +#include "cpython/initconfig.h" PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *); PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int); -PyAPI_FUNC(_PyCoreConfig *) _PyInterpreterState_GetCoreConfig(PyInterpreterState *); - PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *); /* State unique per thread */ diff --git a/Include/internal/pycore_coreconfig.h b/Include/internal/pycore_coreconfig.h deleted file mode 100644 index b17737a..0000000 --- a/Include/internal/pycore_coreconfig.h +++ /dev/null @@ -1,163 +0,0 @@ -#ifndef Py_INTERNAL_CORECONFIG_H -#define Py_INTERNAL_CORECONFIG_H -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef Py_BUILD_CORE -# error "this header requires Py_BUILD_CORE define" -#endif - -#include "pycore_pystate.h" /* _PyRuntimeState */ - -/* --- _PyInitError ----------------------------------------------- */ - -/* Almost all errors causing Python initialization to fail */ -#ifdef _MSC_VER - /* Visual Studio 2015 doesn't implement C99 __func__ in C */ -# define _Py_INIT_GET_FUNC() __FUNCTION__ -#else -# define _Py_INIT_GET_FUNC() __func__ -#endif - -#define _Py_INIT_OK() \ - (_PyInitError){._type = _Py_INIT_ERR_TYPE_OK,} - /* other fields are set to 0 */ -#define _Py_INIT_ERR(ERR_MSG) \ - (_PyInitError){ \ - ._type = _Py_INIT_ERR_TYPE_ERROR, \ - ._func = _Py_INIT_GET_FUNC(), \ - .err_msg = (ERR_MSG)} - /* other fields are set to 0 */ -#define _Py_INIT_NO_MEMORY() _Py_INIT_ERR("memory allocation failed") -#define _Py_INIT_EXIT(EXITCODE) \ - (_PyInitError){ \ - ._type = _Py_INIT_ERR_TYPE_EXIT, \ - .exitcode = (EXITCODE)} -#define _Py_INIT_IS_ERROR(err) \ - (err._type == _Py_INIT_ERR_TYPE_ERROR) -#define _Py_INIT_IS_EXIT(err) \ - (err._type == _Py_INIT_ERR_TYPE_EXIT) -#define _Py_INIT_FAILED(err) \ - (err._type != _Py_INIT_ERR_TYPE_OK) - -/* --- _PyWstrList ------------------------------------------------ */ - -#define _PyWstrList_INIT (_PyWstrList){.length = 0, .items = NULL} - -#ifndef NDEBUG -PyAPI_FUNC(int) _PyWstrList_CheckConsistency(const _PyWstrList *list); -#endif -PyAPI_FUNC(void) _PyWstrList_Clear(_PyWstrList *list); -PyAPI_FUNC(int) _PyWstrList_Copy(_PyWstrList *list, - const _PyWstrList *list2); -PyAPI_FUNC(int) _PyWstrList_Append(_PyWstrList *list, - const wchar_t *item); -PyAPI_FUNC(PyObject*) _PyWstrList_AsList(const _PyWstrList *list); -PyAPI_FUNC(int) _PyWstrList_Extend(_PyWstrList *list, - const _PyWstrList *list2); - - -/* --- _PyArgv ---------------------------------------------------- */ - -typedef struct { - Py_ssize_t argc; - int use_bytes_argv; - char * const *bytes_argv; - wchar_t * const *wchar_argv; -} _PyArgv; - -PyAPI_FUNC(_PyInitError) _PyArgv_AsWstrList(const _PyArgv *args, - _PyWstrList *list); - - -/* --- Helper functions ------------------------------------------- */ - -PyAPI_FUNC(int) _Py_str_to_int( - const char *str, - int *result); -PyAPI_FUNC(const wchar_t*) _Py_get_xoption( - const _PyWstrList *xoptions, - const wchar_t *name); -PyAPI_FUNC(const char*) _Py_GetEnv( - int use_environment, - const char *name); -PyAPI_FUNC(void) _Py_get_env_flag( - int use_environment, - int *flag, - const char *name); - -/* Py_GetArgcArgv() helper */ -PyAPI_FUNC(void) _Py_ClearArgcArgv(void); - - -/* --- _PyPreCmdline ------------------------------------------------- */ - -typedef struct { - _PyWstrList argv; - _PyWstrList xoptions; /* "-X value" option */ - int isolated; /* -I option */ - int use_environment; /* -E option */ - int dev_mode; /* -X dev and PYTHONDEVMODE */ -} _PyPreCmdline; - -#define _PyPreCmdline_INIT \ - (_PyPreCmdline){ \ - .use_environment = -1, \ - .isolated = -1, \ - .dev_mode = -1} -/* Note: _PyPreCmdline_INIT sets other fields to 0/NULL */ - -PyAPI_FUNC(void) _PyPreCmdline_Clear(_PyPreCmdline *cmdline); -PyAPI_FUNC(_PyInitError) _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, - const _PyArgv *args); -PyAPI_FUNC(int) _PyPreCmdline_SetCoreConfig( - const _PyPreCmdline *cmdline, - _PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline, - const _PyPreConfig *preconfig); - - -/* --- _PyPreConfig ----------------------------------------------- */ - -PyAPI_FUNC(void) _PyPreConfig_InitCompatConfig(_PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_InitFromCoreConfig( - _PyPreConfig *config, - const _PyCoreConfig *coreconfig); -PyAPI_FUNC(void) _PyPreConfig_InitFromPreConfig( - _PyPreConfig *config, - const _PyPreConfig *config2); -PyAPI_FUNC(void) _PyPreConfig_Copy(_PyPreConfig *config, - const _PyPreConfig *config2); -PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_GetCoreConfig(_PyPreConfig *config, - const _PyCoreConfig *core_config); -PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config, - const _PyArgv *args); -PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config); - - -/* --- _PyCoreConfig ---------------------------------------------- */ - -PyAPI_FUNC(void) _PyCoreConfig_InitCompatConfig(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_Copy( - _PyCoreConfig *config, - const _PyCoreConfig *config2); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig( - const _PyCoreConfig *config); -PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config, - _PyRuntimeState *runtime); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPyArgv( - _PyCoreConfig *config, - const _PyArgv *args); - - -/* --- Function used for testing ---------------------------------- */ - -PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void); - -#ifdef __cplusplus -} -#endif -#endif /* !Py_INTERNAL_CORECONFIG_H */ diff --git a/Include/internal/pycore_initconfig.h b/Include/internal/pycore_initconfig.h new file mode 100644 index 0000000..c0b3d95 --- /dev/null +++ b/Include/internal/pycore_initconfig.h @@ -0,0 +1,168 @@ +#ifndef Py_INTERNAL_CORECONFIG_H +#define Py_INTERNAL_CORECONFIG_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + +#include "pycore_pystate.h" /* _PyRuntimeState */ + +/* --- PyStatus ----------------------------------------------- */ + +/* Almost all errors causing Python initialization to fail */ +#ifdef _MSC_VER + /* Visual Studio 2015 doesn't implement C99 __func__ in C */ +# define _PyStatus_GET_FUNC() __FUNCTION__ +#else +# define _PyStatus_GET_FUNC() __func__ +#endif + +#define _PyStatus_OK() \ + (PyStatus){._type = _PyStatus_TYPE_OK,} + /* other fields are set to 0 */ +#define _PyStatus_ERR(ERR_MSG) \ + (PyStatus){ \ + ._type = _PyStatus_TYPE_ERROR, \ + .func = _PyStatus_GET_FUNC(), \ + .err_msg = (ERR_MSG)} + /* other fields are set to 0 */ +#define _PyStatus_NO_MEMORY() _PyStatus_ERR("memory allocation failed") +#define _PyStatus_EXIT(EXITCODE) \ + (PyStatus){ \ + ._type = _PyStatus_TYPE_EXIT, \ + .exitcode = (EXITCODE)} +#define _PyStatus_IS_ERROR(err) \ + (err._type == _PyStatus_TYPE_ERROR) +#define _PyStatus_IS_EXIT(err) \ + (err._type == _PyStatus_TYPE_EXIT) +#define _PyStatus_EXCEPTION(err) \ + (err._type != _PyStatus_TYPE_OK) + +/* --- PyWideStringList ------------------------------------------------ */ + +#define PyWideStringList_INIT (PyWideStringList){.length = 0, .items = NULL} + +#ifndef NDEBUG +PyAPI_FUNC(int) _PyWideStringList_CheckConsistency(const PyWideStringList *list); +#endif +PyAPI_FUNC(void) _PyWideStringList_Clear(PyWideStringList *list); +PyAPI_FUNC(int) _PyWideStringList_Copy(PyWideStringList *list, + const PyWideStringList *list2); +PyAPI_FUNC(PyStatus) _PyWideStringList_Extend(PyWideStringList *list, + const PyWideStringList *list2); +PyAPI_FUNC(PyObject*) _PyWideStringList_AsList(const PyWideStringList *list); + + +/* --- _PyArgv ---------------------------------------------------- */ + +typedef struct { + Py_ssize_t argc; + int use_bytes_argv; + char * const *bytes_argv; + wchar_t * const *wchar_argv; +} _PyArgv; + +PyAPI_FUNC(PyStatus) _PyArgv_AsWstrList(const _PyArgv *args, + PyWideStringList *list); + + +/* --- Helper functions ------------------------------------------- */ + +PyAPI_FUNC(int) _Py_str_to_int( + const char *str, + int *result); +PyAPI_FUNC(const wchar_t*) _Py_get_xoption( + const PyWideStringList *xoptions, + const wchar_t *name); +PyAPI_FUNC(const char*) _Py_GetEnv( + int use_environment, + const char *name); +PyAPI_FUNC(void) _Py_get_env_flag( + int use_environment, + int *flag, + const char *name); + +/* Py_GetArgcArgv() helper */ +PyAPI_FUNC(void) _Py_ClearArgcArgv(void); + + +/* --- _PyPreCmdline ------------------------------------------------- */ + +typedef struct { + PyWideStringList argv; + PyWideStringList xoptions; /* "-X value" option */ + int isolated; /* -I option */ + int use_environment; /* -E option */ + int dev_mode; /* -X dev and PYTHONDEVMODE */ +} _PyPreCmdline; + +#define _PyPreCmdline_INIT \ + (_PyPreCmdline){ \ + .use_environment = -1, \ + .isolated = -1, \ + .dev_mode = -1} +/* Note: _PyPreCmdline_INIT sets other fields to 0/NULL */ + +extern void _PyPreCmdline_Clear(_PyPreCmdline *cmdline); +extern PyStatus _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, + const _PyArgv *args); +extern PyStatus _PyPreCmdline_SetConfig( + const _PyPreCmdline *cmdline, + PyConfig *config); +extern PyStatus _PyPreCmdline_Read(_PyPreCmdline *cmdline, + const PyPreConfig *preconfig); + + +/* --- PyPreConfig ----------------------------------------------- */ + +PyAPI_FUNC(void) _PyPreConfig_InitCompatConfig(PyPreConfig *preconfig); +extern void _PyPreConfig_InitFromConfig( + PyPreConfig *preconfig, + const PyConfig *config); +extern void _PyPreConfig_InitFromPreConfig( + PyPreConfig *preconfig, + const PyPreConfig *config2); +extern PyObject* _PyPreConfig_AsDict(const PyPreConfig *preconfig); +extern void _PyPreConfig_GetConfig(PyPreConfig *preconfig, + const PyConfig *config); +extern PyStatus _PyPreConfig_Read(PyPreConfig *preconfig, + const _PyArgv *args); +extern PyStatus _PyPreConfig_Write(const PyPreConfig *preconfig); + + +/* --- PyConfig ---------------------------------------------- */ + +#define _Py_CONFIG_VERSION 1 + +typedef enum { + /* Py_Initialize() API: backward compatibility with Python 3.6 and 3.7 */ + _PyConfig_INIT_COMPAT = 1, + _PyConfig_INIT_PYTHON = 2, + _PyConfig_INIT_ISOLATED = 3 +} _PyConfigInitEnum; + +PyAPI_FUNC(void) _PyConfig_InitCompatConfig(PyConfig *config); +extern PyStatus _PyConfig_Copy( + PyConfig *config, + const PyConfig *config2); +extern PyStatus _PyConfig_InitPathConfig(PyConfig *config); +extern PyStatus _PyConfig_SetPathConfig( + const PyConfig *config); +extern void _PyConfig_Write(const PyConfig *config, + _PyRuntimeState *runtime); +extern PyStatus _PyConfig_SetPyArgv( + PyConfig *config, + const _PyArgv *args); + + +/* --- Function used for testing ---------------------------------- */ + +PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void); + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_CORECONFIG_H */ diff --git a/Include/internal/pycore_pathconfig.h b/Include/internal/pycore_pathconfig.h index bee3911..be12c6f 100644 --- a/Include/internal/pycore_pathconfig.h +++ b/Include/internal/pycore_pathconfig.h @@ -37,17 +37,17 @@ typedef struct _PyPathConfig { PyAPI_DATA(_PyPathConfig) _Py_path_config; -PyAPI_FUNC(void) _PyPathConfig_ClearGlobal(void); -PyAPI_FUNC(_PyInitError) _PyPathConfig_SetGlobal( - const struct _PyPathConfig *config); - -PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl( - _PyPathConfig *config, - const _PyCoreConfig *core_config); -PyAPI_FUNC(int) _PyPathConfig_ComputeSysPath0( - const _PyWstrList *argv, +extern void _PyPathConfig_ClearGlobal(void); +extern PyStatus _PyPathConfig_SetGlobal( + const struct _PyPathConfig *pathconfig); + +extern PyStatus _PyPathConfig_Calculate( + _PyPathConfig *pathconfig, + const PyConfig *config); +extern int _PyPathConfig_ComputeSysPath0( + const PyWideStringList *argv, PyObject **path0); -PyAPI_FUNC(int) _Py_FindEnvConfigValue( +extern int _Py_FindEnvConfigValue( FILE *env_file, const wchar_t *key, wchar_t *value, diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h index 13a31c2..69761ce 100644 --- a/Include/internal/pycore_pylifecycle.h +++ b/Include/internal/pycore_pylifecycle.h @@ -8,20 +8,20 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "pycore_coreconfig.h" /* _PyArgv */ +#include "pycore_initconfig.h" /* _PyArgv */ #include "pycore_pystate.h" /* _PyRuntimeState */ /* True if the main interpreter thread exited due to an unhandled * KeyboardInterrupt exception, suggesting the user pressed ^C. */ PyAPI_DATA(int) _Py_UnhandledKeyboardInterrupt; -PyAPI_FUNC(int) _Py_UnixMain(int argc, char **argv); +PyAPI_FUNC(int) Py_BytesMain(int argc, char **argv); extern int _Py_SetFileSystemEncoding( const char *encoding, const char *errors); extern void _Py_ClearFileSystemEncoding(void); -extern _PyInitError _PyUnicode_InitEncodings(PyInterpreterState *interp); +extern PyStatus _PyUnicode_InitEncodings(PyInterpreterState *interp); #ifdef MS_WINDOWS extern int _PyUnicode_EnableLegacyWindowsFSEncoding(void); #endif @@ -32,30 +32,30 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc); /* Various one-time initializers */ -extern _PyInitError _PyUnicode_Init(void); +extern PyStatus _PyUnicode_Init(void); extern int _PyStructSequence_Init(void); extern int _PyLong_Init(void); -extern _PyInitError _PyFaulthandler_Init(int enable); +extern PyStatus _PyFaulthandler_Init(int enable); extern int _PyTraceMalloc_Init(int enable); extern PyObject * _PyBuiltin_Init(void); -extern _PyInitError _PySys_Create( +extern PyStatus _PySys_Create( _PyRuntimeState *runtime, PyInterpreterState *interp, PyObject **sysmod_p); -extern _PyInitError _PySys_SetPreliminaryStderr(PyObject *sysdict); +extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict); extern int _PySys_InitMain( _PyRuntimeState *runtime, PyInterpreterState *interp); -extern _PyInitError _PyImport_Init(PyInterpreterState *interp); -extern _PyInitError _PyExc_Init(void); -extern _PyInitError _PyErr_Init(void); -extern _PyInitError _PyBuiltins_AddExceptions(PyObject * bltinmod); -extern _PyInitError _PyImportHooks_Init(void); +extern PyStatus _PyImport_Init(PyInterpreterState *interp); +extern PyStatus _PyExc_Init(void); +extern PyStatus _PyErr_Init(void); +extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod); +extern PyStatus _PyImportHooks_Init(void); extern int _PyFloat_Init(void); -extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *); +extern PyStatus _Py_HashRandomization_Init(const PyConfig *); -extern _PyInitError _PyTypes_Init(void); -extern _PyInitError _PyImportZip_Init(PyInterpreterState *interp); +extern PyStatus _PyTypes_Init(void); +extern PyStatus _PyImportZip_Init(PyInterpreterState *interp); /* Various internal finalizers */ @@ -94,11 +94,11 @@ extern void _PyGILState_Fini(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyGC_DumpShutdownStats(_PyRuntimeState *runtime); -PyAPI_FUNC(_PyInitError) _Py_PreInitializeFromPyArgv( - const _PyPreConfig *src_config, +PyAPI_FUNC(PyStatus) _Py_PreInitializeFromPyArgv( + const PyPreConfig *src_config, const _PyArgv *args); -PyAPI_FUNC(_PyInitError) _Py_PreInitializeFromCoreConfig( - const _PyCoreConfig *coreconfig, +PyAPI_FUNC(PyStatus) _Py_PreInitializeFromConfig( + const PyConfig *config, const _PyArgv *args); diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index ef1d8a0..3ab4009 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -8,7 +8,7 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "cpython/coreconfig.h" +#include "cpython/initconfig.h" #include "fileobject.h" #include "pystate.h" #include "pythread.h" @@ -106,7 +106,7 @@ struct _is { _Py_error_handler error_handler; } fs_codec; - _PyCoreConfig core_config; + PyConfig config; #ifdef HAVE_DLOPEN int dlopenflags; #endif @@ -193,7 +193,7 @@ struct _gilstate_runtime_state { /* Full Python runtime state */ typedef struct pyruntimestate { - /* Is Python pre-initialized? Set to 1 by _Py_PreInitialize() */ + /* Is Python pre-initialized? Set to 1 by Py_PreInitialize() */ int pre_initialized; /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */ @@ -234,7 +234,7 @@ typedef struct pyruntimestate { struct _ceval_runtime_state ceval; struct _gilstate_runtime_state gilstate; - _PyPreConfig preconfig; + PyPreConfig preconfig; Py_OpenCodeHookFunction open_code_hook; void *open_code_userdata; @@ -248,13 +248,13 @@ typedef struct pyruntimestate { /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ PyAPI_DATA(_PyRuntimeState) _PyRuntime; -PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *runtime); +PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime); /* Initialize _PyRuntimeState. Return NULL on success, or return an error message on failure. */ -PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void); +PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void); PyAPI_FUNC(void) _PyRuntime_Finalize(void); @@ -307,7 +307,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap( struct _gilstate_runtime_state *gilstate, PyThreadState *newts); -PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *runtime); +PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime); |