blob: c7660f95819ca8ef1b86e11722c037a0366ace60 [file] [log] [blame]
Antoine Pitrou8e605772011-04-25 21:21:07 +02001#include <Python.h>
Eric Snowe3774162017-05-22 19:46:40 -07002#include <inttypes.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +02003#include <stdio.h>
4
Nick Coghlan7d270ee2013-10-17 22:35:35 +10005/*********************************************************
6 * Embedded interpreter tests that need a custom exe
7 *
8 * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
9 *********************************************************/
10
11static void _testembed_Py_Initialize(void)
12{
13 /* HACK: the "./" at front avoids a search along the PATH in
14 Modules/getpath.c */
15 Py_SetProgramName(L"./_testembed");
16 Py_Initialize();
17}
18
19
20/*****************************************************
Martin Panter8f265652016-04-19 04:03:41 +000021 * Test repeated initialisation and subinterpreters
Nick Coghlan7d270ee2013-10-17 22:35:35 +100022 *****************************************************/
23
24static void print_subinterp(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020025{
Eric Snowe3774162017-05-22 19:46:40 -070026 /* Output information about the interpreter in the format
27 expected in Lib/test/test_capi.py (test_subinterps). */
Antoine Pitrou8e605772011-04-25 21:21:07 +020028 PyThreadState *ts = PyThreadState_Get();
Eric Snowe3774162017-05-22 19:46:40 -070029 PyInterpreterState *interp = ts->interp;
30 int64_t id = PyInterpreterState_GetID(interp);
Eric Snowd1c3c132017-05-24 17:19:47 -070031 printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
Eric Snowe3774162017-05-22 19:46:40 -070032 id, (uintptr_t)interp, (uintptr_t)ts);
Antoine Pitrou8e605772011-04-25 21:21:07 +020033 fflush(stdout);
34 PyRun_SimpleString(
35 "import sys;"
36 "print('id(modules) =', id(sys.modules));"
37 "sys.stdout.flush()"
38 );
39}
40
Steve Dowerea74f0c2017-01-01 20:25:03 -080041static int test_repeated_init_and_subinterpreters(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020042{
43 PyThreadState *mainstate, *substate;
Victor Stinner66299a42011-04-26 23:37:02 +020044#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020045 PyGILState_STATE gilstate;
Victor Stinner66299a42011-04-26 23:37:02 +020046#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020047 int i, j;
48
Ned Deily939231b2016-08-16 00:17:42 -040049 for (i=0; i<15; i++) {
Antoine Pitrou8e605772011-04-25 21:21:07 +020050 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100051 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020052 mainstate = PyThreadState_Get();
53
Victor Stinner66299a42011-04-26 23:37:02 +020054#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020055 PyEval_InitThreads();
56 PyEval_ReleaseThread(mainstate);
57
58 gilstate = PyGILState_Ensure();
Victor Stinner66299a42011-04-26 23:37:02 +020059#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020060 print_subinterp();
61 PyThreadState_Swap(NULL);
62
63 for (j=0; j<3; j++) {
64 substate = Py_NewInterpreter();
65 print_subinterp();
66 Py_EndInterpreter(substate);
67 }
68
69 PyThreadState_Swap(mainstate);
70 print_subinterp();
Victor Stinner66299a42011-04-26 23:37:02 +020071#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020072 PyGILState_Release(gilstate);
Victor Stinner66299a42011-04-26 23:37:02 +020073#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020074
75 PyEval_RestoreThread(mainstate);
76 Py_Finalize();
77 }
Steve Dowerea74f0c2017-01-01 20:25:03 -080078 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +100079}
80
81/*****************************************************
82 * Test forcing a particular IO encoding
83 *****************************************************/
84
85static void check_stdio_details(const char *encoding, const char * errors)
86{
87 /* Output info for the test case to check */
88 if (encoding) {
89 printf("Expected encoding: %s\n", encoding);
90 } else {
91 printf("Expected encoding: default\n");
92 }
93 if (errors) {
94 printf("Expected errors: %s\n", errors);
95 } else {
96 printf("Expected errors: default\n");
97 }
98 fflush(stdout);
99 /* Force the given IO encoding */
100 Py_SetStandardStreamEncoding(encoding, errors);
101 _testembed_Py_Initialize();
102 PyRun_SimpleString(
103 "import sys;"
104 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
105 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
106 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
107 "sys.stdout.flush()"
108 );
109 Py_Finalize();
110}
111
Steve Dowerea74f0c2017-01-01 20:25:03 -0800112static int test_forced_io_encoding(void)
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000113{
114 /* Check various combinations */
115 printf("--- Use defaults ---\n");
116 check_stdio_details(NULL, NULL);
117 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100118 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000119 printf("--- Set encoding only ---\n");
120 check_stdio_details("latin-1", NULL);
121 printf("--- Set encoding and errors ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100122 check_stdio_details("latin-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000123
124 /* Check calling after initialization fails */
125 Py_Initialize();
126
127 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
128 printf("Unexpected success calling Py_SetStandardStreamEncoding");
129 }
130 Py_Finalize();
Steve Dowerea74f0c2017-01-01 20:25:03 -0800131 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000132}
133
Steve Dowerea74f0c2017-01-01 20:25:03 -0800134/* *********************************************************
135 * List of test cases and the function that implements it.
136 *
137 * Names are compared case-sensitively with the first
138 * argument. If no match is found, or no first argument was
139 * provided, the names of all test cases are printed and
140 * the exit code will be -1.
141 *
142 * The int returned from test functions is used as the exit
143 * code, and test_capi treats all non-zero exit codes as a
144 * failed test.
145 *********************************************************/
146struct TestCase
147{
148 const char *name;
149 int (*func)(void);
150};
151
152static struct TestCase TestCases[] = {
153 { "forced_io_encoding", test_forced_io_encoding },
154 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
155 { NULL, NULL }
156};
157
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000158int main(int argc, char *argv[])
159{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000160 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800161 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
162 if (strcmp(argv[1], tc->name) == 0)
163 return (*tc->func)();
164 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000165 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800166
167 /* No match found, or no test name provided, so display usage */
168 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
169 "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
170 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
171 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
172 printf(" %s\n", tc->name);
173 }
174
175 /* Non-zero exit code will cause test_capi.py tests to fail.
176 This is intentional. */
177 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200178}