blob: b198962db3a44a23431e17d1624eb0c258fb8458 [file] [log] [blame]
Antoine Pitrou8e605772011-04-25 21:21:07 +02001#include <Python.h>
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002#include "internal/import.h"
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01003#include "pythread.h"
Eric Snowe3774162017-05-22 19:46:40 -07004#include <inttypes.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +02005#include <stdio.h>
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -07006#include <wchar.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +02007
Nick Coghlan7d270ee2013-10-17 22:35:35 +10008/*********************************************************
9 * Embedded interpreter tests that need a custom exe
10 *
11 * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
12 *********************************************************/
13
14static void _testembed_Py_Initialize(void)
15{
16 /* HACK: the "./" at front avoids a search along the PATH in
17 Modules/getpath.c */
18 Py_SetProgramName(L"./_testembed");
19 Py_Initialize();
20}
21
22
23/*****************************************************
Martin Panter8f265652016-04-19 04:03:41 +000024 * Test repeated initialisation and subinterpreters
Nick Coghlan7d270ee2013-10-17 22:35:35 +100025 *****************************************************/
26
27static void print_subinterp(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020028{
Eric Snowe3774162017-05-22 19:46:40 -070029 /* Output information about the interpreter in the format
30 expected in Lib/test/test_capi.py (test_subinterps). */
Antoine Pitrou8e605772011-04-25 21:21:07 +020031 PyThreadState *ts = PyThreadState_Get();
Eric Snowe3774162017-05-22 19:46:40 -070032 PyInterpreterState *interp = ts->interp;
33 int64_t id = PyInterpreterState_GetID(interp);
Eric Snowd1c3c132017-05-24 17:19:47 -070034 printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
Eric Snowe3774162017-05-22 19:46:40 -070035 id, (uintptr_t)interp, (uintptr_t)ts);
Antoine Pitrou8e605772011-04-25 21:21:07 +020036 fflush(stdout);
37 PyRun_SimpleString(
38 "import sys;"
39 "print('id(modules) =', id(sys.modules));"
40 "sys.stdout.flush()"
41 );
42}
43
Steve Dowerea74f0c2017-01-01 20:25:03 -080044static int test_repeated_init_and_subinterpreters(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020045{
46 PyThreadState *mainstate, *substate;
47 PyGILState_STATE gilstate;
48 int i, j;
49
Ned Deily939231b2016-08-16 00:17:42 -040050 for (i=0; i<15; i++) {
Antoine Pitrou8e605772011-04-25 21:21:07 +020051 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100052 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020053 mainstate = PyThreadState_Get();
54
55 PyEval_InitThreads();
56 PyEval_ReleaseThread(mainstate);
57
58 gilstate = PyGILState_Ensure();
59 print_subinterp();
60 PyThreadState_Swap(NULL);
61
62 for (j=0; j<3; j++) {
63 substate = Py_NewInterpreter();
64 print_subinterp();
65 Py_EndInterpreter(substate);
66 }
67
68 PyThreadState_Swap(mainstate);
69 print_subinterp();
70 PyGILState_Release(gilstate);
Antoine Pitrou8e605772011-04-25 21:21:07 +020071
72 PyEval_RestoreThread(mainstate);
73 Py_Finalize();
74 }
Steve Dowerea74f0c2017-01-01 20:25:03 -080075 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +100076}
77
78/*****************************************************
79 * Test forcing a particular IO encoding
80 *****************************************************/
81
82static void check_stdio_details(const char *encoding, const char * errors)
83{
84 /* Output info for the test case to check */
85 if (encoding) {
86 printf("Expected encoding: %s\n", encoding);
87 } else {
88 printf("Expected encoding: default\n");
89 }
90 if (errors) {
91 printf("Expected errors: %s\n", errors);
92 } else {
93 printf("Expected errors: default\n");
94 }
95 fflush(stdout);
96 /* Force the given IO encoding */
97 Py_SetStandardStreamEncoding(encoding, errors);
98 _testembed_Py_Initialize();
99 PyRun_SimpleString(
100 "import sys;"
101 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
102 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
103 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
104 "sys.stdout.flush()"
105 );
106 Py_Finalize();
107}
108
Steve Dowerea74f0c2017-01-01 20:25:03 -0800109static int test_forced_io_encoding(void)
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000110{
111 /* Check various combinations */
112 printf("--- Use defaults ---\n");
113 check_stdio_details(NULL, NULL);
114 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100115 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000116 printf("--- Set encoding only ---\n");
117 check_stdio_details("latin-1", NULL);
118 printf("--- Set encoding and errors ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100119 check_stdio_details("latin-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000120
121 /* Check calling after initialization fails */
122 Py_Initialize();
123
124 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
125 printf("Unexpected success calling Py_SetStandardStreamEncoding");
126 }
127 Py_Finalize();
Steve Dowerea74f0c2017-01-01 20:25:03 -0800128 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000129}
130
Victor Stinner9e87e772017-11-24 12:09:24 +0100131/*********************************************************
132 * Test parts of the C-API that work before initialization
133 *********************************************************/
134
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700135/* The pre-initialization tests tend to break by segfaulting, so explicitly
136 * flushed progress messages make the broken API easier to find when they fail.
137 */
138#define _Py_EMBED_PREINIT_CHECK(msg) \
139 do {printf(msg); fflush(stdout);} while (0);
140
Victor Stinner9e87e772017-11-24 12:09:24 +0100141static int test_pre_initialization_api(void)
142{
Nick Coghlan42746092017-11-26 14:19:13 +1000143 /* Leading "./" ensures getpath.c can still find the standard library */
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700144 _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
Nick Coghlan42746092017-11-26 14:19:13 +1000145 wchar_t *program = Py_DecodeLocale("./spam", NULL);
Victor Stinner9e87e772017-11-24 12:09:24 +0100146 if (program == NULL) {
147 fprintf(stderr, "Fatal error: cannot decode program name\n");
148 return 1;
149 }
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700150 _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100151 Py_SetProgramName(program);
152
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700153 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100154 Py_Initialize();
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700155 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
156 PyRun_SimpleString("import sys; "
157 "print('sys.executable:', sys.executable)");
158 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100159 Py_Finalize();
160
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700161 _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100162 PyMem_RawFree(program);
163 return 0;
164}
165
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700166
167/* bpo-33042: Ensure embedding apps can predefine sys module options */
168static int test_pre_initialization_sys_options(void)
169{
Miss Islington (bot)29617172018-03-29 23:24:03 -0700170 /* We allocate a couple of the options dynamically, and then delete
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700171 * them before calling Py_Initialize. This ensures the interpreter isn't
172 * relying on the caller to keep the passed in strings alive.
173 */
Miss Islington (bot)29617172018-03-29 23:24:03 -0700174 const wchar_t *static_warnoption = L"once";
175 const wchar_t *static_xoption = L"also_not_an_option=2";
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700176 size_t warnoption_len = wcslen(static_warnoption);
177 size_t xoption_len = wcslen(static_xoption);
Miss Islington (bot)29617172018-03-29 23:24:03 -0700178 wchar_t *dynamic_once_warnoption = \
179 (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
180 wchar_t *dynamic_xoption = \
181 (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700182 wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
183 wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);
184
185 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
186 PySys_AddWarnOption(L"default");
187 _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
188 PySys_ResetWarnOptions();
189 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
190 PySys_AddWarnOption(dynamic_once_warnoption);
191 PySys_AddWarnOption(L"module");
192 PySys_AddWarnOption(L"default");
193 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
194 PySys_AddXOption(L"not_an_option=1");
195 PySys_AddXOption(dynamic_xoption);
196
197 /* Delete the dynamic options early */
198 free(dynamic_once_warnoption);
199 dynamic_once_warnoption = NULL;
200 free(dynamic_xoption);
201 dynamic_xoption = NULL;
202
203 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
204 _testembed_Py_Initialize();
205 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
206 PyRun_SimpleString("import sys; "
207 "print('sys.warnoptions:', sys.warnoptions); "
208 "print('sys._xoptions:', sys._xoptions); "
209 "warnings = sys.modules['warnings']; "
210 "latest_filters = [f[0] for f in warnings.filters[:3]]; "
211 "print('warnings.filters[:3]:', latest_filters)");
212 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
213 Py_Finalize();
214
215 return 0;
216}
217
218
219/* bpo-20891: Avoid race condition when initialising the GIL */
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100220static void bpo20891_thread(void *lockp)
221{
222 PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
223
224 PyGILState_STATE state = PyGILState_Ensure();
225 if (!PyGILState_Check()) {
226 fprintf(stderr, "PyGILState_Check failed!");
227 abort();
228 }
229
230 PyGILState_Release(state);
231
232 PyThread_release_lock(lock);
233
234 PyThread_exit_thread();
235}
236
237static int test_bpo20891(void)
238{
239 /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
240 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
241 call PyEval_InitThreads() for us in this case. */
242 PyThread_type_lock lock = PyThread_allocate_lock();
243 if (!lock) {
244 fprintf(stderr, "PyThread_allocate_lock failed!");
245 return 1;
246 }
247
248 _testembed_Py_Initialize();
249
250 unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
251 if (thrd == PYTHREAD_INVALID_THREAD_ID) {
252 fprintf(stderr, "PyThread_start_new_thread failed!");
253 return 1;
254 }
255 PyThread_acquire_lock(lock, WAIT_LOCK);
256
257 Py_BEGIN_ALLOW_THREADS
258 /* wait until the thread exit */
259 PyThread_acquire_lock(lock, WAIT_LOCK);
260 Py_END_ALLOW_THREADS
261
262 PyThread_free_lock(lock);
263
264 return 0;
265}
266
Miss Islington (bot)3747dd12018-06-22 10:33:48 -0700267static int test_initialize_twice(void)
268{
269 _testembed_Py_Initialize();
270
271 /* bpo-33932: Calling Py_Initialize() twice should do nothing
272 * (and not crash!). */
273 Py_Initialize();
274
275 Py_Finalize();
276
277 return 0;
278}
279
Miss Islington (bot)03ec4df2018-07-20 17:16:22 -0700280static int test_initialize_pymain(void)
281{
282 wchar_t *argv[] = {L"PYTHON", L"-c",
283 L"import sys; print(f'Py_Main() after Py_Initialize: sys.argv={sys.argv}')",
284 L"arg2"};
285 _testembed_Py_Initialize();
286
287 /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
288 Py_Main(Py_ARRAY_LENGTH(argv), argv);
289
290 Py_Finalize();
291
292 return 0;
293}
294
Victor Stinner9e87e772017-11-24 12:09:24 +0100295
Victor Stinner35c28d52018-11-14 02:01:52 +0100296static int
297dump_config_impl(void)
298{
299 PyObject *config = NULL;
300 PyObject *dict = NULL;
301
302 config = PyDict_New();
303 if (config == NULL) {
304 goto error;
305 }
306
307 /* global config */
308 dict = _Py_GetGlobalVariablesAsDict();
309 if (dict == NULL) {
310 goto error;
311 }
312 if (PyDict_SetItemString(config, "global_config", dict) < 0) {
313 goto error;
314 }
315 Py_CLEAR(dict);
316
317 /* core config */
318 PyInterpreterState *interp = PyThreadState_Get()->interp;
319 const _PyCoreConfig *core_config = &interp->core_config;
320 dict = _PyCoreConfig_AsDict(core_config);
321 if (dict == NULL) {
322 goto error;
323 }
324 if (PyDict_SetItemString(config, "core_config", dict) < 0) {
325 goto error;
326 }
327 Py_CLEAR(dict);
328
329 /* main config */
330 const _PyMainInterpreterConfig *main_config = &interp->config;
331 dict = _PyMainInterpreterConfig_AsDict(main_config);
332 if (dict == NULL) {
333 goto error;
334 }
335 if (PyDict_SetItemString(config, "main_config", dict) < 0) {
336 goto error;
337 }
338 Py_CLEAR(dict);
339
340 PyObject *json = PyImport_ImportModule("json");
341 PyObject *res = PyObject_CallMethod(json, "dumps", "O", config);
342 Py_DECREF(json);
343 Py_CLEAR(config);
344 if (res == NULL) {
345 goto error;
346 }
347
348 PySys_FormatStdout("%S\n", res);
349 Py_DECREF(res);
350
351 return 0;
352
353error:
354 Py_XDECREF(config);
355 Py_XDECREF(dict);
356 return -1;
357}
358
359
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200360static void
361dump_config(void)
362{
Victor Stinner35c28d52018-11-14 02:01:52 +0100363 if (dump_config_impl() < 0) {
364 fprintf(stderr, "failed to dump the configuration:\n");
365 PyErr_Print();
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200366 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200367}
368
369
370static int test_init_default_config(void)
371{
372 _testembed_Py_Initialize();
373 dump_config();
374 Py_Finalize();
375 return 0;
376}
377
378
379static int test_init_global_config(void)
380{
381 /* FIXME: test Py_IgnoreEnvironmentFlag */
382
383 putenv("PYTHONUTF8=0");
384 Py_UTF8Mode = 1;
385
386 /* Test initialization from global configuration variables (Py_xxx) */
387 Py_SetProgramName(L"./globalvar");
388
389 /* Py_IsolatedFlag is not tested */
390 Py_NoSiteFlag = 1;
391 Py_BytesWarningFlag = 1;
392
393 putenv("PYTHONINSPECT=");
394 Py_InspectFlag = 1;
395
396 putenv("PYTHONOPTIMIZE=0");
397 Py_InteractiveFlag = 1;
398
399 putenv("PYTHONDEBUG=0");
400 Py_OptimizeFlag = 2;
401
402 /* Py_DebugFlag is not tested */
403
404 putenv("PYTHONDONTWRITEBYTECODE=");
405 Py_DontWriteBytecodeFlag = 1;
406
407 putenv("PYTHONVERBOSE=0");
408 Py_VerboseFlag = 1;
409
410 Py_QuietFlag = 1;
411 Py_NoUserSiteDirectory = 1;
412
413 putenv("PYTHONUNBUFFERED=");
414 Py_UnbufferedStdioFlag = 1;
415
416 Py_FrozenFlag = 1;
417
418 /* FIXME: test Py_LegacyWindowsFSEncodingFlag */
419 /* FIXME: test Py_LegacyWindowsStdioFlag */
420
421 Py_Initialize();
422 dump_config();
423 Py_Finalize();
424 return 0;
425}
426
427
428static int test_init_from_config(void)
429{
430 /* Test _Py_InitializeFromConfig() */
431 _PyCoreConfig config = _PyCoreConfig_INIT;
432 config.install_signal_handlers = 0;
433
434 /* FIXME: test ignore_environment */
435
436 putenv("PYTHONHASHSEED=42");
437 config.use_hash_seed = 1;
438 config.hash_seed = 123;
439
440 putenv("PYTHONMALLOC=malloc");
441 config.allocator = "malloc_debug";
442
443 /* dev_mode=1 is tested in test_init_dev_mode() */
444
445 putenv("PYTHONFAULTHANDLER=");
446 config.faulthandler = 1;
447
448 putenv("PYTHONTRACEMALLOC=0");
449 config.tracemalloc = 2;
450
451 putenv("PYTHONPROFILEIMPORTTIME=0");
452 config.import_time = 1;
453
454 config.show_ref_count = 1;
455 config.show_alloc_count = 1;
456 /* FIXME: test dump_refs: bpo-34223 */
457
458 putenv("PYTHONMALLOCSTATS=0");
459 config.malloc_stats = 1;
460
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700461 /* FIXME: test coerce_c_locale and coerce_c_locale_warn */
462
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200463 putenv("PYTHONUTF8=0");
464 Py_UTF8Mode = 0;
465 config.utf8_mode = 1;
466
467 Py_SetProgramName(L"./globalvar");
468 config.program_name = L"./conf_program_name";
469
470 /* FIXME: test argc/argv */
471 config.program = L"conf_program";
472 /* FIXME: test xoptions */
473 /* FIXME: test warnoptions */
474 /* FIXME: test module_search_path_env */
475 /* FIXME: test home */
476 /* FIXME: test path config: module_search_path .. dll_path */
477
478 _PyInitError err = _Py_InitializeFromConfig(&config);
479 /* Don't call _PyCoreConfig_Clear() since all strings are static */
480 if (_Py_INIT_FAILED(err)) {
481 _Py_FatalInitError(err);
482 }
483 dump_config();
484 Py_Finalize();
485 return 0;
486}
487
488
489static void test_init_env_putenvs(void)
490{
491 putenv("PYTHONHASHSEED=42");
492 putenv("PYTHONMALLOC=malloc_debug");
493 putenv("PYTHONTRACEMALLOC=2");
494 putenv("PYTHONPROFILEIMPORTTIME=1");
495 putenv("PYTHONMALLOCSTATS=1");
496 putenv("PYTHONUTF8=1");
497 putenv("PYTHONVERBOSE=1");
498 putenv("PYTHONINSPECT=1");
499 putenv("PYTHONOPTIMIZE=2");
500 putenv("PYTHONDONTWRITEBYTECODE=1");
501 putenv("PYTHONUNBUFFERED=1");
502 putenv("PYTHONNOUSERSITE=1");
503 putenv("PYTHONFAULTHANDLER=1");
504 putenv("PYTHONDEVMODE=1");
505 /* FIXME: test PYTHONWARNINGS */
506 /* FIXME: test PYTHONEXECUTABLE */
507 /* FIXME: test PYTHONHOME */
508 /* FIXME: test PYTHONDEBUG */
509 /* FIXME: test PYTHONDUMPREFS */
510 /* FIXME: test PYTHONCOERCECLOCALE */
511 /* FIXME: test PYTHONPATH */
512}
513
514
515static int test_init_env(void)
516{
517 /* Test initialization from environment variables */
518 Py_IgnoreEnvironmentFlag = 0;
519 test_init_env_putenvs();
520 _testembed_Py_Initialize();
521 dump_config();
522 Py_Finalize();
523 return 0;
524}
525
526
527static int test_init_isolated(void)
528{
529 /* Test _PyCoreConfig.isolated=1 */
530 _PyCoreConfig config = _PyCoreConfig_INIT;
531
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700532 /* Set coerce_c_locale and utf8_mode to not depend on the locale */
533 config.coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200534 config.utf8_mode = 0;
535 /* Use path starting with "./" avoids a search along the PATH */
536 config.program_name = L"./_testembed";
537
538 Py_IsolatedFlag = 1;
539
540 test_init_env_putenvs();
541 _PyInitError err = _Py_InitializeFromConfig(&config);
542 if (_Py_INIT_FAILED(err)) {
543 _Py_FatalInitError(err);
544 }
545 dump_config();
546 Py_Finalize();
547 return 0;
548}
549
550
551static int test_init_dev_mode(void)
552{
553 _PyCoreConfig config = _PyCoreConfig_INIT;
554 putenv("PYTHONFAULTHANDLER=");
555 putenv("PYTHONMALLOC=");
556 config.dev_mode = 1;
557 config.program_name = L"./_testembed";
558 _PyInitError err = _Py_InitializeFromConfig(&config);
559 if (_Py_INIT_FAILED(err)) {
560 _Py_FatalInitError(err);
561 }
562 dump_config();
563 Py_Finalize();
564 return 0;
565}
566
567
Steve Dowerea74f0c2017-01-01 20:25:03 -0800568/* *********************************************************
569 * List of test cases and the function that implements it.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300570 *
Steve Dowerea74f0c2017-01-01 20:25:03 -0800571 * Names are compared case-sensitively with the first
572 * argument. If no match is found, or no first argument was
573 * provided, the names of all test cases are printed and
574 * the exit code will be -1.
575 *
576 * The int returned from test functions is used as the exit
577 * code, and test_capi treats all non-zero exit codes as a
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300578 * failed test.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800579 *********************************************************/
580struct TestCase
581{
582 const char *name;
583 int (*func)(void);
584};
585
586static struct TestCase TestCases[] = {
587 { "forced_io_encoding", test_forced_io_encoding },
588 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
Victor Stinner9e87e772017-11-24 12:09:24 +0100589 { "pre_initialization_api", test_pre_initialization_api },
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700590 { "pre_initialization_sys_options", test_pre_initialization_sys_options },
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100591 { "bpo20891", test_bpo20891 },
Miss Islington (bot)3747dd12018-06-22 10:33:48 -0700592 { "initialize_twice", test_initialize_twice },
Miss Islington (bot)03ec4df2018-07-20 17:16:22 -0700593 { "initialize_pymain", test_initialize_pymain },
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200594 { "init_default_config", test_init_default_config },
595 { "init_global_config", test_init_global_config },
596 { "init_from_config", test_init_from_config },
597 { "init_env", test_init_env },
598 { "init_dev_mode", test_init_dev_mode },
599 { "init_isolated", test_init_isolated },
Steve Dowerea74f0c2017-01-01 20:25:03 -0800600 { NULL, NULL }
601};
602
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000603int main(int argc, char *argv[])
604{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000605 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800606 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
607 if (strcmp(argv[1], tc->name) == 0)
608 return (*tc->func)();
609 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000610 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800611
612 /* No match found, or no test name provided, so display usage */
613 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700614 "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
Steve Dowerea74f0c2017-01-01 20:25:03 -0800615 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
616 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
617 printf(" %s\n", tc->name);
618 }
619
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700620 /* Non-zero exit code will cause test_embed.py tests to fail.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800621 This is intentional. */
622 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200623}