blob: b3d7aa442d1a7fc39a380fd04beffb7e063c6e5d [file] [log] [blame]
Antoine Pitrou8e605772011-04-25 21:21:07 +02001#include <Python.h>
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01002#include "pythread.h"
Eric Snowe3774162017-05-22 19:46:40 -07003#include <inttypes.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +02004#include <stdio.h>
5
Nick Coghlan7d270ee2013-10-17 22:35:35 +10006/*********************************************************
7 * Embedded interpreter tests that need a custom exe
8 *
9 * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
10 *********************************************************/
11
12static void _testembed_Py_Initialize(void)
13{
14 /* HACK: the "./" at front avoids a search along the PATH in
15 Modules/getpath.c */
16 Py_SetProgramName(L"./_testembed");
17 Py_Initialize();
18}
19
20
21/*****************************************************
Martin Panter8f265652016-04-19 04:03:41 +000022 * Test repeated initialisation and subinterpreters
Nick Coghlan7d270ee2013-10-17 22:35:35 +100023 *****************************************************/
24
25static void print_subinterp(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020026{
Eric Snowe3774162017-05-22 19:46:40 -070027 /* Output information about the interpreter in the format
28 expected in Lib/test/test_capi.py (test_subinterps). */
Antoine Pitrou8e605772011-04-25 21:21:07 +020029 PyThreadState *ts = PyThreadState_Get();
Eric Snowe3774162017-05-22 19:46:40 -070030 PyInterpreterState *interp = ts->interp;
31 int64_t id = PyInterpreterState_GetID(interp);
Eric Snowd1c3c132017-05-24 17:19:47 -070032 printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
Eric Snowe3774162017-05-22 19:46:40 -070033 id, (uintptr_t)interp, (uintptr_t)ts);
Antoine Pitrou8e605772011-04-25 21:21:07 +020034 fflush(stdout);
35 PyRun_SimpleString(
36 "import sys;"
37 "print('id(modules) =', id(sys.modules));"
38 "sys.stdout.flush()"
39 );
40}
41
Steve Dowerea74f0c2017-01-01 20:25:03 -080042static int test_repeated_init_and_subinterpreters(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020043{
44 PyThreadState *mainstate, *substate;
45 PyGILState_STATE gilstate;
46 int i, j;
47
Ned Deily939231b2016-08-16 00:17:42 -040048 for (i=0; i<15; i++) {
Antoine Pitrou8e605772011-04-25 21:21:07 +020049 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100050 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020051 mainstate = PyThreadState_Get();
52
53 PyEval_InitThreads();
54 PyEval_ReleaseThread(mainstate);
55
56 gilstate = PyGILState_Ensure();
57 print_subinterp();
58 PyThreadState_Swap(NULL);
59
60 for (j=0; j<3; j++) {
61 substate = Py_NewInterpreter();
62 print_subinterp();
63 Py_EndInterpreter(substate);
64 }
65
66 PyThreadState_Swap(mainstate);
67 print_subinterp();
68 PyGILState_Release(gilstate);
Antoine Pitrou8e605772011-04-25 21:21:07 +020069
70 PyEval_RestoreThread(mainstate);
71 Py_Finalize();
72 }
Steve Dowerea74f0c2017-01-01 20:25:03 -080073 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +100074}
75
76/*****************************************************
77 * Test forcing a particular IO encoding
78 *****************************************************/
79
80static void check_stdio_details(const char *encoding, const char * errors)
81{
82 /* Output info for the test case to check */
83 if (encoding) {
84 printf("Expected encoding: %s\n", encoding);
85 } else {
86 printf("Expected encoding: default\n");
87 }
88 if (errors) {
89 printf("Expected errors: %s\n", errors);
90 } else {
91 printf("Expected errors: default\n");
92 }
93 fflush(stdout);
94 /* Force the given IO encoding */
95 Py_SetStandardStreamEncoding(encoding, errors);
96 _testembed_Py_Initialize();
97 PyRun_SimpleString(
98 "import sys;"
99 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
100 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
101 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
102 "sys.stdout.flush()"
103 );
104 Py_Finalize();
105}
106
Steve Dowerea74f0c2017-01-01 20:25:03 -0800107static int test_forced_io_encoding(void)
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000108{
109 /* Check various combinations */
110 printf("--- Use defaults ---\n");
111 check_stdio_details(NULL, NULL);
112 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100113 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000114 printf("--- Set encoding only ---\n");
115 check_stdio_details("latin-1", NULL);
116 printf("--- Set encoding and errors ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100117 check_stdio_details("latin-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000118
119 /* Check calling after initialization fails */
120 Py_Initialize();
121
122 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
123 printf("Unexpected success calling Py_SetStandardStreamEncoding");
124 }
125 Py_Finalize();
Steve Dowerea74f0c2017-01-01 20:25:03 -0800126 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000127}
128
Victor Stinner9e87e772017-11-24 12:09:24 +0100129/*********************************************************
130 * Test parts of the C-API that work before initialization
131 *********************************************************/
132
133static int test_pre_initialization_api(void)
134{
Nick Coghlan42746092017-11-26 14:19:13 +1000135 /* Leading "./" ensures getpath.c can still find the standard library */
136 wchar_t *program = Py_DecodeLocale("./spam", NULL);
Victor Stinner9e87e772017-11-24 12:09:24 +0100137 if (program == NULL) {
138 fprintf(stderr, "Fatal error: cannot decode program name\n");
139 return 1;
140 }
141 Py_SetProgramName(program);
142
143 Py_Initialize();
144 Py_Finalize();
145
146 PyMem_RawFree(program);
147 return 0;
148}
149
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100150static void bpo20891_thread(void *lockp)
151{
152 PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
153
154 PyGILState_STATE state = PyGILState_Ensure();
155 if (!PyGILState_Check()) {
156 fprintf(stderr, "PyGILState_Check failed!");
157 abort();
158 }
159
160 PyGILState_Release(state);
161
162 PyThread_release_lock(lock);
163
164 PyThread_exit_thread();
165}
166
167static int test_bpo20891(void)
168{
169 /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
170 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
171 call PyEval_InitThreads() for us in this case. */
172 PyThread_type_lock lock = PyThread_allocate_lock();
173 if (!lock) {
174 fprintf(stderr, "PyThread_allocate_lock failed!");
175 return 1;
176 }
177
178 _testembed_Py_Initialize();
179
180 unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
181 if (thrd == PYTHREAD_INVALID_THREAD_ID) {
182 fprintf(stderr, "PyThread_start_new_thread failed!");
183 return 1;
184 }
185 PyThread_acquire_lock(lock, WAIT_LOCK);
186
187 Py_BEGIN_ALLOW_THREADS
188 /* wait until the thread exit */
189 PyThread_acquire_lock(lock, WAIT_LOCK);
190 Py_END_ALLOW_THREADS
191
192 PyThread_free_lock(lock);
193
194 return 0;
195}
196
Victor Stinner9e87e772017-11-24 12:09:24 +0100197
Steve Dowerea74f0c2017-01-01 20:25:03 -0800198/* *********************************************************
199 * List of test cases and the function that implements it.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300200 *
Steve Dowerea74f0c2017-01-01 20:25:03 -0800201 * Names are compared case-sensitively with the first
202 * argument. If no match is found, or no first argument was
203 * provided, the names of all test cases are printed and
204 * the exit code will be -1.
205 *
206 * The int returned from test functions is used as the exit
207 * code, and test_capi treats all non-zero exit codes as a
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300208 * failed test.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800209 *********************************************************/
210struct TestCase
211{
212 const char *name;
213 int (*func)(void);
214};
215
216static struct TestCase TestCases[] = {
217 { "forced_io_encoding", test_forced_io_encoding },
218 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
Victor Stinner9e87e772017-11-24 12:09:24 +0100219 { "pre_initialization_api", test_pre_initialization_api },
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100220 { "bpo20891", test_bpo20891 },
Steve Dowerea74f0c2017-01-01 20:25:03 -0800221 { NULL, NULL }
222};
223
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000224int main(int argc, char *argv[])
225{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000226 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800227 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
228 if (strcmp(argv[1], tc->name) == 0)
229 return (*tc->func)();
230 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000231 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800232
233 /* No match found, or no test name provided, so display usage */
234 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
235 "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
236 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
237 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
238 printf(" %s\n", tc->name);
239 }
240
241 /* Non-zero exit code will cause test_capi.py tests to fail.
242 This is intentional. */
243 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200244}