blob: 52a0b5124a37f06f8ef388d488c816649489952c [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;
44 PyGILState_STATE gilstate;
45 int i, j;
46
Ned Deily939231b2016-08-16 00:17:42 -040047 for (i=0; i<15; i++) {
Antoine Pitrou8e605772011-04-25 21:21:07 +020048 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100049 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020050 mainstate = PyThreadState_Get();
51
52 PyEval_InitThreads();
53 PyEval_ReleaseThread(mainstate);
54
55 gilstate = PyGILState_Ensure();
56 print_subinterp();
57 PyThreadState_Swap(NULL);
58
59 for (j=0; j<3; j++) {
60 substate = Py_NewInterpreter();
61 print_subinterp();
62 Py_EndInterpreter(substate);
63 }
64
65 PyThreadState_Swap(mainstate);
66 print_subinterp();
67 PyGILState_Release(gilstate);
Antoine Pitrou8e605772011-04-25 21:21:07 +020068
69 PyEval_RestoreThread(mainstate);
70 Py_Finalize();
71 }
Steve Dowerea74f0c2017-01-01 20:25:03 -080072 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +100073}
74
75/*****************************************************
76 * Test forcing a particular IO encoding
77 *****************************************************/
78
79static void check_stdio_details(const char *encoding, const char * errors)
80{
81 /* Output info for the test case to check */
82 if (encoding) {
83 printf("Expected encoding: %s\n", encoding);
84 } else {
85 printf("Expected encoding: default\n");
86 }
87 if (errors) {
88 printf("Expected errors: %s\n", errors);
89 } else {
90 printf("Expected errors: default\n");
91 }
92 fflush(stdout);
93 /* Force the given IO encoding */
94 Py_SetStandardStreamEncoding(encoding, errors);
95 _testembed_Py_Initialize();
96 PyRun_SimpleString(
97 "import sys;"
98 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
99 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
100 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
101 "sys.stdout.flush()"
102 );
103 Py_Finalize();
104}
105
Steve Dowerea74f0c2017-01-01 20:25:03 -0800106static int test_forced_io_encoding(void)
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000107{
108 /* Check various combinations */
109 printf("--- Use defaults ---\n");
110 check_stdio_details(NULL, NULL);
111 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100112 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000113 printf("--- Set encoding only ---\n");
114 check_stdio_details("latin-1", NULL);
115 printf("--- Set encoding and errors ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100116 check_stdio_details("latin-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000117
118 /* Check calling after initialization fails */
119 Py_Initialize();
120
121 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
122 printf("Unexpected success calling Py_SetStandardStreamEncoding");
123 }
124 Py_Finalize();
Steve Dowerea74f0c2017-01-01 20:25:03 -0800125 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000126}
127
Victor Stinner9e87e772017-11-24 12:09:24 +0100128
129/*********************************************************
130 * Test parts of the C-API that work before initialization
131 *********************************************************/
132
133static int test_pre_initialization_api(void)
134{
135 wchar_t *program = Py_DecodeLocale("spam", NULL);
136 if (program == NULL) {
137 fprintf(stderr, "Fatal error: cannot decode program name\n");
138 return 1;
139 }
140 Py_SetProgramName(program);
141
142 Py_Initialize();
143 Py_Finalize();
144
145 PyMem_RawFree(program);
146 return 0;
147}
148
149
Steve Dowerea74f0c2017-01-01 20:25:03 -0800150/* *********************************************************
151 * List of test cases and the function that implements it.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300152 *
Steve Dowerea74f0c2017-01-01 20:25:03 -0800153 * Names are compared case-sensitively with the first
154 * argument. If no match is found, or no first argument was
155 * provided, the names of all test cases are printed and
156 * the exit code will be -1.
157 *
158 * The int returned from test functions is used as the exit
159 * code, and test_capi treats all non-zero exit codes as a
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300160 * failed test.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800161 *********************************************************/
162struct TestCase
163{
164 const char *name;
165 int (*func)(void);
166};
167
168static struct TestCase TestCases[] = {
169 { "forced_io_encoding", test_forced_io_encoding },
170 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
Victor Stinner9e87e772017-11-24 12:09:24 +0100171 { "pre_initialization_api", test_pre_initialization_api },
Steve Dowerea74f0c2017-01-01 20:25:03 -0800172 { NULL, NULL }
173};
174
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000175int main(int argc, char *argv[])
176{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000177 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800178 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
179 if (strcmp(argv[1], tc->name) == 0)
180 return (*tc->func)();
181 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000182 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800183
184 /* No match found, or no test name provided, so display usage */
185 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
186 "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
187 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
188 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
189 printf(" %s\n", tc->name);
190 }
191
192 /* Non-zero exit code will cause test_capi.py tests to fail.
193 This is intentional. */
194 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200195}