2019-05-14 17:01:51 -03:00
|
|
|
#ifndef Py_BUILD_CORE_MODULE
|
|
|
|
# define Py_BUILD_CORE_MODULE
|
|
|
|
#endif
|
|
|
|
|
2019-06-06 09:25:18 -03:00
|
|
|
/* Always enable assertion (even in release mode) */
|
|
|
|
#undef NDEBUG
|
|
|
|
|
2011-04-25 16:21:07 -03:00
|
|
|
#include <Python.h>
|
2019-06-06 09:25:18 -03:00
|
|
|
#include "pycore_initconfig.h" /* _PyConfig_InitCompatConfig() */
|
|
|
|
#include "pycore_pystate.h" /* _PyRuntime */
|
2019-05-17 22:21:27 -03:00
|
|
|
#include <Python.h>
|
2017-11-30 17:05:00 -04:00
|
|
|
#include "pythread.h"
|
2017-05-22 23:46:40 -03:00
|
|
|
#include <inttypes.h>
|
2011-04-25 16:21:07 -03:00
|
|
|
#include <stdio.h>
|
2018-03-25 07:44:30 -03:00
|
|
|
#include <wchar.h>
|
2011-04-25 16:21:07 -03:00
|
|
|
|
2013-10-17 09:35:35 -03:00
|
|
|
/*********************************************************
|
|
|
|
* Embedded interpreter tests that need a custom exe
|
|
|
|
*
|
|
|
|
* Executed via 'EmbeddingTests' in Lib/test/test_capi.py
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
static void _testembed_Py_Initialize(void)
|
|
|
|
{
|
|
|
|
/* HACK: the "./" at front avoids a search along the PATH in
|
|
|
|
Modules/getpath.c */
|
|
|
|
Py_SetProgramName(L"./_testembed");
|
|
|
|
Py_Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************
|
2016-04-19 01:03:41 -03:00
|
|
|
* Test repeated initialisation and subinterpreters
|
2013-10-17 09:35:35 -03:00
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
static void print_subinterp(void)
|
2011-04-25 16:21:07 -03:00
|
|
|
{
|
2017-05-22 23:46:40 -03:00
|
|
|
/* Output information about the interpreter in the format
|
|
|
|
expected in Lib/test/test_capi.py (test_subinterps). */
|
2011-04-25 16:21:07 -03:00
|
|
|
PyThreadState *ts = PyThreadState_Get();
|
2017-05-22 23:46:40 -03:00
|
|
|
PyInterpreterState *interp = ts->interp;
|
|
|
|
int64_t id = PyInterpreterState_GetID(interp);
|
2017-05-24 21:19:47 -03:00
|
|
|
printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
|
2017-05-22 23:46:40 -03:00
|
|
|
id, (uintptr_t)interp, (uintptr_t)ts);
|
2011-04-25 16:21:07 -03:00
|
|
|
fflush(stdout);
|
|
|
|
PyRun_SimpleString(
|
|
|
|
"import sys;"
|
|
|
|
"print('id(modules) =', id(sys.modules));"
|
|
|
|
"sys.stdout.flush()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-02 00:25:03 -04:00
|
|
|
static int test_repeated_init_and_subinterpreters(void)
|
2011-04-25 16:21:07 -03:00
|
|
|
{
|
|
|
|
PyThreadState *mainstate, *substate;
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
int i, j;
|
|
|
|
|
2016-08-16 01:17:42 -03:00
|
|
|
for (i=0; i<15; i++) {
|
2011-04-25 16:21:07 -03:00
|
|
|
printf("--- Pass %d ---\n", i);
|
2013-10-17 09:35:35 -03:00
|
|
|
_testembed_Py_Initialize();
|
2011-04-25 16:21:07 -03:00
|
|
|
mainstate = PyThreadState_Get();
|
|
|
|
|
|
|
|
PyEval_InitThreads();
|
|
|
|
PyEval_ReleaseThread(mainstate);
|
|
|
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
print_subinterp();
|
|
|
|
PyThreadState_Swap(NULL);
|
|
|
|
|
|
|
|
for (j=0; j<3; j++) {
|
|
|
|
substate = Py_NewInterpreter();
|
|
|
|
print_subinterp();
|
|
|
|
Py_EndInterpreter(substate);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState_Swap(mainstate);
|
|
|
|
print_subinterp();
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
|
|
|
PyEval_RestoreThread(mainstate);
|
|
|
|
Py_Finalize();
|
|
|
|
}
|
2017-01-02 00:25:03 -04:00
|
|
|
return 0;
|
2013-10-17 09:35:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
* Test forcing a particular IO encoding
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
static void check_stdio_details(const char *encoding, const char * errors)
|
|
|
|
{
|
|
|
|
/* Output info for the test case to check */
|
|
|
|
if (encoding) {
|
|
|
|
printf("Expected encoding: %s\n", encoding);
|
|
|
|
} else {
|
|
|
|
printf("Expected encoding: default\n");
|
|
|
|
}
|
|
|
|
if (errors) {
|
|
|
|
printf("Expected errors: %s\n", errors);
|
|
|
|
} else {
|
|
|
|
printf("Expected errors: default\n");
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
/* Force the given IO encoding */
|
|
|
|
Py_SetStandardStreamEncoding(encoding, errors);
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
PyRun_SimpleString(
|
|
|
|
"import sys;"
|
|
|
|
"print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
|
|
|
|
"print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
|
|
|
|
"print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
|
|
|
|
"sys.stdout.flush()"
|
|
|
|
);
|
|
|
|
Py_Finalize();
|
|
|
|
}
|
|
|
|
|
2017-01-02 00:25:03 -04:00
|
|
|
static int test_forced_io_encoding(void)
|
2013-10-17 09:35:35 -03:00
|
|
|
{
|
|
|
|
/* Check various combinations */
|
|
|
|
printf("--- Use defaults ---\n");
|
|
|
|
check_stdio_details(NULL, NULL);
|
|
|
|
printf("--- Set errors only ---\n");
|
2014-03-17 22:38:12 -03:00
|
|
|
check_stdio_details(NULL, "ignore");
|
2013-10-17 09:35:35 -03:00
|
|
|
printf("--- Set encoding only ---\n");
|
2018-08-28 18:26:33 -03:00
|
|
|
check_stdio_details("iso8859-1", NULL);
|
2013-10-17 09:35:35 -03:00
|
|
|
printf("--- Set encoding and errors ---\n");
|
2018-08-28 18:26:33 -03:00
|
|
|
check_stdio_details("iso8859-1", "replace");
|
2013-10-17 09:35:35 -03:00
|
|
|
|
|
|
|
/* Check calling after initialization fails */
|
|
|
|
Py_Initialize();
|
|
|
|
|
|
|
|
if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
|
|
|
|
printf("Unexpected success calling Py_SetStandardStreamEncoding");
|
|
|
|
}
|
|
|
|
Py_Finalize();
|
2017-01-02 00:25:03 -04:00
|
|
|
return 0;
|
2013-10-17 09:35:35 -03:00
|
|
|
}
|
|
|
|
|
2017-11-24 07:09:24 -04:00
|
|
|
/*********************************************************
|
|
|
|
* Test parts of the C-API that work before initialization
|
|
|
|
*********************************************************/
|
|
|
|
|
2018-03-25 07:44:30 -03:00
|
|
|
/* The pre-initialization tests tend to break by segfaulting, so explicitly
|
|
|
|
* flushed progress messages make the broken API easier to find when they fail.
|
|
|
|
*/
|
|
|
|
#define _Py_EMBED_PREINIT_CHECK(msg) \
|
|
|
|
do {printf(msg); fflush(stdout);} while (0);
|
|
|
|
|
2017-11-24 07:09:24 -04:00
|
|
|
static int test_pre_initialization_api(void)
|
|
|
|
{
|
2019-03-05 18:31:54 -04:00
|
|
|
/* the test doesn't support custom memory allocators */
|
|
|
|
putenv("PYTHONMALLOC=");
|
|
|
|
|
2017-11-26 00:19:13 -04:00
|
|
|
/* Leading "./" ensures getpath.c can still find the standard library */
|
2018-03-25 07:44:30 -03:00
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
|
2017-11-26 00:19:13 -04:00
|
|
|
wchar_t *program = Py_DecodeLocale("./spam", NULL);
|
2017-11-24 07:09:24 -04:00
|
|
|
if (program == NULL) {
|
|
|
|
fprintf(stderr, "Fatal error: cannot decode program name\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-03-25 07:44:30 -03:00
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
|
2017-11-24 07:09:24 -04:00
|
|
|
Py_SetProgramName(program);
|
|
|
|
|
2018-03-25 07:44:30 -03:00
|
|
|
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
|
2017-11-24 07:09:24 -04:00
|
|
|
Py_Initialize();
|
2018-03-25 07:44:30 -03:00
|
|
|
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
|
|
|
|
PyRun_SimpleString("import sys; "
|
|
|
|
"print('sys.executable:', sys.executable)");
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
|
2017-11-24 07:09:24 -04:00
|
|
|
Py_Finalize();
|
|
|
|
|
2018-03-25 07:44:30 -03:00
|
|
|
_Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
|
2017-11-24 07:09:24 -04:00
|
|
|
PyMem_RawFree(program);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 07:44:30 -03:00
|
|
|
|
|
|
|
/* bpo-33042: Ensure embedding apps can predefine sys module options */
|
|
|
|
static int test_pre_initialization_sys_options(void)
|
|
|
|
{
|
2018-03-30 02:36:42 -03:00
|
|
|
/* We allocate a couple of the options dynamically, and then delete
|
2018-03-25 07:44:30 -03:00
|
|
|
* them before calling Py_Initialize. This ensures the interpreter isn't
|
|
|
|
* relying on the caller to keep the passed in strings alive.
|
|
|
|
*/
|
2018-03-30 02:36:42 -03:00
|
|
|
const wchar_t *static_warnoption = L"once";
|
|
|
|
const wchar_t *static_xoption = L"also_not_an_option=2";
|
2018-03-25 07:44:30 -03:00
|
|
|
size_t warnoption_len = wcslen(static_warnoption);
|
|
|
|
size_t xoption_len = wcslen(static_xoption);
|
2018-03-30 02:36:42 -03:00
|
|
|
wchar_t *dynamic_once_warnoption = \
|
|
|
|
(wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
|
|
|
|
wchar_t *dynamic_xoption = \
|
|
|
|
(wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
|
2018-03-25 07:44:30 -03:00
|
|
|
wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
|
|
|
|
wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);
|
|
|
|
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
|
|
|
|
PySys_AddWarnOption(L"default");
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
|
|
|
|
PySys_ResetWarnOptions();
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
|
|
|
|
PySys_AddWarnOption(dynamic_once_warnoption);
|
|
|
|
PySys_AddWarnOption(L"module");
|
|
|
|
PySys_AddWarnOption(L"default");
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
|
|
|
|
PySys_AddXOption(L"not_an_option=1");
|
|
|
|
PySys_AddXOption(dynamic_xoption);
|
|
|
|
|
|
|
|
/* Delete the dynamic options early */
|
|
|
|
free(dynamic_once_warnoption);
|
|
|
|
dynamic_once_warnoption = NULL;
|
|
|
|
free(dynamic_xoption);
|
|
|
|
dynamic_xoption = NULL;
|
|
|
|
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
|
|
|
|
PyRun_SimpleString("import sys; "
|
|
|
|
"print('sys.warnoptions:', sys.warnoptions); "
|
|
|
|
"print('sys._xoptions:', sys._xoptions); "
|
|
|
|
"warnings = sys.modules['warnings']; "
|
|
|
|
"latest_filters = [f[0] for f in warnings.filters[:3]]; "
|
|
|
|
"print('warnings.filters[:3]:', latest_filters)");
|
|
|
|
_Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* bpo-20891: Avoid race condition when initialising the GIL */
|
2017-11-30 17:05:00 -04:00
|
|
|
static void bpo20891_thread(void *lockp)
|
|
|
|
{
|
|
|
|
PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
|
|
|
|
|
|
|
|
PyGILState_STATE state = PyGILState_Ensure();
|
|
|
|
if (!PyGILState_Check()) {
|
|
|
|
fprintf(stderr, "PyGILState_Check failed!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
PyGILState_Release(state);
|
|
|
|
|
|
|
|
PyThread_release_lock(lock);
|
|
|
|
|
|
|
|
PyThread_exit_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_bpo20891(void)
|
|
|
|
{
|
2019-03-05 18:31:54 -04:00
|
|
|
/* the test doesn't support custom memory allocators */
|
|
|
|
putenv("PYTHONMALLOC=");
|
|
|
|
|
2017-11-30 17:05:00 -04:00
|
|
|
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
|
|
|
|
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
|
|
|
|
call PyEval_InitThreads() for us in this case. */
|
|
|
|
PyThread_type_lock lock = PyThread_allocate_lock();
|
|
|
|
if (!lock) {
|
|
|
|
fprintf(stderr, "PyThread_allocate_lock failed!");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
|
|
|
|
unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
|
|
|
|
if (thrd == PYTHREAD_INVALID_THREAD_ID) {
|
|
|
|
fprintf(stderr, "PyThread_start_new_thread failed!");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyThread_acquire_lock(lock, WAIT_LOCK);
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
/* wait until the thread exit */
|
|
|
|
PyThread_acquire_lock(lock, WAIT_LOCK);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
PyThread_free_lock(lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-22 14:14:51 -03:00
|
|
|
static int test_initialize_twice(void)
|
|
|
|
{
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
|
|
|
|
/* bpo-33932: Calling Py_Initialize() twice should do nothing
|
|
|
|
* (and not crash!). */
|
|
|
|
Py_Initialize();
|
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-20 12:34:23 -03:00
|
|
|
static int test_initialize_pymain(void)
|
|
|
|
{
|
|
|
|
wchar_t *argv[] = {L"PYTHON", L"-c",
|
2019-05-17 14:01:14 -03:00
|
|
|
(L"import sys; "
|
|
|
|
L"print(f'Py_Main() after Py_Initialize: "
|
|
|
|
L"sys.argv={sys.argv}')"),
|
2018-07-20 12:34:23 -03:00
|
|
|
L"arg2"};
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
|
|
|
|
/* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
|
|
|
|
Py_Main(Py_ARRAY_LENGTH(argv), argv);
|
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-24 07:09:24 -04:00
|
|
|
|
2018-11-13 14:59:26 -04:00
|
|
|
static void
|
|
|
|
dump_config(void)
|
|
|
|
{
|
2019-04-18 06:37:26 -03:00
|
|
|
(void) PyRun_SimpleStringFlags(
|
|
|
|
"import _testinternalcapi, json; "
|
|
|
|
"print(json.dumps(_testinternalcapi.get_configs()))",
|
|
|
|
0);
|
2018-11-13 14:59:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
static int test_init_initialize_config(void)
|
2018-07-26 13:57:56 -03:00
|
|
|
{
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
static int check_init_compat_config(int preinit)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-22 18:58:50 -03:00
|
|
|
|
|
|
|
if (preinit) {
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-05-22 18:58:50 -03:00
|
|
|
_PyPreConfig_InitCompatConfig(&preconfig);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-22 18:58:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2019-05-22 18:58:50 -03:00
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-22 18:58:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_preinit_compat_config(void)
|
|
|
|
{
|
|
|
|
return check_init_compat_config(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_init_compat_config(void)
|
|
|
|
{
|
|
|
|
return check_init_compat_config(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
static int test_init_global_config(void)
|
|
|
|
{
|
|
|
|
/* FIXME: test Py_IgnoreEnvironmentFlag */
|
|
|
|
|
|
|
|
putenv("PYTHONUTF8=0");
|
|
|
|
Py_UTF8Mode = 1;
|
|
|
|
|
|
|
|
/* Test initialization from global configuration variables (Py_xxx) */
|
|
|
|
Py_SetProgramName(L"./globalvar");
|
|
|
|
|
|
|
|
/* Py_IsolatedFlag is not tested */
|
|
|
|
Py_NoSiteFlag = 1;
|
|
|
|
Py_BytesWarningFlag = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONINSPECT=");
|
|
|
|
Py_InspectFlag = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONOPTIMIZE=0");
|
|
|
|
Py_InteractiveFlag = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONDEBUG=0");
|
|
|
|
Py_OptimizeFlag = 2;
|
|
|
|
|
|
|
|
/* Py_DebugFlag is not tested */
|
|
|
|
|
|
|
|
putenv("PYTHONDONTWRITEBYTECODE=");
|
|
|
|
Py_DontWriteBytecodeFlag = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONVERBOSE=0");
|
|
|
|
Py_VerboseFlag = 1;
|
|
|
|
|
|
|
|
Py_QuietFlag = 1;
|
|
|
|
Py_NoUserSiteDirectory = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONUNBUFFERED=");
|
|
|
|
Py_UnbufferedStdioFlag = 1;
|
|
|
|
|
2019-05-16 13:30:15 -03:00
|
|
|
Py_FrozenFlag = 1;
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
/* FIXME: test Py_LegacyWindowsFSEncodingFlag */
|
|
|
|
/* FIXME: test Py_LegacyWindowsStdioFlag */
|
|
|
|
|
|
|
|
Py_Initialize();
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_init_from_config(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-25 22:31:11 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-05-22 18:58:50 -03:00
|
|
|
_PyPreConfig_InitCompatConfig(&preconfig);
|
2019-03-25 22:31:11 -03:00
|
|
|
|
|
|
|
putenv("PYTHONMALLOC=malloc_debug");
|
2019-05-17 10:20:52 -03:00
|
|
|
preconfig.allocator = PYMEM_ALLOCATOR_MALLOC;
|
2019-03-25 22:31:11 -03:00
|
|
|
|
|
|
|
putenv("PYTHONUTF8=0");
|
|
|
|
Py_UTF8Mode = 0;
|
|
|
|
preconfig.utf8_mode = 1;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-25 22:31:11 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* Test Py_InitializeFromConfig() */
|
|
|
|
PyConfig config;
|
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2018-07-26 13:57:56 -03:00
|
|
|
config.install_signal_handlers = 0;
|
|
|
|
|
|
|
|
/* FIXME: test use_environment */
|
|
|
|
|
|
|
|
putenv("PYTHONHASHSEED=42");
|
|
|
|
config.use_hash_seed = 1;
|
|
|
|
config.hash_seed = 123;
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
/* dev_mode=1 is tested in test_init_dev_mode() */
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
putenv("PYTHONFAULTHANDLER=");
|
|
|
|
config.faulthandler = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONTRACEMALLOC=0");
|
|
|
|
config.tracemalloc = 2;
|
|
|
|
|
|
|
|
putenv("PYTHONPROFILEIMPORTTIME=0");
|
|
|
|
config.import_time = 1;
|
|
|
|
|
|
|
|
config.show_ref_count = 1;
|
|
|
|
config.show_alloc_count = 1;
|
|
|
|
/* FIXME: test dump_refs: bpo-34223 */
|
|
|
|
|
|
|
|
putenv("PYTHONMALLOCSTATS=0");
|
|
|
|
config.malloc_stats = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
|
|
|
|
config.pycache_prefix = L"conf_pycache_prefix";
|
|
|
|
|
|
|
|
Py_SetProgramName(L"./globalvar");
|
|
|
|
config.program_name = L"./conf_program_name";
|
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
static wchar_t* argv[] = {
|
|
|
|
L"python3",
|
2018-11-14 12:39:45 -04:00
|
|
|
L"-c",
|
|
|
|
L"pass",
|
2019-03-29 11:13:46 -03:00
|
|
|
L"arg2",
|
2018-11-14 12:39:45 -04:00
|
|
|
};
|
2019-03-15 11:08:05 -03:00
|
|
|
config.argv.length = Py_ARRAY_LENGTH(argv);
|
|
|
|
config.argv.items = argv;
|
2019-05-17 14:01:14 -03:00
|
|
|
config.parse_argv = 1;
|
2018-11-14 12:39:45 -04:00
|
|
|
|
|
|
|
static wchar_t* xoptions[3] = {
|
2019-05-27 11:39:22 -03:00
|
|
|
L"xoption1=3",
|
|
|
|
L"xoption2=",
|
|
|
|
L"xoption3",
|
2018-11-14 12:39:45 -04:00
|
|
|
};
|
2019-03-15 11:08:05 -03:00
|
|
|
config.xoptions.length = Py_ARRAY_LENGTH(xoptions);
|
|
|
|
config.xoptions.items = xoptions;
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2019-03-26 12:58:50 -03:00
|
|
|
static wchar_t* warnoptions[1] = {
|
2018-11-14 12:39:45 -04:00
|
|
|
L"error::ResourceWarning",
|
|
|
|
};
|
2019-03-15 11:08:05 -03:00
|
|
|
config.warnoptions.length = Py_ARRAY_LENGTH(warnoptions);
|
|
|
|
config.warnoptions.items = warnoptions;
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* FIXME: test pythonpath_env */
|
2018-07-26 13:57:56 -03:00
|
|
|
/* FIXME: test home */
|
|
|
|
/* FIXME: test path config: module_search_path .. dll_path */
|
|
|
|
|
|
|
|
putenv("PYTHONVERBOSE=0");
|
|
|
|
Py_VerboseFlag = 0;
|
|
|
|
config.verbose = 1;
|
|
|
|
|
|
|
|
Py_NoSiteFlag = 0;
|
|
|
|
config.site_import = 0;
|
|
|
|
|
|
|
|
Py_BytesWarningFlag = 0;
|
|
|
|
config.bytes_warning = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONINSPECT=");
|
|
|
|
Py_InspectFlag = 0;
|
|
|
|
config.inspect = 1;
|
|
|
|
|
|
|
|
Py_InteractiveFlag = 0;
|
|
|
|
config.interactive = 1;
|
|
|
|
|
|
|
|
putenv("PYTHONOPTIMIZE=0");
|
|
|
|
Py_OptimizeFlag = 1;
|
|
|
|
config.optimization_level = 2;
|
|
|
|
|
2018-07-31 22:07:00 -03:00
|
|
|
/* FIXME: test parser_debug */
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
putenv("PYTHONDONTWRITEBYTECODE=");
|
|
|
|
Py_DontWriteBytecodeFlag = 0;
|
|
|
|
config.write_bytecode = 0;
|
|
|
|
|
|
|
|
Py_QuietFlag = 0;
|
|
|
|
config.quiet = 1;
|
|
|
|
|
2019-05-17 14:01:14 -03:00
|
|
|
config.configure_c_stdio = 1;
|
2019-05-16 13:30:15 -03:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
putenv("PYTHONUNBUFFERED=");
|
|
|
|
Py_UnbufferedStdioFlag = 0;
|
2018-07-31 22:07:00 -03:00
|
|
|
config.buffered_stdio = 0;
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
putenv("PYTHONIOENCODING=cp424");
|
|
|
|
Py_SetStandardStreamEncoding("ascii", "ignore");
|
2018-11-14 12:39:45 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* Py_SetStandardStreamEncoding() sets Py_LegacyWindowsStdioFlag to 1.
|
|
|
|
Force it to 0 through the config. */
|
|
|
|
config.legacy_windows_stdio = 0;
|
|
|
|
#endif
|
2019-05-02 15:56:30 -03:00
|
|
|
config.stdio_encoding = L"iso8859-1";
|
|
|
|
config.stdio_errors = L"replace";
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
putenv("PYTHONNOUSERSITE=");
|
|
|
|
Py_NoUserSiteDirectory = 0;
|
|
|
|
config.user_site_directory = 0;
|
|
|
|
|
2019-05-02 00:51:56 -03:00
|
|
|
config.check_hash_pycs_mode = L"always";
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-16 13:30:15 -03:00
|
|
|
Py_FrozenFlag = 0;
|
|
|
|
config.pathconfig_warnings = 0;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int check_init_parse_argv(int parse_argv)
|
2019-05-16 12:02:56 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-16 12:02:56 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-16 12:02:56 -03:00
|
|
|
|
|
|
|
static wchar_t* argv[] = {
|
2019-05-17 14:01:14 -03:00
|
|
|
L"./argv0",
|
|
|
|
L"-E",
|
2019-05-16 12:02:56 -03:00
|
|
|
L"-c",
|
2019-05-17 14:01:14 -03:00
|
|
|
L"pass",
|
2019-05-16 12:02:56 -03:00
|
|
|
L"arg1",
|
2019-05-17 14:01:14 -03:00
|
|
|
L"-v",
|
|
|
|
L"arg3",
|
2019-05-16 12:02:56 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
config.argv.length = Py_ARRAY_LENGTH(argv);
|
|
|
|
config.argv.items = argv;
|
2019-05-17 14:01:14 -03:00
|
|
|
config.parse_argv = parse_argv;
|
2019-05-16 12:02:56 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-16 12:02:56 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int test_init_parse_argv(void)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-05-17 22:21:27 -03:00
|
|
|
return check_init_parse_argv(1);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int test_init_dont_parse_argv(void)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-05-17 22:21:27 -03:00
|
|
|
return check_init_parse_argv(0);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 11:38:48 -03:00
|
|
|
static void set_most_env_vars(void)
|
2018-07-26 13:57:56 -03:00
|
|
|
{
|
|
|
|
putenv("PYTHONHASHSEED=42");
|
2019-03-06 07:51:53 -04:00
|
|
|
putenv("PYTHONMALLOC=malloc");
|
2018-07-26 13:57:56 -03:00
|
|
|
putenv("PYTHONTRACEMALLOC=2");
|
|
|
|
putenv("PYTHONPROFILEIMPORTTIME=1");
|
|
|
|
putenv("PYTHONMALLOCSTATS=1");
|
|
|
|
putenv("PYTHONUTF8=1");
|
|
|
|
putenv("PYTHONVERBOSE=1");
|
|
|
|
putenv("PYTHONINSPECT=1");
|
|
|
|
putenv("PYTHONOPTIMIZE=2");
|
|
|
|
putenv("PYTHONDONTWRITEBYTECODE=1");
|
|
|
|
putenv("PYTHONUNBUFFERED=1");
|
|
|
|
putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
|
|
|
|
putenv("PYTHONNOUSERSITE=1");
|
|
|
|
putenv("PYTHONFAULTHANDLER=1");
|
2018-08-29 06:47:29 -03:00
|
|
|
putenv("PYTHONIOENCODING=iso8859-1:replace");
|
2019-05-20 11:38:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void set_all_env_vars(void)
|
|
|
|
{
|
|
|
|
set_most_env_vars();
|
|
|
|
|
|
|
|
putenv("PYTHONWARNINGS=EnvVar");
|
|
|
|
putenv("PYTHONPATH=/my/path");
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 23:12:27 -03:00
|
|
|
static int test_init_compat_env(void)
|
2019-03-06 07:51:53 -04:00
|
|
|
{
|
|
|
|
/* Test initialization from environment variables */
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
2019-05-20 06:02:00 -03:00
|
|
|
set_all_env_vars();
|
2019-03-06 07:51:53 -04:00
|
|
|
_testembed_Py_Initialize();
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 23:12:27 -03:00
|
|
|
static int test_init_python_env(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-22 23:12:27 -03:00
|
|
|
|
|
|
|
set_all_env_vars();
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-22 23:12:27 -03:00
|
|
|
}
|
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-22 23:12:27 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
static void set_all_env_vars_dev_mode(void)
|
2019-03-05 12:37:44 -04:00
|
|
|
{
|
2019-03-06 07:51:53 -04:00
|
|
|
putenv("PYTHONMALLOC=");
|
2019-03-05 12:37:44 -04:00
|
|
|
putenv("PYTHONFAULTHANDLER=");
|
|
|
|
putenv("PYTHONDEVMODE=1");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 07:51:53 -04:00
|
|
|
static int test_init_env_dev_mode(void)
|
2018-07-26 13:57:56 -03:00
|
|
|
{
|
|
|
|
/* Test initialization from environment variables */
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
2019-05-20 06:02:00 -03:00
|
|
|
set_all_env_vars_dev_mode();
|
2018-07-26 13:57:56 -03:00
|
|
|
_testembed_Py_Initialize();
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 07:51:53 -04:00
|
|
|
static int test_init_env_dev_mode_alloc(void)
|
2019-03-05 12:37:44 -04:00
|
|
|
{
|
|
|
|
/* Test initialization from environment variables */
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
2019-05-20 06:02:00 -03:00
|
|
|
set_all_env_vars_dev_mode();
|
2019-03-06 07:51:53 -04:00
|
|
|
putenv("PYTHONMALLOC=malloc");
|
2019-03-05 12:37:44 -04:00
|
|
|
_testembed_Py_Initialize();
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int test_init_isolated_flag(void)
|
2018-07-26 13:57:56 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-25 22:31:11 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* Test PyConfig.isolated=1 */
|
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-03-04 21:01:27 -04:00
|
|
|
Py_IsolatedFlag = 0;
|
2019-03-25 22:31:11 -03:00
|
|
|
config.isolated = 1;
|
2019-03-04 21:01:27 -04:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
/* Use path starting with "./" avoids a search along the PATH */
|
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
set_all_env_vars();
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* PyPreConfig.isolated=1, PyConfig.isolated=0 */
|
2019-03-26 20:26:18 -03:00
|
|
|
static int test_preinit_isolated1(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-26 20:26:18 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-05-22 18:58:50 -03:00
|
|
|
_PyPreConfig_InitCompatConfig(&preconfig);
|
2019-03-26 20:26:18 -03:00
|
|
|
preconfig.isolated = 1;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-26 20:26:18 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2019-03-26 20:26:18 -03:00
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
set_all_env_vars();
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-26 20:26:18 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* PyPreConfig.isolated=0, PyConfig.isolated=1 */
|
2019-03-26 20:26:18 -03:00
|
|
|
static int test_preinit_isolated2(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-26 20:26:18 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-05-22 18:58:50 -03:00
|
|
|
_PyPreConfig_InitCompatConfig(&preconfig);
|
2019-03-26 20:26:18 -03:00
|
|
|
preconfig.isolated = 0;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-26 20:26:18 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* Test PyConfig.isolated=1 */
|
|
|
|
PyConfig config;
|
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2019-03-26 20:26:18 -03:00
|
|
|
|
|
|
|
Py_IsolatedFlag = 0;
|
|
|
|
config.isolated = 1;
|
|
|
|
|
|
|
|
/* Use path starting with "./" avoids a search along the PATH */
|
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
set_all_env_vars();
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-26 20:26:18 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
static int test_preinit_dont_parse_argv(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
PyPreConfig_InitIsolatedConfig(&preconfig);
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
preconfig.isolated = 0;
|
|
|
|
|
|
|
|
/* -X dev must be ignored by isolated preconfiguration */
|
|
|
|
wchar_t *argv[] = {L"python3",
|
|
|
|
L"-E",
|
|
|
|
L"-I",
|
|
|
|
L"-X", L"dev",
|
|
|
|
L"-X", L"utf8",
|
|
|
|
L"script.py"};
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitializeFromArgs(&preconfig, Py_ARRAY_LENGTH(argv), argv);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_InitIsolatedConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
config.isolated = 0;
|
|
|
|
|
|
|
|
/* Pre-initialize implicitly using argv: make sure that -X dev
|
|
|
|
is used to configure the allocation in preinitialization */
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetArgv(&config, Py_ARRAY_LENGTH(argv), argv);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2019-05-20 06:02:00 -03:00
|
|
|
L"./_testembed");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed:
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_preinit_parse_argv(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-initialize implicitly using argv: make sure that -X dev
|
|
|
|
is used to configure the allocation in preinitialization */
|
|
|
|
wchar_t *argv[] = {L"python3", L"-X", L"dev", L"script.py"};
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetArgv(&config, Py_ARRAY_LENGTH(argv), argv);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2019-05-20 06:02:00 -03:00
|
|
|
L"./_testembed");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed:
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static void set_all_global_config_variables(void)
|
|
|
|
{
|
|
|
|
Py_IsolatedFlag = 0;
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
|
|
|
Py_BytesWarningFlag = 2;
|
|
|
|
Py_InspectFlag = 1;
|
|
|
|
Py_InteractiveFlag = 1;
|
|
|
|
Py_OptimizeFlag = 1;
|
|
|
|
Py_DebugFlag = 1;
|
|
|
|
Py_VerboseFlag = 1;
|
|
|
|
Py_QuietFlag = 1;
|
|
|
|
Py_FrozenFlag = 0;
|
|
|
|
Py_UnbufferedStdioFlag = 1;
|
|
|
|
Py_NoSiteFlag = 1;
|
|
|
|
Py_DontWriteBytecodeFlag = 1;
|
|
|
|
Py_NoUserSiteDirectory = 1;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
Py_LegacyWindowsStdioFlag = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
static int check_preinit_isolated_config(int preinit)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyPreConfig *rt_preconfig;
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
/* environment variables must be ignored */
|
|
|
|
set_all_env_vars();
|
|
|
|
|
|
|
|
/* global configuration variables must be ignored */
|
|
|
|
set_all_global_config_variables();
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
if (preinit) {
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
PyPreConfig_InitIsolatedConfig(&preconfig);
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
rt_preconfig = &_PyRuntime.preconfig;
|
|
|
|
assert(rt_preconfig->isolated == 1);
|
|
|
|
assert(rt_preconfig->use_environment == 0);
|
|
|
|
}
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitIsolatedConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
rt_preconfig = &_PyRuntime.preconfig;
|
|
|
|
assert(rt_preconfig->isolated == 1);
|
|
|
|
assert(rt_preconfig->use_environment == 0);
|
|
|
|
|
2019-05-17 14:01:14 -03:00
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
static int test_preinit_isolated_config(void)
|
|
|
|
{
|
|
|
|
return check_preinit_isolated_config(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_init_isolated_config(void)
|
|
|
|
{
|
|
|
|
return check_preinit_isolated_config(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int check_init_python_config(int preinit)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
/* global configuration variables must be ignored */
|
|
|
|
set_all_global_config_variables();
|
|
|
|
Py_IsolatedFlag = 1;
|
|
|
|
Py_IgnoreEnvironmentFlag = 1;
|
|
|
|
Py_FrozenFlag = 1;
|
|
|
|
Py_UnbufferedStdioFlag = 1;
|
|
|
|
Py_NoSiteFlag = 1;
|
|
|
|
Py_DontWriteBytecodeFlag = 1;
|
|
|
|
Py_NoUserSiteDirectory = 1;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
Py_LegacyWindowsStdioFlag = 1;
|
|
|
|
#endif
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
if (preinit) {
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
PyPreConfig_InitPythonConfig(&preconfig);
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
config.program_name = L"./_testembed";
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
static int test_preinit_python_config(void)
|
|
|
|
{
|
|
|
|
return check_init_python_config(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_init_python_config(void)
|
|
|
|
{
|
|
|
|
return check_init_python_config(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int test_init_dont_configure_locale(void)
|
2019-05-17 17:44:16 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-17 17:44:16 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
PyPreConfig_InitPythonConfig(&preconfig);
|
2019-05-17 17:44:16 -03:00
|
|
|
preconfig.configure_locale = 0;
|
|
|
|
preconfig.coerce_c_locale = 1;
|
|
|
|
preconfig.coerce_c_locale_warn = 1;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 17:44:16 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-17 17:44:16 -03:00
|
|
|
config.program_name = L"./_testembed";
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-17 17:44:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 22:21:27 -03:00
|
|
|
static int test_init_dev_mode(void)
|
2018-07-26 13:57:56 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2018-07-26 13:57:56 -03:00
|
|
|
putenv("PYTHONFAULTHANDLER=");
|
|
|
|
putenv("PYTHONMALLOC=");
|
2019-03-25 22:31:11 -03:00
|
|
|
config.dev_mode = 1;
|
2018-07-26 13:57:56 -03:00
|
|
|
config.program_name = L"./_testembed";
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
static PyObject *_open_code_hook(PyObject *path, void *data)
|
|
|
|
{
|
|
|
|
if (PyUnicode_CompareWithASCIIString(path, "$$test-filename") == 0) {
|
|
|
|
return PyLong_FromVoidPtr(data);
|
|
|
|
}
|
|
|
|
PyObject *io = PyImport_ImportModule("_io");
|
|
|
|
if (!io) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_CallMethod(io, "open", "Os", path, "rb");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_open_code_hook(void)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
/* Provide a hook */
|
|
|
|
result = PyFile_SetOpenCodeHook(_open_code_hook, &result);
|
|
|
|
if (result) {
|
|
|
|
printf("Failed to set hook\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* A second hook should fail */
|
|
|
|
result = PyFile_SetOpenCodeHook(_open_code_hook, &result);
|
|
|
|
if (!result) {
|
|
|
|
printf("Should have failed to set second hook\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
PyObject *r = PyFile_OpenCode("$$test-filename");
|
|
|
|
if (!r) {
|
|
|
|
PyErr_Print();
|
|
|
|
result = 3;
|
|
|
|
} else {
|
|
|
|
void *cmp = PyLong_AsVoidPtr(r);
|
|
|
|
Py_DECREF(r);
|
|
|
|
if (cmp != &result) {
|
|
|
|
printf("Did not get expected result from hook\n");
|
|
|
|
result = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
PyObject *io = PyImport_ImportModule("_io");
|
|
|
|
PyObject *r = io
|
|
|
|
? PyObject_CallMethod(io, "open_code", "s", "$$test-filename")
|
|
|
|
: NULL;
|
|
|
|
if (!r) {
|
|
|
|
PyErr_Print();
|
|
|
|
result = 5;
|
|
|
|
} else {
|
|
|
|
void *cmp = PyLong_AsVoidPtr(r);
|
|
|
|
Py_DECREF(r);
|
|
|
|
if (cmp != &result) {
|
|
|
|
printf("Did not get expected result from hook\n");
|
|
|
|
result = 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_XDECREF(io);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _audit_hook(const char *event, PyObject *args, void *userdata)
|
|
|
|
{
|
|
|
|
if (strcmp(event, "_testembed.raise") == 0) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Intentional error");
|
|
|
|
return -1;
|
|
|
|
} else if (strcmp(event, "_testembed.set") == 0) {
|
|
|
|
if (!PyArg_ParseTuple(args, "n", userdata)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _test_audit(Py_ssize_t setValue)
|
|
|
|
{
|
|
|
|
Py_ssize_t sawSet = 0;
|
|
|
|
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
|
|
|
PySys_AddAuditHook(_audit_hook, &sawSet);
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
|
|
|
|
if (PySys_Audit("_testembed.raise", NULL) == 0) {
|
|
|
|
printf("No error raised");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PySys_Audit("_testembed.nop", NULL) != 0) {
|
|
|
|
printf("Nop event failed");
|
|
|
|
/* Exception from above may still remain */
|
|
|
|
PyErr_Clear();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
printf("Exception not preserved");
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
if (PySys_Audit("_testembed.set", "n", setValue) != 0) {
|
|
|
|
PyErr_Print();
|
|
|
|
printf("Set event failed");
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sawSet != 42) {
|
|
|
|
printf("Failed to see *userData change\n");
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_audit(void)
|
|
|
|
{
|
|
|
|
int result = _test_audit(42);
|
|
|
|
Py_Finalize();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static volatile int _audit_subinterpreter_interpreter_count = 0;
|
|
|
|
|
|
|
|
static int _audit_subinterpreter_hook(const char *event, PyObject *args, void *userdata)
|
|
|
|
{
|
|
|
|
printf("%s\n", event);
|
|
|
|
if (strcmp(event, "cpython.PyInterpreterState_New") == 0) {
|
|
|
|
_audit_subinterpreter_interpreter_count += 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_audit_subinterpreter(void)
|
|
|
|
{
|
|
|
|
Py_IgnoreEnvironmentFlag = 0;
|
|
|
|
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
|
|
|
|
_testembed_Py_Initialize();
|
|
|
|
|
2019-05-23 20:53:21 -03:00
|
|
|
Py_NewInterpreter();
|
|
|
|
Py_NewInterpreter();
|
|
|
|
Py_NewInterpreter();
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
switch (_audit_subinterpreter_interpreter_count) {
|
|
|
|
case 3: return 0;
|
|
|
|
case 0: return -1;
|
|
|
|
default: return _audit_subinterpreter_interpreter_count;
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-14 17:01:51 -03:00
|
|
|
static int test_init_read_set(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-14 17:01:51 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetBytesString(&config, &config.program_name,
|
2019-05-20 06:02:00 -03:00
|
|
|
"./init_read_set");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-14 17:01:51 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_Read(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-14 17:01:51 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyWideStringList_Append(&config.module_search_paths,
|
|
|
|
L"init_read_set_path");
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-14 17:01:51 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* override executable computed by PyConfig_Read() */
|
|
|
|
status = PyConfig_SetString(&config, &config.executable, L"my_executable");
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-14 17:01:51 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-14 17:01:51 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
dump_config();
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2019-05-27 11:39:22 -03:00
|
|
|
Py_ExitStatusException(status);
|
2019-05-14 17:01:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
wchar_t *init_main_argv[] = {
|
|
|
|
L"python3", L"-c",
|
|
|
|
(L"import _testinternalcapi, json; "
|
|
|
|
L"print(json.dumps(_testinternalcapi.get_configs()))"),
|
|
|
|
L"arg2"};
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static void configure_init_main(PyConfig *config)
|
2019-05-16 12:38:16 -03:00
|
|
|
{
|
|
|
|
config->argv.length = Py_ARRAY_LENGTH(init_main_argv);
|
|
|
|
config->argv.items = init_main_argv;
|
2019-05-17 14:01:14 -03:00
|
|
|
config->parse_argv = 1;
|
2019-05-16 12:38:16 -03:00
|
|
|
config->program_name = L"./python3";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_init_run_main(void)
|
2019-03-29 11:13:46 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-16 12:38:16 -03:00
|
|
|
configure_init_main(&config);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return Py_RunMain();
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
2019-03-29 11:13:46 -03:00
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
|
|
|
|
static int test_init_main(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
2019-05-16 12:38:16 -03:00
|
|
|
configure_init_main(&config);
|
|
|
|
config._init_main = 0;
|
2019-03-29 11:13:46 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
/* sys.stdout don't exist yet: it is created by _Py_InitializeMain() */
|
|
|
|
int res = PyRun_SimpleString(
|
|
|
|
"import sys; "
|
|
|
|
"print('Run Python code before _Py_InitializeMain', "
|
|
|
|
"file=sys.stderr)");
|
|
|
|
if (res < 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _Py_InitializeMain();
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return Py_RunMain();
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
static int test_run_main(void)
|
2019-05-14 21:12:48 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_InitPythonConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
2019-05-14 21:12:48 -03:00
|
|
|
|
|
|
|
wchar_t *argv[] = {L"python3", L"-c",
|
2019-05-16 12:38:16 -03:00
|
|
|
(L"import sys; "
|
2019-05-27 11:39:22 -03:00
|
|
|
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
|
2019-05-14 21:12:48 -03:00
|
|
|
L"arg2"};
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetArgv(&config, Py_ARRAY_LENGTH(argv), argv);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
2019-05-14 21:12:48 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2019-05-20 06:02:00 -03:00
|
|
|
L"./python3");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 06:02:00 -03:00
|
|
|
goto failed;
|
2019-05-14 21:12:48 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
2019-05-14 21:12:48 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return Py_RunMain();
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
failed:
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-14 21:12:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-02 00:25:03 -04:00
|
|
|
/* *********************************************************
|
|
|
|
* List of test cases and the function that implements it.
|
2017-09-14 03:38:36 -03:00
|
|
|
*
|
2017-01-02 00:25:03 -04:00
|
|
|
* Names are compared case-sensitively with the first
|
|
|
|
* argument. If no match is found, or no first argument was
|
|
|
|
* provided, the names of all test cases are printed and
|
|
|
|
* the exit code will be -1.
|
|
|
|
*
|
|
|
|
* The int returned from test functions is used as the exit
|
|
|
|
* code, and test_capi treats all non-zero exit codes as a
|
2017-09-14 03:38:36 -03:00
|
|
|
* failed test.
|
2017-01-02 00:25:03 -04:00
|
|
|
*********************************************************/
|
|
|
|
struct TestCase
|
2013-10-17 09:35:35 -03:00
|
|
|
{
|
2017-01-02 00:25:03 -04:00
|
|
|
const char *name;
|
|
|
|
int (*func)(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct TestCase TestCases[] = {
|
2019-05-22 19:57:57 -03:00
|
|
|
{"test_forced_io_encoding", test_forced_io_encoding},
|
|
|
|
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
|
|
|
|
{"test_pre_initialization_api", test_pre_initialization_api},
|
|
|
|
{"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
|
|
|
|
{"test_bpo20891", test_bpo20891},
|
|
|
|
{"test_initialize_twice", test_initialize_twice},
|
|
|
|
{"test_initialize_pymain", test_initialize_pymain},
|
|
|
|
{"test_init_initialize_config", test_init_initialize_config},
|
|
|
|
{"test_preinit_compat_config", test_preinit_compat_config},
|
|
|
|
{"test_init_compat_config", test_init_compat_config},
|
|
|
|
{"test_init_global_config", test_init_global_config},
|
|
|
|
{"test_init_from_config", test_init_from_config},
|
|
|
|
{"test_init_parse_argv", test_init_parse_argv},
|
|
|
|
{"test_init_dont_parse_argv", test_init_dont_parse_argv},
|
2019-05-22 23:12:27 -03:00
|
|
|
{"test_init_compat_env", test_init_compat_env},
|
|
|
|
{"test_init_python_env", test_init_python_env},
|
2019-05-22 19:57:57 -03:00
|
|
|
{"test_init_env_dev_mode", test_init_env_dev_mode},
|
|
|
|
{"test_init_env_dev_mode_alloc", test_init_env_dev_mode_alloc},
|
|
|
|
{"test_init_dont_configure_locale", test_init_dont_configure_locale},
|
|
|
|
{"test_init_dev_mode", test_init_dev_mode},
|
|
|
|
{"test_init_isolated_flag", test_init_isolated_flag},
|
|
|
|
{"test_preinit_isolated_config", test_preinit_isolated_config},
|
|
|
|
{"test_init_isolated_config", test_init_isolated_config},
|
|
|
|
{"test_preinit_python_config", test_preinit_python_config},
|
|
|
|
{"test_init_python_config", test_init_python_config},
|
|
|
|
{"test_preinit_isolated1", test_preinit_isolated1},
|
|
|
|
{"test_preinit_isolated2", test_preinit_isolated2},
|
|
|
|
{"test_preinit_parse_argv", test_preinit_parse_argv},
|
|
|
|
{"test_preinit_dont_parse_argv", test_preinit_dont_parse_argv},
|
|
|
|
{"test_init_read_set", test_init_read_set},
|
|
|
|
{"test_init_run_main", test_init_run_main},
|
|
|
|
{"test_init_main", test_init_main},
|
|
|
|
{"test_run_main", test_run_main},
|
2019-05-23 12:45:22 -03:00
|
|
|
{"test_open_code_hook", test_open_code_hook},
|
|
|
|
{"test_audit", test_audit},
|
|
|
|
{"test_audit_subinterpreter", test_audit_subinterpreter},
|
2019-05-22 19:57:57 -03:00
|
|
|
{NULL, NULL}
|
2017-01-02 00:25:03 -04:00
|
|
|
};
|
2013-10-17 09:35:35 -03:00
|
|
|
|
2017-01-02 00:25:03 -04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-10-17 09:35:35 -03:00
|
|
|
if (argc > 1) {
|
2017-01-02 00:25:03 -04:00
|
|
|
for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
|
|
|
|
if (strcmp(argv[1], tc->name) == 0)
|
|
|
|
return (*tc->func)();
|
|
|
|
}
|
2013-10-17 09:35:35 -03:00
|
|
|
}
|
2017-01-02 00:25:03 -04:00
|
|
|
|
|
|
|
/* No match found, or no test name provided, so display usage */
|
|
|
|
printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
|
2018-03-25 07:44:30 -03:00
|
|
|
"Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
|
2017-01-02 00:25:03 -04:00
|
|
|
"Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
|
|
|
|
for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
|
|
|
|
printf(" %s\n", tc->name);
|
|
|
|
}
|
|
|
|
|
2018-03-25 07:44:30 -03:00
|
|
|
/* Non-zero exit code will cause test_embed.py tests to fail.
|
2017-01-02 00:25:03 -04:00
|
|
|
This is intentional. */
|
|
|
|
return -1;
|
2011-04-25 16:21:07 -03:00
|
|
|
}
|