blob: b1b7c6e0ffc3916fa2554f0fe7c61a9e91269577 [file] [log] [blame]
Victor Stinner91c99872019-05-14 22:01:51 +02001/* FIXME: PEP 587 makes these functions public */
2#ifndef Py_BUILD_CORE_MODULE
3# define Py_BUILD_CORE_MODULE
4#endif
5
Antoine Pitrou8e605772011-04-25 21:21:07 +02006#include <Python.h>
Victor Stinner91c99872019-05-14 22:01:51 +02007#include "pycore_coreconfig.h" /* FIXME: PEP 587 makes these functions public */
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01008#include "pythread.h"
Eric Snowe3774162017-05-22 19:46:40 -07009#include <inttypes.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +020010#include <stdio.h>
Nick Coghlanbc77eff2018-03-25 20:44:30 +100011#include <wchar.h>
Antoine Pitrou8e605772011-04-25 21:21:07 +020012
Nick Coghlan7d270ee2013-10-17 22:35:35 +100013/*********************************************************
14 * Embedded interpreter tests that need a custom exe
15 *
16 * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
17 *********************************************************/
18
19static void _testembed_Py_Initialize(void)
20{
21 /* HACK: the "./" at front avoids a search along the PATH in
22 Modules/getpath.c */
23 Py_SetProgramName(L"./_testembed");
24 Py_Initialize();
25}
26
27
28/*****************************************************
Martin Panter8f265652016-04-19 04:03:41 +000029 * Test repeated initialisation and subinterpreters
Nick Coghlan7d270ee2013-10-17 22:35:35 +100030 *****************************************************/
31
32static void print_subinterp(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020033{
Eric Snowe3774162017-05-22 19:46:40 -070034 /* Output information about the interpreter in the format
35 expected in Lib/test/test_capi.py (test_subinterps). */
Antoine Pitrou8e605772011-04-25 21:21:07 +020036 PyThreadState *ts = PyThreadState_Get();
Eric Snowe3774162017-05-22 19:46:40 -070037 PyInterpreterState *interp = ts->interp;
38 int64_t id = PyInterpreterState_GetID(interp);
Eric Snowd1c3c132017-05-24 17:19:47 -070039 printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
Eric Snowe3774162017-05-22 19:46:40 -070040 id, (uintptr_t)interp, (uintptr_t)ts);
Antoine Pitrou8e605772011-04-25 21:21:07 +020041 fflush(stdout);
42 PyRun_SimpleString(
43 "import sys;"
44 "print('id(modules) =', id(sys.modules));"
45 "sys.stdout.flush()"
46 );
47}
48
Steve Dowerea74f0c2017-01-01 20:25:03 -080049static int test_repeated_init_and_subinterpreters(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020050{
51 PyThreadState *mainstate, *substate;
52 PyGILState_STATE gilstate;
53 int i, j;
54
Ned Deily939231b2016-08-16 00:17:42 -040055 for (i=0; i<15; i++) {
Antoine Pitrou8e605772011-04-25 21:21:07 +020056 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100057 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020058 mainstate = PyThreadState_Get();
59
60 PyEval_InitThreads();
61 PyEval_ReleaseThread(mainstate);
62
63 gilstate = PyGILState_Ensure();
64 print_subinterp();
65 PyThreadState_Swap(NULL);
66
67 for (j=0; j<3; j++) {
68 substate = Py_NewInterpreter();
69 print_subinterp();
70 Py_EndInterpreter(substate);
71 }
72
73 PyThreadState_Swap(mainstate);
74 print_subinterp();
75 PyGILState_Release(gilstate);
Antoine Pitrou8e605772011-04-25 21:21:07 +020076
77 PyEval_RestoreThread(mainstate);
78 Py_Finalize();
79 }
Steve Dowerea74f0c2017-01-01 20:25:03 -080080 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +100081}
82
83/*****************************************************
84 * Test forcing a particular IO encoding
85 *****************************************************/
86
87static void check_stdio_details(const char *encoding, const char * errors)
88{
89 /* Output info for the test case to check */
90 if (encoding) {
91 printf("Expected encoding: %s\n", encoding);
92 } else {
93 printf("Expected encoding: default\n");
94 }
95 if (errors) {
96 printf("Expected errors: %s\n", errors);
97 } else {
98 printf("Expected errors: default\n");
99 }
100 fflush(stdout);
101 /* Force the given IO encoding */
102 Py_SetStandardStreamEncoding(encoding, errors);
103 _testembed_Py_Initialize();
104 PyRun_SimpleString(
105 "import sys;"
106 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
107 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
108 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
109 "sys.stdout.flush()"
110 );
111 Py_Finalize();
112}
113
Steve Dowerea74f0c2017-01-01 20:25:03 -0800114static int test_forced_io_encoding(void)
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000115{
116 /* Check various combinations */
117 printf("--- Use defaults ---\n");
118 check_stdio_details(NULL, NULL);
119 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100120 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000121 printf("--- Set encoding only ---\n");
Victor Stinner9e4994d2018-08-28 23:26:33 +0200122 check_stdio_details("iso8859-1", NULL);
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000123 printf("--- Set encoding and errors ---\n");
Victor Stinner9e4994d2018-08-28 23:26:33 +0200124 check_stdio_details("iso8859-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000125
126 /* Check calling after initialization fails */
127 Py_Initialize();
128
129 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
130 printf("Unexpected success calling Py_SetStandardStreamEncoding");
131 }
132 Py_Finalize();
Steve Dowerea74f0c2017-01-01 20:25:03 -0800133 return 0;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000134}
135
Victor Stinner9e87e772017-11-24 12:09:24 +0100136/*********************************************************
137 * Test parts of the C-API that work before initialization
138 *********************************************************/
139
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000140/* The pre-initialization tests tend to break by segfaulting, so explicitly
141 * flushed progress messages make the broken API easier to find when they fail.
142 */
143#define _Py_EMBED_PREINIT_CHECK(msg) \
144 do {printf(msg); fflush(stdout);} while (0);
145
Victor Stinner9e87e772017-11-24 12:09:24 +0100146static int test_pre_initialization_api(void)
147{
Victor Stinnera9df6512019-03-05 23:31:54 +0100148 /* the test doesn't support custom memory allocators */
149 putenv("PYTHONMALLOC=");
150
Nick Coghlan42746092017-11-26 14:19:13 +1000151 /* Leading "./" ensures getpath.c can still find the standard library */
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000152 _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
Nick Coghlan42746092017-11-26 14:19:13 +1000153 wchar_t *program = Py_DecodeLocale("./spam", NULL);
Victor Stinner9e87e772017-11-24 12:09:24 +0100154 if (program == NULL) {
155 fprintf(stderr, "Fatal error: cannot decode program name\n");
156 return 1;
157 }
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000158 _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100159 Py_SetProgramName(program);
160
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000161 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100162 Py_Initialize();
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000163 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
164 PyRun_SimpleString("import sys; "
165 "print('sys.executable:', sys.executable)");
166 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100167 Py_Finalize();
168
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000169 _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
Victor Stinner9e87e772017-11-24 12:09:24 +0100170 PyMem_RawFree(program);
171 return 0;
172}
173
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000174
175/* bpo-33042: Ensure embedding apps can predefine sys module options */
176static int test_pre_initialization_sys_options(void)
177{
Nick Coghlan69f5c732018-03-30 15:36:42 +1000178 /* We allocate a couple of the options dynamically, and then delete
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000179 * them before calling Py_Initialize. This ensures the interpreter isn't
180 * relying on the caller to keep the passed in strings alive.
181 */
Nick Coghlan69f5c732018-03-30 15:36:42 +1000182 const wchar_t *static_warnoption = L"once";
183 const wchar_t *static_xoption = L"also_not_an_option=2";
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000184 size_t warnoption_len = wcslen(static_warnoption);
185 size_t xoption_len = wcslen(static_xoption);
Nick Coghlan69f5c732018-03-30 15:36:42 +1000186 wchar_t *dynamic_once_warnoption = \
187 (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
188 wchar_t *dynamic_xoption = \
189 (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000190 wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
191 wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);
192
193 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
194 PySys_AddWarnOption(L"default");
195 _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
196 PySys_ResetWarnOptions();
197 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
198 PySys_AddWarnOption(dynamic_once_warnoption);
199 PySys_AddWarnOption(L"module");
200 PySys_AddWarnOption(L"default");
201 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
202 PySys_AddXOption(L"not_an_option=1");
203 PySys_AddXOption(dynamic_xoption);
204
205 /* Delete the dynamic options early */
206 free(dynamic_once_warnoption);
207 dynamic_once_warnoption = NULL;
208 free(dynamic_xoption);
209 dynamic_xoption = NULL;
210
211 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
212 _testembed_Py_Initialize();
213 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
214 PyRun_SimpleString("import sys; "
215 "print('sys.warnoptions:', sys.warnoptions); "
216 "print('sys._xoptions:', sys._xoptions); "
217 "warnings = sys.modules['warnings']; "
218 "latest_filters = [f[0] for f in warnings.filters[:3]]; "
219 "print('warnings.filters[:3]:', latest_filters)");
220 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
221 Py_Finalize();
222
223 return 0;
224}
225
226
227/* bpo-20891: Avoid race condition when initialising the GIL */
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100228static void bpo20891_thread(void *lockp)
229{
230 PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
231
232 PyGILState_STATE state = PyGILState_Ensure();
233 if (!PyGILState_Check()) {
234 fprintf(stderr, "PyGILState_Check failed!");
235 abort();
236 }
237
238 PyGILState_Release(state);
239
240 PyThread_release_lock(lock);
241
242 PyThread_exit_thread();
243}
244
245static int test_bpo20891(void)
246{
Victor Stinnera9df6512019-03-05 23:31:54 +0100247 /* the test doesn't support custom memory allocators */
248 putenv("PYTHONMALLOC=");
249
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100250 /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
251 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
252 call PyEval_InitThreads() for us in this case. */
253 PyThread_type_lock lock = PyThread_allocate_lock();
254 if (!lock) {
255 fprintf(stderr, "PyThread_allocate_lock failed!");
256 return 1;
257 }
258
259 _testembed_Py_Initialize();
260
261 unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
262 if (thrd == PYTHREAD_INVALID_THREAD_ID) {
263 fprintf(stderr, "PyThread_start_new_thread failed!");
264 return 1;
265 }
266 PyThread_acquire_lock(lock, WAIT_LOCK);
267
268 Py_BEGIN_ALLOW_THREADS
269 /* wait until the thread exit */
270 PyThread_acquire_lock(lock, WAIT_LOCK);
271 Py_END_ALLOW_THREADS
272
273 PyThread_free_lock(lock);
274
275 return 0;
276}
277
Victor Stinner209abf72018-06-22 19:14:51 +0200278static int test_initialize_twice(void)
279{
280 _testembed_Py_Initialize();
281
282 /* bpo-33932: Calling Py_Initialize() twice should do nothing
283 * (and not crash!). */
284 Py_Initialize();
285
286 Py_Finalize();
287
288 return 0;
289}
290
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200291static int test_initialize_pymain(void)
292{
293 wchar_t *argv[] = {L"PYTHON", L"-c",
294 L"import sys; print(f'Py_Main() after Py_Initialize: sys.argv={sys.argv}')",
295 L"arg2"};
296 _testembed_Py_Initialize();
297
298 /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
299 Py_Main(Py_ARRAY_LENGTH(argv), argv);
300
301 Py_Finalize();
302
303 return 0;
304}
305
Victor Stinner9e87e772017-11-24 12:09:24 +0100306
Victor Stinner00b137c2018-11-13 19:59:26 +0100307static void
308dump_config(void)
309{
Victor Stinner23bace22019-04-18 11:37:26 +0200310 (void) PyRun_SimpleStringFlags(
311 "import _testinternalcapi, json; "
312 "print(json.dumps(_testinternalcapi.get_configs()))",
313 0);
Victor Stinner00b137c2018-11-13 19:59:26 +0100314}
315
316
Victor Stinner56b29b62018-07-26 18:57:56 +0200317static int test_init_default_config(void)
318{
319 _testembed_Py_Initialize();
320 dump_config();
321 Py_Finalize();
322 return 0;
323}
324
325
326static int test_init_global_config(void)
327{
328 /* FIXME: test Py_IgnoreEnvironmentFlag */
329
330 putenv("PYTHONUTF8=0");
331 Py_UTF8Mode = 1;
332
333 /* Test initialization from global configuration variables (Py_xxx) */
334 Py_SetProgramName(L"./globalvar");
335
336 /* Py_IsolatedFlag is not tested */
337 Py_NoSiteFlag = 1;
338 Py_BytesWarningFlag = 1;
339
340 putenv("PYTHONINSPECT=");
341 Py_InspectFlag = 1;
342
343 putenv("PYTHONOPTIMIZE=0");
344 Py_InteractiveFlag = 1;
345
346 putenv("PYTHONDEBUG=0");
347 Py_OptimizeFlag = 2;
348
349 /* Py_DebugFlag is not tested */
350
351 putenv("PYTHONDONTWRITEBYTECODE=");
352 Py_DontWriteBytecodeFlag = 1;
353
354 putenv("PYTHONVERBOSE=0");
355 Py_VerboseFlag = 1;
356
357 Py_QuietFlag = 1;
358 Py_NoUserSiteDirectory = 1;
359
360 putenv("PYTHONUNBUFFERED=");
361 Py_UnbufferedStdioFlag = 1;
362
Victor Stinner54b43bb2019-05-16 18:30:15 +0200363 Py_FrozenFlag = 1;
364
Victor Stinner56b29b62018-07-26 18:57:56 +0200365 /* FIXME: test Py_LegacyWindowsFSEncodingFlag */
366 /* FIXME: test Py_LegacyWindowsStdioFlag */
367
Victor Stinner56b29b62018-07-26 18:57:56 +0200368 Py_Initialize();
369 dump_config();
370 Py_Finalize();
371 return 0;
372}
373
374
375static int test_init_from_config(void)
376{
Victor Stinner20004952019-03-26 02:31:11 +0100377 _PyInitError err;
378
379 _PyPreConfig preconfig = _PyPreConfig_INIT;
380
381 putenv("PYTHONMALLOC=malloc_debug");
382 preconfig.allocator = "malloc";
383
384 putenv("PYTHONUTF8=0");
385 Py_UTF8Mode = 0;
386 preconfig.utf8_mode = 1;
387
Victor Stinner5ac27a52019-03-27 13:40:14 +0100388 err = _Py_PreInitialize(&preconfig);
Victor Stinner20004952019-03-26 02:31:11 +0100389 if (_Py_INIT_FAILED(err)) {
390 _Py_ExitInitError(err);
391 }
392
Victor Stinner56b29b62018-07-26 18:57:56 +0200393 /* Test _Py_InitializeFromConfig() */
394 _PyCoreConfig config = _PyCoreConfig_INIT;
395 config.install_signal_handlers = 0;
396
397 /* FIXME: test use_environment */
398
399 putenv("PYTHONHASHSEED=42");
400 config.use_hash_seed = 1;
401 config.hash_seed = 123;
402
Victor Stinner56b29b62018-07-26 18:57:56 +0200403 /* dev_mode=1 is tested in test_init_dev_mode() */
404
405 putenv("PYTHONFAULTHANDLER=");
406 config.faulthandler = 1;
407
408 putenv("PYTHONTRACEMALLOC=0");
409 config.tracemalloc = 2;
410
411 putenv("PYTHONPROFILEIMPORTTIME=0");
412 config.import_time = 1;
413
414 config.show_ref_count = 1;
415 config.show_alloc_count = 1;
416 /* FIXME: test dump_refs: bpo-34223 */
417
418 putenv("PYTHONMALLOCSTATS=0");
419 config.malloc_stats = 1;
420
Victor Stinner56b29b62018-07-26 18:57:56 +0200421 putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
422 config.pycache_prefix = L"conf_pycache_prefix";
423
424 Py_SetProgramName(L"./globalvar");
425 config.program_name = L"./conf_program_name";
426
Victor Stinner2f549082019-03-29 15:13:46 +0100427 static wchar_t* argv[] = {
428 L"python3",
Victor Stinner01de89c2018-11-14 17:39:45 +0100429 L"-c",
430 L"pass",
Victor Stinner2f549082019-03-29 15:13:46 +0100431 L"arg2",
Victor Stinner01de89c2018-11-14 17:39:45 +0100432 };
Victor Stinner74f65682019-03-15 15:08:05 +0100433 config.argv.length = Py_ARRAY_LENGTH(argv);
434 config.argv.items = argv;
Victor Stinner01de89c2018-11-14 17:39:45 +0100435
Victor Stinner01de89c2018-11-14 17:39:45 +0100436 static wchar_t* xoptions[3] = {
437 L"core_xoption1=3",
438 L"core_xoption2=",
439 L"core_xoption3",
440 };
Victor Stinner74f65682019-03-15 15:08:05 +0100441 config.xoptions.length = Py_ARRAY_LENGTH(xoptions);
442 config.xoptions.items = xoptions;
Victor Stinner01de89c2018-11-14 17:39:45 +0100443
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100444 static wchar_t* warnoptions[1] = {
Victor Stinner01de89c2018-11-14 17:39:45 +0100445 L"error::ResourceWarning",
446 };
Victor Stinner74f65682019-03-15 15:08:05 +0100447 config.warnoptions.length = Py_ARRAY_LENGTH(warnoptions);
448 config.warnoptions.items = warnoptions;
Victor Stinner01de89c2018-11-14 17:39:45 +0100449
Victor Stinner56b29b62018-07-26 18:57:56 +0200450 /* FIXME: test module_search_path_env */
451 /* FIXME: test home */
452 /* FIXME: test path config: module_search_path .. dll_path */
453
454 putenv("PYTHONVERBOSE=0");
455 Py_VerboseFlag = 0;
456 config.verbose = 1;
457
458 Py_NoSiteFlag = 0;
459 config.site_import = 0;
460
461 Py_BytesWarningFlag = 0;
462 config.bytes_warning = 1;
463
464 putenv("PYTHONINSPECT=");
465 Py_InspectFlag = 0;
466 config.inspect = 1;
467
468 Py_InteractiveFlag = 0;
469 config.interactive = 1;
470
471 putenv("PYTHONOPTIMIZE=0");
472 Py_OptimizeFlag = 1;
473 config.optimization_level = 2;
474
Victor Stinner98512272018-08-01 03:07:00 +0200475 /* FIXME: test parser_debug */
Victor Stinner56b29b62018-07-26 18:57:56 +0200476
477 putenv("PYTHONDONTWRITEBYTECODE=");
478 Py_DontWriteBytecodeFlag = 0;
479 config.write_bytecode = 0;
480
481 Py_QuietFlag = 0;
482 config.quiet = 1;
483
Victor Stinner54b43bb2019-05-16 18:30:15 +0200484 config.configure_c_stdio = 0;
485
Victor Stinner56b29b62018-07-26 18:57:56 +0200486 putenv("PYTHONUNBUFFERED=");
487 Py_UnbufferedStdioFlag = 0;
Victor Stinner98512272018-08-01 03:07:00 +0200488 config.buffered_stdio = 0;
Victor Stinner56b29b62018-07-26 18:57:56 +0200489
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200490 putenv("PYTHONIOENCODING=cp424");
491 Py_SetStandardStreamEncoding("ascii", "ignore");
Victor Stinner01de89c2018-11-14 17:39:45 +0100492#ifdef MS_WINDOWS
493 /* Py_SetStandardStreamEncoding() sets Py_LegacyWindowsStdioFlag to 1.
494 Force it to 0 through the config. */
495 config.legacy_windows_stdio = 0;
496#endif
Victor Stinner709d23d2019-05-02 14:56:30 -0400497 config.stdio_encoding = L"iso8859-1";
498 config.stdio_errors = L"replace";
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200499
Victor Stinner56b29b62018-07-26 18:57:56 +0200500 putenv("PYTHONNOUSERSITE=");
501 Py_NoUserSiteDirectory = 0;
502 config.user_site_directory = 0;
503
Victor Stinnercb9fbd32019-05-01 23:51:56 -0400504 config.check_hash_pycs_mode = L"always";
Victor Stinner56b29b62018-07-26 18:57:56 +0200505
Victor Stinner54b43bb2019-05-16 18:30:15 +0200506 Py_FrozenFlag = 0;
507 config.pathconfig_warnings = 0;
508
Victor Stinner5ac27a52019-03-27 13:40:14 +0100509 err = _Py_InitializeFromConfig(&config);
Victor Stinner56b29b62018-07-26 18:57:56 +0200510 if (_Py_INIT_FAILED(err)) {
Victor Stinnerdfe88472019-03-01 12:14:41 +0100511 _Py_ExitInitError(err);
Victor Stinner56b29b62018-07-26 18:57:56 +0200512 }
513 dump_config();
514 Py_Finalize();
515 return 0;
516}
517
518
Victor Stinnerae239f62019-05-16 17:02:56 +0200519static int test_init_dont_parse_argv(void)
520{
521 _PyInitError err;
522
523 _PyCoreConfig config = _PyCoreConfig_INIT;
524
525 static wchar_t* argv[] = {
526 L"-v",
527 L"-c",
528 L"arg1",
529 L"-W",
530 L"arg2",
531 };
532
Victor Stinnerae239f62019-05-16 17:02:56 +0200533 config.program_name = L"./_testembed";
534
535 config.argv.length = Py_ARRAY_LENGTH(argv);
536 config.argv.items = argv;
537 config.parse_argv = 0;
538
539 err = _Py_InitializeFromConfig(&config);
540 if (_Py_INIT_FAILED(err)) {
541 _Py_ExitInitError(err);
542 }
543 dump_config();
544 Py_Finalize();
545 return 0;
546}
547
548
Victor Stinner56b29b62018-07-26 18:57:56 +0200549static void test_init_env_putenvs(void)
550{
551 putenv("PYTHONHASHSEED=42");
Victor Stinner25d13f32019-03-06 12:51:53 +0100552 putenv("PYTHONMALLOC=malloc");
Victor Stinner56b29b62018-07-26 18:57:56 +0200553 putenv("PYTHONTRACEMALLOC=2");
554 putenv("PYTHONPROFILEIMPORTTIME=1");
555 putenv("PYTHONMALLOCSTATS=1");
556 putenv("PYTHONUTF8=1");
557 putenv("PYTHONVERBOSE=1");
558 putenv("PYTHONINSPECT=1");
559 putenv("PYTHONOPTIMIZE=2");
560 putenv("PYTHONDONTWRITEBYTECODE=1");
561 putenv("PYTHONUNBUFFERED=1");
562 putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
563 putenv("PYTHONNOUSERSITE=1");
564 putenv("PYTHONFAULTHANDLER=1");
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200565 putenv("PYTHONIOENCODING=iso8859-1:replace");
Victor Stinner56b29b62018-07-26 18:57:56 +0200566 /* FIXME: test PYTHONWARNINGS */
567 /* FIXME: test PYTHONEXECUTABLE */
568 /* FIXME: test PYTHONHOME */
569 /* FIXME: test PYTHONDEBUG */
570 /* FIXME: test PYTHONDUMPREFS */
571 /* FIXME: test PYTHONCOERCECLOCALE */
572 /* FIXME: test PYTHONPATH */
573}
574
575
576static int test_init_env(void)
577{
578 /* Test initialization from environment variables */
579 Py_IgnoreEnvironmentFlag = 0;
580 test_init_env_putenvs();
581 _testembed_Py_Initialize();
582 dump_config();
583 Py_Finalize();
584 return 0;
585}
586
587
Victor Stinner25d13f32019-03-06 12:51:53 +0100588static void test_init_env_dev_mode_putenvs(void)
589{
590 test_init_env_putenvs();
591 putenv("PYTHONMALLOC=");
592 putenv("PYTHONFAULTHANDLER=");
593 putenv("PYTHONDEVMODE=1");
594}
595
596
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100597static int test_init_env_dev_mode(void)
598{
599 /* Test initialization from environment variables */
600 Py_IgnoreEnvironmentFlag = 0;
601 test_init_env_dev_mode_putenvs();
602 _testembed_Py_Initialize();
603 dump_config();
604 Py_Finalize();
605 return 0;
606}
607
608
Victor Stinner25d13f32019-03-06 12:51:53 +0100609static int test_init_env_dev_mode_alloc(void)
610{
611 /* Test initialization from environment variables */
612 Py_IgnoreEnvironmentFlag = 0;
613 test_init_env_dev_mode_putenvs();
614 putenv("PYTHONMALLOC=malloc");
615 _testembed_Py_Initialize();
616 dump_config();
617 Py_Finalize();
618 return 0;
619}
620
621
Victor Stinner56b29b62018-07-26 18:57:56 +0200622static int test_init_isolated(void)
623{
Victor Stinner20004952019-03-26 02:31:11 +0100624 _PyInitError err;
625
Victor Stinner56b29b62018-07-26 18:57:56 +0200626 /* Test _PyCoreConfig.isolated=1 */
627 _PyCoreConfig config = _PyCoreConfig_INIT;
628
Victor Stinnercad1f742019-03-05 02:01:27 +0100629 Py_IsolatedFlag = 0;
Victor Stinner20004952019-03-26 02:31:11 +0100630 config.isolated = 1;
Victor Stinnercad1f742019-03-05 02:01:27 +0100631
Victor Stinner56b29b62018-07-26 18:57:56 +0200632 /* Use path starting with "./" avoids a search along the PATH */
633 config.program_name = L"./_testembed";
634
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100635 test_init_env_dev_mode_putenvs();
Victor Stinner5ac27a52019-03-27 13:40:14 +0100636 err = _Py_InitializeFromConfig(&config);
Victor Stinner56b29b62018-07-26 18:57:56 +0200637 if (_Py_INIT_FAILED(err)) {
Victor Stinnerdfe88472019-03-01 12:14:41 +0100638 _Py_ExitInitError(err);
Victor Stinner56b29b62018-07-26 18:57:56 +0200639 }
640 dump_config();
641 Py_Finalize();
642 return 0;
643}
644
645
Victor Stinner6da20a42019-03-27 00:26:18 +0100646/* _PyPreConfig.isolated=1, _PyCoreConfig.isolated=0 */
647static int test_preinit_isolated1(void)
648{
649 _PyInitError err;
650
651 _PyPreConfig preconfig = _PyPreConfig_INIT;
Victor Stinner6da20a42019-03-27 00:26:18 +0100652 preconfig.isolated = 1;
653
Victor Stinner5ac27a52019-03-27 13:40:14 +0100654 err = _Py_PreInitialize(&preconfig);
Victor Stinner6da20a42019-03-27 00:26:18 +0100655 if (_Py_INIT_FAILED(err)) {
656 _Py_ExitInitError(err);
657 }
658
659 _PyCoreConfig config = _PyCoreConfig_INIT;
660 config.program_name = L"./_testembed";
661
662 test_init_env_dev_mode_putenvs();
Victor Stinner5ac27a52019-03-27 13:40:14 +0100663 err = _Py_InitializeFromConfig(&config);
Victor Stinner6da20a42019-03-27 00:26:18 +0100664 if (_Py_INIT_FAILED(err)) {
665 _Py_ExitInitError(err);
666 }
667 dump_config();
668 Py_Finalize();
669 return 0;
670}
671
672
673/* _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1 */
674static int test_preinit_isolated2(void)
675{
676 _PyInitError err;
677
678 _PyPreConfig preconfig = _PyPreConfig_INIT;
Victor Stinner6da20a42019-03-27 00:26:18 +0100679 preconfig.isolated = 0;
680
Victor Stinner5ac27a52019-03-27 13:40:14 +0100681 err = _Py_PreInitialize(&preconfig);
Victor Stinner6da20a42019-03-27 00:26:18 +0100682 if (_Py_INIT_FAILED(err)) {
683 _Py_ExitInitError(err);
684 }
685
686 /* Test _PyCoreConfig.isolated=1 */
687 _PyCoreConfig config = _PyCoreConfig_INIT;
688
689 Py_IsolatedFlag = 0;
690 config.isolated = 1;
691
692 /* Use path starting with "./" avoids a search along the PATH */
693 config.program_name = L"./_testembed";
694
695 test_init_env_dev_mode_putenvs();
Victor Stinner5ac27a52019-03-27 13:40:14 +0100696 err = _Py_InitializeFromConfig(&config);
Victor Stinner6da20a42019-03-27 00:26:18 +0100697 if (_Py_INIT_FAILED(err)) {
698 _Py_ExitInitError(err);
699 }
700 dump_config();
701 Py_Finalize();
702 return 0;
703}
704
705
Victor Stinner56b29b62018-07-26 18:57:56 +0200706static int test_init_dev_mode(void)
707{
708 _PyCoreConfig config = _PyCoreConfig_INIT;
709 putenv("PYTHONFAULTHANDLER=");
710 putenv("PYTHONMALLOC=");
Victor Stinner20004952019-03-26 02:31:11 +0100711 config.dev_mode = 1;
Victor Stinner56b29b62018-07-26 18:57:56 +0200712 config.program_name = L"./_testembed";
Victor Stinner5ac27a52019-03-27 13:40:14 +0100713 _PyInitError err = _Py_InitializeFromConfig(&config);
Victor Stinner56b29b62018-07-26 18:57:56 +0200714 if (_Py_INIT_FAILED(err)) {
Victor Stinnerdfe88472019-03-01 12:14:41 +0100715 _Py_ExitInitError(err);
Victor Stinner56b29b62018-07-26 18:57:56 +0200716 }
717 dump_config();
718 Py_Finalize();
719 return 0;
720}
721
722
Victor Stinner91c99872019-05-14 22:01:51 +0200723static int test_init_read_set(void)
724{
725 _PyInitError err;
726 _PyCoreConfig config = _PyCoreConfig_INIT;
727
728 err = _PyCoreConfig_DecodeLocale(&config.program_name, "./init_read_set");
729 if (_Py_INIT_FAILED(err)) {
730 goto fail;
731 }
732
733 err = _PyCoreConfig_Read(&config);
734 if (_Py_INIT_FAILED(err)) {
735 goto fail;
736 }
737
738 if (_PyWstrList_Append(&config.module_search_paths,
739 L"init_read_set_path") < 0) {
740 err = _Py_INIT_NO_MEMORY();
741 goto fail;
742 }
743
744 /* override executable computed by _PyCoreConfig_Read() */
745 err = _PyCoreConfig_SetString(&config.executable, L"my_executable");
746 if (_Py_INIT_FAILED(err)) {
747 goto fail;
748 }
749
750 err = _Py_InitializeFromConfig(&config);
751 _PyCoreConfig_Clear(&config);
752 if (_Py_INIT_FAILED(err)) {
753 goto fail;
754 }
755 dump_config();
756 Py_Finalize();
757 return 0;
758
759fail:
760 _Py_ExitInitError(err);
761}
762
763
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200764wchar_t *init_main_argv[] = {
765 L"python3", L"-c",
766 (L"import _testinternalcapi, json; "
767 L"print(json.dumps(_testinternalcapi.get_configs()))"),
768 L"arg2"};
769
770
771static void configure_init_main(_PyCoreConfig *config)
772{
773 config->argv.length = Py_ARRAY_LENGTH(init_main_argv);
774 config->argv.items = init_main_argv;
775 config->program_name = L"./python3";
776}
777
778
779static int test_init_run_main(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100780{
781 _PyCoreConfig config = _PyCoreConfig_INIT;
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200782 configure_init_main(&config);
Victor Stinner2f549082019-03-29 15:13:46 +0100783
784 _PyInitError err = _Py_InitializeFromConfig(&config);
785 if (_Py_INIT_FAILED(err)) {
786 _Py_ExitInitError(err);
787 }
788
789 return _Py_RunMain();
790}
791
792
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200793static int test_init_main(void)
794{
795 _PyCoreConfig config = _PyCoreConfig_INIT;
796 configure_init_main(&config);
797 config._init_main = 0;
798
799 _PyInitError err = _Py_InitializeFromConfig(&config);
800 if (_Py_INIT_FAILED(err)) {
801 _Py_ExitInitError(err);
802 }
803
804 /* sys.stdout don't exist yet: it is created by _Py_InitializeMain() */
805 int res = PyRun_SimpleString(
806 "import sys; "
807 "print('Run Python code before _Py_InitializeMain', "
808 "file=sys.stderr)");
809 if (res < 0) {
810 exit(1);
811 }
812
813 err = _Py_InitializeMain();
814 if (_Py_INIT_FAILED(err)) {
815 _Py_ExitInitError(err);
816 }
817
818 return _Py_RunMain();
819}
820
821
822static int test_run_main(void)
Victor Stinner5eb8b072019-05-15 02:12:48 +0200823{
824 _PyCoreConfig config = _PyCoreConfig_INIT;
825
826 wchar_t *argv[] = {L"python3", L"-c",
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200827 (L"import sys; "
828 L"print(f'_Py_RunMain(): sys.argv={sys.argv}')"),
Victor Stinner5eb8b072019-05-15 02:12:48 +0200829 L"arg2"};
830 config.argv.length = Py_ARRAY_LENGTH(argv);
831 config.argv.items = argv;
832 config.program_name = L"./python3";
833
834 _PyInitError err = _Py_InitializeFromConfig(&config);
835 if (_Py_INIT_FAILED(err)) {
836 _Py_ExitInitError(err);
837 }
838
839 return _Py_RunMain();
840}
841
842
Steve Dowerea74f0c2017-01-01 20:25:03 -0800843/* *********************************************************
844 * List of test cases and the function that implements it.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300845 *
Steve Dowerea74f0c2017-01-01 20:25:03 -0800846 * Names are compared case-sensitively with the first
847 * argument. If no match is found, or no first argument was
848 * provided, the names of all test cases are printed and
849 * the exit code will be -1.
850 *
851 * The int returned from test functions is used as the exit
852 * code, and test_capi treats all non-zero exit codes as a
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300853 * failed test.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800854 *********************************************************/
855struct TestCase
856{
857 const char *name;
858 int (*func)(void);
859};
860
861static struct TestCase TestCases[] = {
862 { "forced_io_encoding", test_forced_io_encoding },
863 { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
Victor Stinner9e87e772017-11-24 12:09:24 +0100864 { "pre_initialization_api", test_pre_initialization_api },
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000865 { "pre_initialization_sys_options", test_pre_initialization_sys_options },
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100866 { "bpo20891", test_bpo20891 },
Victor Stinner209abf72018-06-22 19:14:51 +0200867 { "initialize_twice", test_initialize_twice },
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200868 { "initialize_pymain", test_initialize_pymain },
Victor Stinner56b29b62018-07-26 18:57:56 +0200869 { "init_default_config", test_init_default_config },
870 { "init_global_config", test_init_global_config },
871 { "init_from_config", test_init_from_config },
Victor Stinnerae239f62019-05-16 17:02:56 +0200872 { "init_dont_parse_argv", test_init_dont_parse_argv },
Victor Stinner56b29b62018-07-26 18:57:56 +0200873 { "init_env", test_init_env },
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100874 { "init_env_dev_mode", test_init_env_dev_mode },
Victor Stinner25d13f32019-03-06 12:51:53 +0100875 { "init_env_dev_mode_alloc", test_init_env_dev_mode_alloc },
Victor Stinner56b29b62018-07-26 18:57:56 +0200876 { "init_dev_mode", test_init_dev_mode },
877 { "init_isolated", test_init_isolated },
Victor Stinner6da20a42019-03-27 00:26:18 +0100878 { "preinit_isolated1", test_preinit_isolated1 },
879 { "preinit_isolated2", test_preinit_isolated2 },
Victor Stinner91c99872019-05-14 22:01:51 +0200880 { "init_read_set", test_init_read_set },
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200881 { "init_run_main", test_init_run_main },
882 { "init_main", test_init_main },
Victor Stinner2f549082019-03-29 15:13:46 +0100883 { "run_main", test_run_main },
Steve Dowerea74f0c2017-01-01 20:25:03 -0800884 { NULL, NULL }
885};
886
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000887int main(int argc, char *argv[])
888{
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000889 if (argc > 1) {
Steve Dowerea74f0c2017-01-01 20:25:03 -0800890 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
891 if (strcmp(argv[1], tc->name) == 0)
892 return (*tc->func)();
893 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000894 }
Steve Dowerea74f0c2017-01-01 20:25:03 -0800895
896 /* No match found, or no test name provided, so display usage */
897 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000898 "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
Steve Dowerea74f0c2017-01-01 20:25:03 -0800899 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
900 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
901 printf(" %s\n", tc->name);
902 }
903
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000904 /* Non-zero exit code will cause test_embed.py tests to fail.
Steve Dowerea74f0c2017-01-01 20:25:03 -0800905 This is intentional. */
906 return -1;
Antoine Pitrou8e605772011-04-25 21:21:07 +0200907}