blob: 6c35f9586bfe18029b5040be1e327d210bac6f9e [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 Stinner0c90d6f2018-08-05 12:31:59 +0200296static void
297dump_config(void)
298{
299#define ASSERT_EQUAL(a, b) \
300 if ((a) != (b)) { \
301 printf("ERROR: %s != %s (%i != %i)\n", #a, #b, (a), (b)); \
302 exit(1); \
303 }
304#define ASSERT_STR_EQUAL(a, b) \
305 if ((a) == NULL || (b == NULL) || wcscmp((a), (b)) != 0) { \
306 printf("ERROR: %s != %s ('%ls' != '%ls')\n", #a, #b, (a), (b)); \
307 exit(1); \
308 }
309
310 PyInterpreterState *interp = PyThreadState_Get()->interp;
311 _PyCoreConfig *config = &interp->core_config;
312
313 printf("install_signal_handlers = %i\n", config->install_signal_handlers);
314
315 printf("Py_IgnoreEnvironmentFlag = %i\n", Py_IgnoreEnvironmentFlag);
316
317 printf("use_hash_seed = %i\n", config->use_hash_seed);
318 printf("hash_seed = %lu\n", config->hash_seed);
319
320 printf("allocator = %s\n", config->allocator);
321
322 printf("dev_mode = %i\n", config->dev_mode);
323 printf("faulthandler = %i\n", config->faulthandler);
324 printf("tracemalloc = %i\n", config->tracemalloc);
325 printf("import_time = %i\n", config->import_time);
326 printf("show_ref_count = %i\n", config->show_ref_count);
327 printf("show_alloc_count = %i\n", config->show_alloc_count);
328 printf("dump_refs = %i\n", config->dump_refs);
329 printf("malloc_stats = %i\n", config->malloc_stats);
330
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700331 printf("coerce_c_locale = %i\n", config->coerce_c_locale);
332 printf("coerce_c_locale_warn = %i\n", config->coerce_c_locale_warn);
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200333 printf("utf8_mode = %i\n", config->utf8_mode);
334
335 printf("program_name = %ls\n", config->program_name);
336 ASSERT_STR_EQUAL(config->program_name, Py_GetProgramName());
337
338 printf("argc = %i\n", config->argc);
339 printf("argv = [");
340 for (int i=0; i < config->argc; i++) {
341 if (i) {
342 printf(", ");
343 }
344 printf("\"%ls\"", config->argv[i]);
345 }
346 printf("]\n");
347
348 printf("program = %ls\n", config->program);
349 /* FIXME: test xoptions */
350 /* FIXME: test warnoptions */
351 /* FIXME: test module_search_path_env */
352 /* FIXME: test home */
353 /* FIXME: test module_search_paths */
354 /* FIXME: test executable */
355 /* FIXME: test prefix */
356 /* FIXME: test base_prefix */
357 /* FIXME: test exec_prefix */
358 /* FIXME: test base_exec_prefix */
359 /* FIXME: test dll_path */
360
361 printf("Py_IsolatedFlag = %i\n", Py_IsolatedFlag);
362 printf("Py_NoSiteFlag = %i\n", Py_NoSiteFlag);
363 printf("Py_BytesWarningFlag = %i\n", Py_BytesWarningFlag);
364 printf("Py_InspectFlag = %i\n", Py_InspectFlag);
365 printf("Py_InteractiveFlag = %i\n", Py_InteractiveFlag);
366 printf("Py_OptimizeFlag = %i\n", Py_OptimizeFlag);
367 printf("Py_DebugFlag = %i\n", Py_DebugFlag);
368 printf("Py_DontWriteBytecodeFlag = %i\n", Py_DontWriteBytecodeFlag);
369 printf("Py_VerboseFlag = %i\n", Py_VerboseFlag);
370 printf("Py_QuietFlag = %i\n", Py_QuietFlag);
371 printf("Py_NoUserSiteDirectory = %i\n", Py_NoUserSiteDirectory);
372 printf("Py_UnbufferedStdioFlag = %i\n", Py_UnbufferedStdioFlag);
373 /* FIXME: test legacy_windows_fs_encoding */
374 /* FIXME: test legacy_windows_stdio */
375
376 printf("_disable_importlib = %i\n", config->_disable_importlib);
377 /* cannot test _Py_CheckHashBasedPycsMode: the symbol is not exported */
378 printf("Py_FrozenFlag = %i\n", Py_FrozenFlag);
379
380#undef ASSERT_EQUAL
381#undef ASSERT_STR_EQUAL
382}
383
384
385static int test_init_default_config(void)
386{
387 _testembed_Py_Initialize();
388 dump_config();
389 Py_Finalize();
390 return 0;
391}
392
393
394static int test_init_global_config(void)
395{
396 /* FIXME: test Py_IgnoreEnvironmentFlag */
397
398 putenv("PYTHONUTF8=0");
399 Py_UTF8Mode = 1;
400
401 /* Test initialization from global configuration variables (Py_xxx) */
402 Py_SetProgramName(L"./globalvar");
403
404 /* Py_IsolatedFlag is not tested */
405 Py_NoSiteFlag = 1;
406 Py_BytesWarningFlag = 1;
407
408 putenv("PYTHONINSPECT=");
409 Py_InspectFlag = 1;
410
411 putenv("PYTHONOPTIMIZE=0");
412 Py_InteractiveFlag = 1;
413
414 putenv("PYTHONDEBUG=0");
415 Py_OptimizeFlag = 2;
416
417 /* Py_DebugFlag is not tested */
418
419 putenv("PYTHONDONTWRITEBYTECODE=");
420 Py_DontWriteBytecodeFlag = 1;
421
422 putenv("PYTHONVERBOSE=0");
423 Py_VerboseFlag = 1;
424
425 Py_QuietFlag = 1;
426 Py_NoUserSiteDirectory = 1;
427
428 putenv("PYTHONUNBUFFERED=");
429 Py_UnbufferedStdioFlag = 1;
430
431 Py_FrozenFlag = 1;
432
433 /* FIXME: test Py_LegacyWindowsFSEncodingFlag */
434 /* FIXME: test Py_LegacyWindowsStdioFlag */
435
436 Py_Initialize();
437 dump_config();
438 Py_Finalize();
439 return 0;
440}
441
442
443static int test_init_from_config(void)
444{
445 /* Test _Py_InitializeFromConfig() */
446 _PyCoreConfig config = _PyCoreConfig_INIT;
447 config.install_signal_handlers = 0;
448
449 /* FIXME: test ignore_environment */
450
451 putenv("PYTHONHASHSEED=42");
452 config.use_hash_seed = 1;
453 config.hash_seed = 123;
454
455 putenv("PYTHONMALLOC=malloc");
456 config.allocator = "malloc_debug";
457
458 /* dev_mode=1 is tested in test_init_dev_mode() */
459
460 putenv("PYTHONFAULTHANDLER=");
461 config.faulthandler = 1;
462
463 putenv("PYTHONTRACEMALLOC=0");
464 config.tracemalloc = 2;
465
466 putenv("PYTHONPROFILEIMPORTTIME=0");
467 config.import_time = 1;
468
469 config.show_ref_count = 1;
470 config.show_alloc_count = 1;
471 /* FIXME: test dump_refs: bpo-34223 */
472
473 putenv("PYTHONMALLOCSTATS=0");
474 config.malloc_stats = 1;
475
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700476 /* FIXME: test coerce_c_locale and coerce_c_locale_warn */
477
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200478 putenv("PYTHONUTF8=0");
479 Py_UTF8Mode = 0;
480 config.utf8_mode = 1;
481
482 Py_SetProgramName(L"./globalvar");
483 config.program_name = L"./conf_program_name";
484
485 /* FIXME: test argc/argv */
486 config.program = L"conf_program";
487 /* FIXME: test xoptions */
488 /* FIXME: test warnoptions */
489 /* FIXME: test module_search_path_env */
490 /* FIXME: test home */
491 /* FIXME: test path config: module_search_path .. dll_path */
492
493 _PyInitError err = _Py_InitializeFromConfig(&config);
494 /* Don't call _PyCoreConfig_Clear() since all strings are static */
495 if (_Py_INIT_FAILED(err)) {
496 _Py_FatalInitError(err);
497 }
498 dump_config();
499 Py_Finalize();
500 return 0;
501}
502
503
504static void test_init_env_putenvs(void)
505{
506 putenv("PYTHONHASHSEED=42");
507 putenv("PYTHONMALLOC=malloc_debug");
508 putenv("PYTHONTRACEMALLOC=2");
509 putenv("PYTHONPROFILEIMPORTTIME=1");
510 putenv("PYTHONMALLOCSTATS=1");
511 putenv("PYTHONUTF8=1");
512 putenv("PYTHONVERBOSE=1");
513 putenv("PYTHONINSPECT=1");
514 putenv("PYTHONOPTIMIZE=2");
515 putenv("PYTHONDONTWRITEBYTECODE=1");
516 putenv("PYTHONUNBUFFERED=1");
517 putenv("PYTHONNOUSERSITE=1");
518 putenv("PYTHONFAULTHANDLER=1");
519 putenv("PYTHONDEVMODE=1");
520 /* FIXME: test PYTHONWARNINGS */
521 /* FIXME: test PYTHONEXECUTABLE */
522 /* FIXME: test PYTHONHOME */
523 /* FIXME: test PYTHONDEBUG */
524 /* FIXME: test PYTHONDUMPREFS */
525 /* FIXME: test PYTHONCOERCECLOCALE */
526 /* FIXME: test PYTHONPATH */
527}
528
529
530static int test_init_env(void)
531{
532 /* Test initialization from environment variables */
533 Py_IgnoreEnvironmentFlag = 0;
534 test_init_env_putenvs();
535 _testembed_Py_Initialize();
536 dump_config();
537 Py_Finalize();
538 return 0;
539}
540
541
542static int test_init_isolated(void)
543{
544 /* Test _PyCoreConfig.isolated=1 */
545 _PyCoreConfig config = _PyCoreConfig_INIT;
546
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700547 /* Set coerce_c_locale and utf8_mode to not depend on the locale */
548 config.coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200549 config.utf8_mode = 0;
550 /* Use path starting with "./" avoids a search along the PATH */
551 config.program_name = L"./_testembed";
552
553 Py_IsolatedFlag = 1;
554
555 test_init_env_putenvs();
556 _PyInitError err = _Py_InitializeFromConfig(&config);
557 if (_Py_INIT_FAILED(err)) {
558 _Py_FatalInitError(err);
559 }
560 dump_config();
561 Py_Finalize();
562 return 0;
563}
564
565
566static int test_init_dev_mode(void)
567{
568 _PyCoreConfig config = _PyCoreConfig_INIT;
569 putenv("PYTHONFAULTHANDLER=");
570 putenv("PYTHONMALLOC=");
571 config.dev_mode = 1;
572 config.program_name = L"./_testembed";
573 _PyInitError err = _Py_InitializeFromConfig(&config);
574 if (_Py_INIT_FAILED(err)) {
575 _Py_FatalInitError(err);
576 }
577 dump_config();
578 Py_Finalize();
579 return 0;
580}
581
582
Steve Dowerea74f0c2017-01-01 20:25:03 -0800583/* *********************************************************
584 * List of test cases and the function that implements it.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300585 *
Steve Dowerea74f0c2017-01-01 20:25:03 -0800586 * Names are compared case-sensitively with the first
587 * argument. If no match is found, or no first argument was
588 * provided, the names of all test cases are printed and
589 * the exit code will be -1.
590 *
591 * The int returned from test functions is used as the exit
592 * code, and test_capi treats all non-zero exit codes as a
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300593 * failed test.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800594 *********************************************************/
595struct TestCase
596{
597 const char *name;
598 int (*func)(void);
599};
600
601static struct TestCase TestCases[] = {
602 { "forced_io_encoding", test_forced_io_encoding },
603 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
Victor Stinner9e87e772017-11-24 12:09:24 +0100604 { "pre_initialization_api", test_pre_initialization_api },
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700605 { "pre_initialization_sys_options", test_pre_initialization_sys_options },
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100606 { "bpo20891", test_bpo20891 },
Miss Islington (bot)3747dd12018-06-22 10:33:48 -0700607 { "initialize_twice", test_initialize_twice },
Miss Islington (bot)03ec4df2018-07-20 17:16:22 -0700608 { "initialize_pymain", test_initialize_pymain },
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200609 { "init_default_config", test_init_default_config },
610 { "init_global_config", test_init_global_config },
611 { "init_from_config", test_init_from_config },
612 { "init_env", test_init_env },
613 { "init_dev_mode", test_init_dev_mode },
614 { "init_isolated", test_init_isolated },
Steve Dowerea74f0c2017-01-01 20:25:03 -0800615 { NULL, NULL }
616};
617
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000618int main(int argc, char *argv[])
619{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000620 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800621 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
622 if (strcmp(argv[1], tc->name) == 0)
623 return (*tc->func)();
624 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000625 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800626
627 /* No match found, or no test name provided, so display usage */
628 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700629 "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
Steve Dowerea74f0c2017-01-01 20:25:03 -0800630 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
631 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
632 printf(" %s\n", tc->name);
633 }
634
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700635 /* Non-zero exit code will cause test_embed.py tests to fail.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800636 This is intentional. */
637 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200638}