2011-04-25 21:21:07 +02:00
|
|
|
#include <Python.h>
|
2017-05-23 04:46:40 +02:00
|
|
|
#include <inttypes.h>
|
2011-04-25 21:21:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-10-17 14:35:35 +02: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 06:03:41 +02:00
|
|
|
* Test repeated initialisation and subinterpreters
|
2013-10-17 14:35:35 +02:00
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
static void print_subinterp(void)
|
2011-04-25 21:21:07 +02:00
|
|
|
{
|
2017-05-23 04:46:40 +02:00
|
|
|
/* Output information about the interpreter in the format
|
|
|
|
expected in Lib/test/test_capi.py (test_subinterps). */
|
2011-04-25 21:21:07 +02:00
|
|
|
PyThreadState *ts = PyThreadState_Get();
|
2017-05-23 04:46:40 +02:00
|
|
|
PyInterpreterState *interp = ts->interp;
|
|
|
|
int64_t id = PyInterpreterState_GetID(interp);
|
2017-05-25 02:19:47 +02:00
|
|
|
printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
|
2017-05-23 04:46:40 +02:00
|
|
|
id, (uintptr_t)interp, (uintptr_t)ts);
|
2011-04-25 21:21:07 +02:00
|
|
|
fflush(stdout);
|
|
|
|
PyRun_SimpleString(
|
|
|
|
"import sys;"
|
|
|
|
"print('id(modules) =', id(sys.modules));"
|
|
|
|
"sys.stdout.flush()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-02 05:25:03 +01:00
|
|
|
static int test_repeated_init_and_subinterpreters(void)
|
2011-04-25 21:21:07 +02:00
|
|
|
{
|
|
|
|
PyThreadState *mainstate, *substate;
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
int i, j;
|
|
|
|
|
2016-08-16 06:17:42 +02:00
|
|
|
for (i=0; i<15; i++) {
|
2011-04-25 21:21:07 +02:00
|
|
|
printf("--- Pass %d ---\n", i);
|
2013-10-17 14:35:35 +02:00
|
|
|
_testembed_Py_Initialize();
|
2011-04-25 21:21:07 +02: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 05:25:03 +01:00
|
|
|
return 0;
|
2013-10-17 14:35:35 +02: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 05:25:03 +01:00
|
|
|
static int test_forced_io_encoding(void)
|
2013-10-17 14:35:35 +02:00
|
|
|
{
|
|
|
|
/* Check various combinations */
|
|
|
|
printf("--- Use defaults ---\n");
|
|
|
|
check_stdio_details(NULL, NULL);
|
|
|
|
printf("--- Set errors only ---\n");
|
2014-03-18 02:38:12 +01:00
|
|
|
check_stdio_details(NULL, "ignore");
|
2013-10-17 14:35:35 +02:00
|
|
|
printf("--- Set encoding only ---\n");
|
|
|
|
check_stdio_details("latin-1", NULL);
|
|
|
|
printf("--- Set encoding and errors ---\n");
|
2014-03-18 02:38:12 +01:00
|
|
|
check_stdio_details("latin-1", "replace");
|
2013-10-17 14:35:35 +02: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 05:25:03 +01:00
|
|
|
return 0;
|
2013-10-17 14:35:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 05:25:03 +01:00
|
|
|
/* *********************************************************
|
|
|
|
* List of test cases and the function that implements it.
|
2017-09-14 08:38:36 +02:00
|
|
|
*
|
2017-01-02 05:25:03 +01: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 08:38:36 +02:00
|
|
|
* failed test.
|
2017-01-02 05:25:03 +01:00
|
|
|
*********************************************************/
|
|
|
|
struct TestCase
|
2013-10-17 14:35:35 +02:00
|
|
|
{
|
2017-01-02 05:25:03 +01:00
|
|
|
const char *name;
|
|
|
|
int (*func)(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct TestCase TestCases[] = {
|
|
|
|
{ "forced_io_encoding", test_forced_io_encoding },
|
|
|
|
{ "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2013-10-17 14:35:35 +02:00
|
|
|
|
2017-01-02 05:25:03 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-10-17 14:35:35 +02:00
|
|
|
if (argc > 1) {
|
2017-01-02 05:25:03 +01:00
|
|
|
for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
|
|
|
|
if (strcmp(argv[1], tc->name) == 0)
|
|
|
|
return (*tc->func)();
|
|
|
|
}
|
2013-10-17 14:35:35 +02:00
|
|
|
}
|
2017-01-02 05:25:03 +01:00
|
|
|
|
|
|
|
/* No match found, or no test name provided, so display usage */
|
|
|
|
printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
|
|
|
|
"Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
|
|
|
|
"Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
|
|
|
|
for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
|
|
|
|
printf(" %s\n", tc->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-zero exit code will cause test_capi.py tests to fail.
|
|
|
|
This is intentional. */
|
|
|
|
return -1;
|
2011-04-25 21:21:07 +02:00
|
|
|
}
|