Commit 871ff77c authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36763: Add _PyInitError functions (GH-13395)

* Add _PyInitError functions:

  * _PyInitError_Ok()
  * _PyInitError_Error()
  * _PyInitError_NoMemory()
  * _PyInitError_Exit()
  * _PyInitError_IsError()
  * _PyInitError_IsExit()
  * _PyInitError_Failed()

* frozenmain.c and _testembed.c now use functions rather than macros.
* Move _Py_INIT_xxx() macros to the internal API.
* Move _PyWstrList_INIT macro to the internal API.
parent 12083284
...@@ -18,34 +18,13 @@ typedef struct { ...@@ -18,34 +18,13 @@ typedef struct {
int exitcode; int exitcode;
} _PyInitError; } _PyInitError;
/* Almost all errors causing Python initialization to fail */ PyAPI_FUNC(_PyInitError) _PyInitError_Ok(void);
#ifdef _MSC_VER PyAPI_FUNC(_PyInitError) _PyInitError_Error(const char *err_msg);
/* Visual Studio 2015 doesn't implement C99 __func__ in C */ PyAPI_FUNC(_PyInitError) _PyInitError_NoMemory(void);
# define _Py_INIT_GET_FUNC() __FUNCTION__ PyAPI_FUNC(_PyInitError) _PyInitError_Exit(int exitcode);
#else PyAPI_FUNC(int) _PyInitError_IsError(_PyInitError err);
# define _Py_INIT_GET_FUNC() __func__ PyAPI_FUNC(int) _PyInitError_IsExit(_PyInitError err);
#endif PyAPI_FUNC(int) _PyInitError_Failed(_PyInitError err);
#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 ------------------------------------------------ */ /* --- _PyWstrList ------------------------------------------------ */
...@@ -56,8 +35,6 @@ typedef struct { ...@@ -56,8 +35,6 @@ typedef struct {
wchar_t **items; wchar_t **items;
} _PyWstrList; } _PyWstrList;
#define _PyWstrList_INIT (_PyWstrList){.length = 0, .items = NULL}
/* --- _PyPreConfig ----------------------------------------------- */ /* --- _PyPreConfig ----------------------------------------------- */
......
...@@ -10,9 +10,41 @@ extern "C" { ...@@ -10,9 +10,41 @@ extern "C" {
#include "pycore_pystate.h" /* _PyRuntimeState */ #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 ------------------------------------------------ */ /* --- _PyWstrList ------------------------------------------------ */
#define _PyWstrList_INIT (_PyWstrList){.length = 0, .items = NULL}
#ifndef NDEBUG #ifndef NDEBUG
PyAPI_FUNC(int) _PyWstrList_CheckConsistency(const _PyWstrList *list); PyAPI_FUNC(int) _PyWstrList_CheckConsistency(const _PyWstrList *list);
#endif #endif
......
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h"
#include "pythread.h" #include "pythread.h"
#include <signal.h> #include <signal.h>
#include <object.h> #include <object.h>
......
/* Return the initial module search path. */ /* Return the initial module search path. */
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h"
#include "osdefs.h" #include "osdefs.h"
#include "pycore_fileutils.h" #include "pycore_fileutils.h"
#include "pycore_pathconfig.h" #include "pycore_pathconfig.h"
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define PY_SSIZE_T_CLEAN #define PY_SSIZE_T_CLEAN
#include <Python.h> #include <Python.h>
#include "pycore_coreconfig.h"
#include "pycore_object.h" #include "pycore_object.h"
#include "pycore_pymem.h" #include "pycore_pymem.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* Generic object operations; and implementation of None */ /* Generic object operations; and implementation of None */
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h"
#include "pycore_object.h" #include "pycore_object.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "pycore_context.h" #include "pycore_context.h"
......
...@@ -80,6 +80,7 @@ ...@@ -80,6 +80,7 @@
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "osdefs.h" #include "osdefs.h"
#include <wchar.h> #include <wchar.h>
......
...@@ -90,7 +90,7 @@ main(int argc, char *argv[]) ...@@ -90,7 +90,7 @@ main(int argc, char *argv[])
_PyInitError err = _Py_InitializeFromConfig(&config); _PyInitError err = _Py_InitializeFromConfig(&config);
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any /* No need to call _PyCoreConfig_Clear() since we didn't allocate any
memory: program_name is a constant string. */ memory: program_name is a constant string. */
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
......
...@@ -389,7 +389,7 @@ static int test_init_from_config(void) ...@@ -389,7 +389,7 @@ static int test_init_from_config(void)
preconfig.utf8_mode = 1; preconfig.utf8_mode = 1;
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -512,7 +512,7 @@ static int test_init_from_config(void) ...@@ -512,7 +512,7 @@ static int test_init_from_config(void)
config.pathconfig_warnings = 0; config.pathconfig_warnings = 0;
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -543,7 +543,7 @@ static int test_init_parse_argv(int parse_argv) ...@@ -543,7 +543,7 @@ static int test_init_parse_argv(int parse_argv)
config.parse_argv = parse_argv; config.parse_argv = parse_argv;
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -653,7 +653,7 @@ static int init_isolated_flag(void) ...@@ -653,7 +653,7 @@ static int init_isolated_flag(void)
test_init_env_dev_mode_putenvs(); test_init_env_dev_mode_putenvs();
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -672,7 +672,7 @@ static int test_preinit_isolated1(void) ...@@ -672,7 +672,7 @@ static int test_preinit_isolated1(void)
preconfig.isolated = 1; preconfig.isolated = 1;
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -682,7 +682,7 @@ static int test_preinit_isolated1(void) ...@@ -682,7 +682,7 @@ static int test_preinit_isolated1(void)
test_init_env_dev_mode_putenvs(); test_init_env_dev_mode_putenvs();
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -701,7 +701,7 @@ static int test_preinit_isolated2(void) ...@@ -701,7 +701,7 @@ static int test_preinit_isolated2(void)
preconfig.isolated = 0; preconfig.isolated = 0;
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -717,7 +717,7 @@ static int test_preinit_isolated2(void) ...@@ -717,7 +717,7 @@ static int test_preinit_isolated2(void)
test_init_env_dev_mode_putenvs(); test_init_env_dev_mode_putenvs();
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -734,7 +734,7 @@ static int init_isolated_config(void) ...@@ -734,7 +734,7 @@ static int init_isolated_config(void)
_PyPreConfig_InitIsolatedConfig(&preconfig); _PyPreConfig_InitIsolatedConfig(&preconfig);
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -744,13 +744,13 @@ static int init_isolated_config(void) ...@@ -744,13 +744,13 @@ static int init_isolated_config(void)
_PyCoreConfig config; _PyCoreConfig config;
err = _PyCoreConfig_InitIsolatedConfig(&config); err = _PyCoreConfig_InitIsolatedConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
config.program_name = L"./_testembed"; config.program_name = L"./_testembed";
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -767,19 +767,19 @@ static int init_python_config(void) ...@@ -767,19 +767,19 @@ static int init_python_config(void)
_PyPreConfig_InitPythonConfig(&preconfig); _PyPreConfig_InitPythonConfig(&preconfig);
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
_PyCoreConfig config; _PyCoreConfig config;
err = _PyCoreConfig_InitPythonConfig(&config); err = _PyCoreConfig_InitPythonConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
config.program_name = L"./_testembed"; config.program_name = L"./_testembed";
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -798,14 +798,14 @@ static int init_dont_configure_locale(void) ...@@ -798,14 +798,14 @@ static int init_dont_configure_locale(void)
preconfig.coerce_c_locale_warn = 1; preconfig.coerce_c_locale_warn = 1;
err = _Py_PreInitialize(&preconfig); err = _Py_PreInitialize(&preconfig);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
_PyCoreConfig config = _PyCoreConfig_INIT; _PyCoreConfig config = _PyCoreConfig_INIT;
config.program_name = L"./_testembed"; config.program_name = L"./_testembed";
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -824,7 +824,7 @@ static int init_dev_mode(void) ...@@ -824,7 +824,7 @@ static int init_dev_mode(void)
config.dev_mode = 1; config.dev_mode = 1;
config.program_name = L"./_testembed"; config.program_name = L"./_testembed";
_PyInitError err = _Py_InitializeFromConfig(&config); _PyInitError err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
dump_config(); dump_config();
...@@ -840,30 +840,30 @@ static int test_init_read_set(void) ...@@ -840,30 +840,30 @@ static int test_init_read_set(void)
_PyCoreConfig_Init(&config); _PyCoreConfig_Init(&config);
err = _PyCoreConfig_DecodeLocale(&config.program_name, "./init_read_set"); err = _PyCoreConfig_DecodeLocale(&config.program_name, "./init_read_set");
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
goto fail; goto fail;
} }
err = _PyCoreConfig_Read(&config); err = _PyCoreConfig_Read(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
goto fail; goto fail;
} }
if (_PyWstrList_Append(&config.module_search_paths, if (_PyWstrList_Append(&config.module_search_paths,
L"init_read_set_path") < 0) { L"init_read_set_path") < 0) {
err = _Py_INIT_NO_MEMORY(); err = _PyInitError_NoMemory();
goto fail; goto fail;
} }
/* override executable computed by _PyCoreConfig_Read() */ /* override executable computed by _PyCoreConfig_Read() */
err = _PyCoreConfig_SetString(&config.executable, L"my_executable"); err = _PyCoreConfig_SetString(&config.executable, L"my_executable");
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
goto fail; goto fail;
} }
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
_PyCoreConfig_Clear(&config); _PyCoreConfig_Clear(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
goto fail; goto fail;
} }
dump_config(); dump_config();
...@@ -898,7 +898,7 @@ static int test_init_run_main(void) ...@@ -898,7 +898,7 @@ static int test_init_run_main(void)
configure_init_main(&config); configure_init_main(&config);
_PyInitError err = _Py_InitializeFromConfig(&config); _PyInitError err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -914,7 +914,7 @@ static int test_init_main(void) ...@@ -914,7 +914,7 @@ static int test_init_main(void)
config._init_main = 0; config._init_main = 0;
_PyInitError err = _Py_InitializeFromConfig(&config); _PyInitError err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -928,7 +928,7 @@ static int test_init_main(void) ...@@ -928,7 +928,7 @@ static int test_init_main(void)
} }
err = _Py_InitializeMain(); err = _Py_InitializeMain();
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -951,7 +951,7 @@ static int test_run_main(void) ...@@ -951,7 +951,7 @@ static int test_run_main(void)
config.program_name = L"./python3"; config.program_name = L"./python3";
_PyInitError err = _Py_InitializeFromConfig(&config); _PyInitError err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
......
#include "Python.h" #include "Python.h"
#include "pycore_coreconfig.h"
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
# include <windows.h> # include <windows.h>
/* All sample MSDN wincrypt programs include the header below. It is at least /* All sample MSDN wincrypt programs include the header below. It is at least
......
...@@ -203,6 +203,34 @@ fail: ...@@ -203,6 +203,34 @@ fail:
} }
/* --- _PyInitError ----------------------------------------------- */
_PyInitError _PyInitError_Ok(void)
{ return _Py_INIT_OK(); }
_PyInitError _PyInitError_Error(const char *err_msg)
{
return (_PyInitError){._type = _Py_INIT_ERR_TYPE_ERROR,
.err_msg = err_msg};
}
_PyInitError _PyInitError_NoMemory(void)
{ return _PyInitError_Error("memory allocation failed"); }
_PyInitError _PyInitError_Exit(int exitcode)
{ return _Py_INIT_EXIT(exitcode); }
int _PyInitError_IsError(_PyInitError err)
{ return _Py_INIT_IS_ERROR(err); }
int _PyInitError_IsExit(_PyInitError err)
{ return _Py_INIT_IS_EXIT(err); }
int _PyInitError_Failed(_PyInitError err)
{ return _Py_INIT_FAILED(err); }
/* --- _PyWstrList ------------------------------------------------ */ /* --- _PyWstrList ------------------------------------------------ */
#ifndef NDEBUG #ifndef NDEBUG
......
...@@ -17,7 +17,7 @@ int ...@@ -17,7 +17,7 @@ int
Py_FrozenMain(int argc, char **argv) Py_FrozenMain(int argc, char **argv)
{ {
_PyInitError err = _PyRuntime_Initialize(); _PyInitError err = _PyRuntime_Initialize();
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -84,7 +84,7 @@ Py_FrozenMain(int argc, char **argv) ...@@ -84,7 +84,7 @@ Py_FrozenMain(int argc, char **argv)
err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any /* No need to call _PyCoreConfig_Clear() since we didn't allocate any
memory: program_name is a constant string. */ memory: program_name is a constant string. */
if (_Py_INIT_FAILED(err)) { if (_PyInitError_Failed(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment