Debian Codegraph @

Programs/_testembed.c

raw
1 #ifndef Py_BUILD_CORE_MODULE
2 # define Py_BUILD_CORE_MODULE
3 #endif
4
5 /* Always enable assertion (even in release mode) */
6 #undef NDEBUG
7
8 #include <Python.h>
9 #include "pycore_initconfig.h" // _PyConfig_InitCompatConfig()
10 #include "pycore_runtime.h" // _PyRuntime
11 #include "pycore_pythread.h" // PyThread_start_joinable_thread()
12 #include "pycore_import.h" // _PyImport_FrozenBootstrap
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <stdlib.h> // putenv()
16 #include <wchar.h>
17
18 // These functions were removed from Python 3.13 API but are still exported
19 // for the stable ABI. We want to test them in this program.
20 extern void PySys_AddWarnOption(const wchar_t *s);
21 extern void PySys_AddXOption(const wchar_t *s);
22 extern void Py_SetPath(const wchar_t *path);
23
24
25 int main_argc;
26 char **main_argv;
27
28 /*********************************************************
29 * Embedded interpreter tests that need a custom exe
30 *
31 * Executed via Lib/test/test_embed.py
32 *********************************************************/
33
34 // Use to display the usage
35 #define PROGRAM "test_embed"
36
37 /* Use path starting with "./" avoids a search along the PATH */
38 #define PROGRAM_NAME L"./_testembed"
39
40 #define INIT_LOOPS 4
41
42 // Ignore Py_DEPRECATED() compiler warnings: deprecated functions are
43 // tested on purpose here.
44 _Py_COMP_DIAG_PUSH
45 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
46
47
48 static void error(const char *msg)
49 {
50 fprintf(stderr, "ERROR: %s\n", msg);
51 fflush(stderr);
52 }
53
54
55 static void error_fmt(const char *format, ...)
56 {
57 va_list vargs;
58 va_start(vargs, format);
59 fprintf(stderr, "ERROR: ");
60 vfprintf(stderr, format, vargs);
61 fprintf(stderr, "\n");
62 va_end(vargs);
63 fflush(stderr);
64 }
65
66
67 static wchar_t* py_getenv(const char *name)
68 {
69 const char *env = getenv(name);
70 if (env == NULL) {
71 error_fmt("need %s env var", name);
72 return NULL;
73 }
74
75 wchar_t *result = Py_DecodeLocale(env, NULL);
76 if (result == NULL) {
77 error("Py_DecodeLocale() failed");
78 return NULL;
79 }
80 return result;
81 }
82
83
84 static wchar_t* get_cmdline_arg(const char *arg_name)
85 {
86 if (main_argc < 3) {
87 const char *test = main_argv[1];
88 fprintf(stderr, "usage: %s %s %s\n", PROGRAM, test, arg_name);
89 return NULL;
90 }
91 const char *arg = main_argv[2];
92
93 wchar_t *result = Py_DecodeLocale(arg, NULL);
94 if (result == NULL) {
95 error_fmt("failed to decode %s command line argument", arg_name);
96 return NULL;
97 }
98 return result;
99 }
100
101
102 static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str)
103 {
104 PyStatus status = PyConfig_SetString(config, config_str, str);
105 if (PyStatus_Exception(status)) {
106 PyConfig_Clear(config);
107 Py_ExitStatusException(status);
108 }
109 }
110
111
112 static void config_set_program_name(PyConfig *config)
113 {
114 const wchar_t *program_name = PROGRAM_NAME;
115 config_set_string(config, &config->program_name, program_name);
116 }
117
118
119 static void init_from_config_clear(PyConfig *config)
120 {
121 PyStatus status = Py_InitializeFromConfig(config);
122 PyConfig_Clear(config);
123 if (PyStatus_Exception(status)) {
124 Py_ExitStatusException(status);
125 }
126 }
127
128
129 static void _testembed_Py_InitializeFromConfig(void)
130 {
131 PyConfig config;
132 _PyConfig_InitCompatConfig(&config);
133 config_set_program_name(&config);
134 init_from_config_clear(&config);
135 }
136
137 static void _testembed_Py_Initialize(void)
138 {
139 Py_SetProgramName(PROGRAM_NAME);
140 Py_Initialize();
141 }
142
143
144 static int test_import_in_subinterpreters(void)
145 {
146 _testembed_Py_InitializeFromConfig();
147 PyThreadState_Swap(Py_NewInterpreter());
148 return PyRun_SimpleString("import readline"); // gh-124160
149 }
150
151
152 /*****************************************************
153 * Test repeated initialisation and subinterpreters
154 *****************************************************/
155
156 static void print_subinterp(void)
157 {
158 /* Output information about the interpreter in the format
159 expected in Lib/test/test_capi.py (test_subinterps). */
160 PyThreadState *ts = PyThreadState_Get();
161 PyInterpreterState *interp = ts->interp;
162 int64_t id = PyInterpreterState_GetID(interp);
163 printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
164 id, (uintptr_t)interp, (uintptr_t)ts);
165 fflush(stdout);
166 PyRun_SimpleString(
167 "import sys;"
168 "print('id(modules) =', id(sys.modules));"
169 "sys.stdout.flush()"
170 );
171 }
172
173 static int test_repeated_init_and_subinterpreters(void)
174 {
175 PyThreadState *mainstate, *substate;
176 PyGILState_STATE gilstate;
177
178 for (int i=1; i <= INIT_LOOPS; i++) {
179 printf("--- Pass %d ---\n", i);
180 _testembed_Py_InitializeFromConfig();
181 mainstate = PyThreadState_Get();
182
183 PyEval_ReleaseThread(mainstate);
184
185 gilstate = PyGILState_Ensure();
186 print_subinterp();
187 PyThreadState_Swap(NULL);
188
189 for (int j=0; j<3; j++) {
190 substate = Py_NewInterpreter();
191 print_subinterp();
192 Py_EndInterpreter(substate);
193 }
194
195 PyThreadState_Swap(mainstate);
196 print_subinterp();
197 PyGILState_Release(gilstate);
198
199 PyEval_RestoreThread(mainstate);
200 Py_Finalize();
201 }
202 return 0;
203 }
204
205 #define EMBEDDED_EXT_NAME "embedded_ext"
206
207 static PyModuleDef embedded_ext = {
208 PyModuleDef_HEAD_INIT,
209 .m_name = EMBEDDED_EXT_NAME,
210 .m_size = 0,
211 };
212
213 static PyObject*
214 PyInit_embedded_ext(void)
215 {
216 return PyModule_Create(&embedded_ext);
217 }
218
219 /****************************************************************************
220 * Call Py_Initialize()/Py_Finalize() multiple times and execute Python code
221 ***************************************************************************/
222
223 // Used by bpo-46417 to test that structseq types used by the sys module are
224 // cleared properly and initialized again properly when Python is finalized
225 // multiple times.
226 static int test_repeated_init_exec(void)
227 {
228 if (main_argc < 3) {
229 fprintf(stderr,
230 "usage: %s test_repeated_init_exec CODE ...\n", PROGRAM);
231 exit(1);
232 }
233 const char *code = main_argv[2];
234 int loops = main_argc > 3
235 ? main_argc - 2
236 : INIT_LOOPS;
237
238 for (int i=0; i < loops; i++) {
239 fprintf(stderr, "--- Loop #%d ---\n", i+1);
240 fflush(stderr);
241
242 if (main_argc > 3) {
243 code = main_argv[i+2];
244 }
245
246 _testembed_Py_InitializeFromConfig();
247 int err = PyRun_SimpleString(code);
248 Py_Finalize();
249 if (err) {
250 return 1;
251 }
252 }
253 return 0;
254 }
255
256 /****************************************************************************
257 * Test the Py_Initialize(Ex) convenience/compatibility wrappers
258 ***************************************************************************/
259 // This is here to help ensure there are no wrapper resource leaks (gh-96853)
260 static int test_repeated_simple_init(void)
261 {
262 for (int i=1; i <= INIT_LOOPS; i++) {
263 fprintf(stderr, "--- Loop #%d ---\n", i);
264 fflush(stderr);
265
266 _testembed_Py_Initialize();
267 Py_Finalize();
268 printf("Finalized\n"); // Give test_embed some output to check
269 }
270 return 0;
271 }
272
273
274 /*****************************************************
275 * Test forcing a particular IO encoding
276 *****************************************************/
277
278 static void check_stdio_details(const wchar_t *encoding, const wchar_t *errors)
279 {
280 /* Output info for the test case to check */
281 if (encoding) {
282 printf("Expected encoding: %ls\n", encoding);
283 } else {
284 printf("Expected encoding: default\n");
285 }
286 if (errors) {
287 printf("Expected errors: %ls\n", errors);
288 } else {
289 printf("Expected errors: default\n");
290 }
291 fflush(stdout);
292
293 PyConfig config;
294 _PyConfig_InitCompatConfig(&config);
295 /* Force the given IO encoding */
296 if (encoding) {
297 config_set_string(&config, &config.stdio_encoding, encoding);
298 }
299 if (errors) {
300 config_set_string(&config, &config.stdio_errors, errors);
301 }
302 #ifdef MS_WINDOWS
303 // gh-106659: On Windows, don't use _io._WindowsConsoleIO which always
304 // announce UTF-8 for sys.stdin.encoding.
305 config.legacy_windows_stdio = 1;
306 #endif
307 config_set_program_name(&config);
308 init_from_config_clear(&config);
309
310
311 PyRun_SimpleString(
312 "import sys;"
313 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
314 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
315 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
316 "sys.stdout.flush()"
317 );
318 Py_Finalize();
319 }
320
321 static int test_forced_io_encoding(void)
322 {
323 /* Check various combinations */
324 printf("--- Use defaults ---\n");
325 check_stdio_details(NULL, NULL);
326 printf("--- Set errors only ---\n");
327 check_stdio_details(NULL, L"ignore");
328 printf("--- Set encoding only ---\n");
329 check_stdio_details(L"iso8859-1", NULL);
330 printf("--- Set encoding and errors ---\n");
331 check_stdio_details(L"iso8859-1", L"replace");
332 return 0;
333 }
334
335 /*********************************************************
336 * Test parts of the C-API that work before initialization
337 *********************************************************/
338
339 /* The pre-initialization tests tend to break by segfaulting, so explicitly
340 * flushed progress messages make the broken API easier to find when they fail.
341 */
342 #define _Py_EMBED_PREINIT_CHECK(msg) \
343 do {printf(msg); fflush(stdout);} while (0);
344
345 static int test_pre_initialization_api(void)
346 {
347 /* the test doesn't support custom memory allocators */
348 putenv("PYTHONMALLOC=");
349
350 /* Leading "./" ensures getpath.c can still find the standard library */
351 _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
352 wchar_t *program = Py_DecodeLocale("./spam", NULL);
353 if (program == NULL) {
354 fprintf(stderr, "Fatal error: cannot decode program name\n");
355 return 1;
356 }
357 _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
358 Py_SetProgramName(program);
359
360 _Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized pre-initialization\n");
361 if (Py_IsInitialized()) {
362 fprintf(stderr, "Fatal error: initialized before initialization!\n");
363 return 1;
364 }
365
366 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
367 Py_Initialize();
368
369 _Py_EMBED_PREINIT_CHECK("Checking Py_IsInitialized post-initialization\n");
370 if (!Py_IsInitialized()) {
371 fprintf(stderr, "Fatal error: not initialized after initialization!\n");
372 return 1;
373 }
374
375 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
376 PyRun_SimpleString(
377 "import sys; "
378 "print('sys.executable:', sys.executable); "
379 "sys.stdout.flush(); "
380 );
381 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
382 Py_Finalize();
383
384 _Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized post-finalization\n");
385 if (Py_IsInitialized()) {
386 fprintf(stderr, "Fatal error: still initialized after finalization!\n");
387 return 1;
388 }
389
390 _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
391 PyMem_RawFree(program);
392 return 0;
393 }
394
395
396 /* bpo-33042: Ensure embedding apps can predefine sys module options */
397 static int test_pre_initialization_sys_options(void)
398 {
399 /* We allocate a couple of the options dynamically, and then delete
400 * them before calling Py_Initialize. This ensures the interpreter isn't
401 * relying on the caller to keep the passed in strings alive.
402 */
403 const wchar_t *static_warnoption = L"once";
404 const wchar_t *static_xoption = L"also_not_an_option=2";
405 size_t warnoption_len = wcslen(static_warnoption);
406 size_t xoption_len = wcslen(static_xoption);
407 wchar_t *dynamic_once_warnoption = \
408 (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
409 if (dynamic_once_warnoption == NULL) {
410 error("out of memory allocating warnoption");
411 return 1;
412 }
413 wchar_t *dynamic_xoption = \
414 (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
415 if (dynamic_xoption == NULL) {
416 free(dynamic_once_warnoption);
417 error("out of memory allocating xoption");
418 return 1;
419 }
420
421 wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
422 wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);
423
424 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
425 PySys_AddWarnOption(L"default");
426 _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
427 PySys_ResetWarnOptions();
428 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
429 PySys_AddWarnOption(dynamic_once_warnoption);
430 PySys_AddWarnOption(L"module");
431 PySys_AddWarnOption(L"default");
432 _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
433 PySys_AddXOption(L"not_an_option=1");
434 PySys_AddXOption(dynamic_xoption);
435
436 /* Delete the dynamic options early */
437 free(dynamic_once_warnoption);
438 dynamic_once_warnoption = NULL;
439 free(dynamic_xoption);
440 dynamic_xoption = NULL;
441
442 _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
443 _testembed_Py_InitializeFromConfig();
444 _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
445 PyRun_SimpleString(
446 "import sys; "
447 "print('sys.warnoptions:', sys.warnoptions); "
448 "print('sys._xoptions:', sys._xoptions); "
449 "warnings = sys.modules['warnings']; "
450 "latest_filters = [f[0] for f in warnings.filters[:3]]; "
451 "print('warnings.filters[:3]:', latest_filters); "
452 "sys.stdout.flush(); "
453 );
454 _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
455 Py_Finalize();
456
457 return 0;
458 }
459
460
461 /* bpo-20891: Avoid race condition when initialising the GIL */
462 static void bpo20891_thread(void *eventp)
463 {
464 PyEvent *event = (PyEvent *)eventp;
465
466 PyGILState_STATE state = PyGILState_Ensure();
467 if (!PyGILState_Check()) {
468 error("PyGILState_Check failed!");
469 abort();
470 }
471
472 PyGILState_Release(state);
473 _PyEvent_Notify(event);
474 }
475
476 static int test_bpo20891(void)
477 {
478 /* the test doesn't support custom memory allocators */
479 putenv("PYTHONMALLOC=");
480
481 /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
482 crash. */
483
484 _testembed_Py_InitializeFromConfig();
485 PyEvent event = {0};
486
487 unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &event);
488 if (thrd == PYTHREAD_INVALID_THREAD_ID) {
489 error("PyThread_start_new_thread failed!");
490 return 1;
491 }
492
493 PyEvent_Wait(&event);
494
495 Py_Finalize();
496
497 return 0;
498 }
499
500 static int test_initialize_twice(void)
501 {
502 _testembed_Py_InitializeFromConfig();
503
504 /* bpo-33932: Calling Py_Initialize() twice should do nothing
505 * (and not crash!). */
506 Py_Initialize();
507
508 Py_Finalize();
509
510 return 0;
511 }
512
513 static int test_initialize_pymain(void)
514 {
515 wchar_t *argv[] = {L"PYTHON", L"-c",
516 (L"import sys; "
517 L"print(f'Py_Main() after Py_Initialize: "
518 L"sys.argv={sys.argv}')"),
519 L"arg2"};
520 _testembed_Py_InitializeFromConfig();
521
522 /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
523 Py_Main(Py_ARRAY_LENGTH(argv), argv);
524
525 Py_Finalize();
526
527 return 0;
528 }
529
530
531 static void
532 dump_config(void)
533 {
534 (void) PyRun_SimpleStringFlags(
535 "import _testinternalcapi, json; "
536 "print(json.dumps(_testinternalcapi.get_configs()))",
537 0);
538 }
539
540
541 static int test_init_initialize_config(void)
542 {
543 _testembed_Py_InitializeFromConfig();
544 dump_config();
545 Py_Finalize();
546 return 0;
547 }
548
549
550 static void config_set_argv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv)
551 {
552 PyStatus status = PyConfig_SetArgv(config, argc, argv);
553 if (PyStatus_Exception(status)) {
554 PyConfig_Clear(config);
555 Py_ExitStatusException(status);
556 }
557 }
558
559
560 static void
561 config_set_wide_string_list(PyConfig *config, PyWideStringList *list,
562 Py_ssize_t length, wchar_t **items)
563 {
564 PyStatus status = PyConfig_SetWideStringList(config, list, length, items);
565 if (PyStatus_Exception(status)) {
566 PyConfig_Clear(config);
567 Py_ExitStatusException(status);
568 }
569 }
570
571
572 static int check_init_compat_config(int preinit)
573 {
574 PyStatus status;
575
576 if (preinit) {
577 PyPreConfig preconfig;
578 _PyPreConfig_InitCompatConfig(&preconfig);
579
580 status = Py_PreInitialize(&preconfig);
581 if (PyStatus_Exception(status)) {
582 Py_ExitStatusException(status);
583 }
584 }
585
586 PyConfig config;
587 _PyConfig_InitCompatConfig(&config);
588
589 config_set_program_name(&config);
590 init_from_config_clear(&config);
591
592 dump_config();
593 Py_Finalize();
594 return 0;
595 }
596
597
598 static int test_preinit_compat_config(void)
599 {
600 return check_init_compat_config(1);
601 }
602
603
604 static int test_init_compat_config(void)
605 {
606 return check_init_compat_config(0);
607 }
608
609
610 static int test_init_global_config(void)
611 {
612 /* FIXME: test Py_IgnoreEnvironmentFlag */
613
614 putenv("PYTHONUTF8=0");
615 Py_UTF8Mode = 1;
616
617 /* Test initialization from global configuration variables (Py_xxx) */
618 Py_SetProgramName(L"./globalvar");
619
620 /* Py_IsolatedFlag is not tested */
621 Py_NoSiteFlag = 1;
622 Py_BytesWarningFlag = 1;
623
624 putenv("PYTHONINSPECT=");
625 Py_InspectFlag = 1;
626
627 putenv("PYTHONOPTIMIZE=0");
628 Py_InteractiveFlag = 1;
629
630 putenv("PYTHONDEBUG=0");
631 Py_OptimizeFlag = 2;
632
633 /* Py_DebugFlag is not tested */
634
635 putenv("PYTHONDONTWRITEBYTECODE=");
636 Py_DontWriteBytecodeFlag = 1;
637
638 putenv("PYTHONVERBOSE=0");
639 Py_VerboseFlag = 1;
640
641 Py_QuietFlag = 1;
642 Py_NoUserSiteDirectory = 1;
643
644 putenv("PYTHONUNBUFFERED=");
645 Py_UnbufferedStdioFlag = 1;
646
647 Py_FrozenFlag = 1;
648
649 /* FIXME: test Py_LegacyWindowsFSEncodingFlag */
650 /* FIXME: test Py_LegacyWindowsStdioFlag */
651
652 Py_Initialize();
653 dump_config();
654 Py_Finalize();
655 return 0;
656 }
657
658
659 static int test_init_from_config(void)
660 {
661 PyPreConfig preconfig;
662 _PyPreConfig_InitCompatConfig(&preconfig);
663
664 putenv("PYTHONMALLOC=malloc_debug");
665 #ifndef Py_GIL_DISABLED
666 preconfig.allocator = PYMEM_ALLOCATOR_MALLOC;
667 #else
668 preconfig.allocator = PYMEM_ALLOCATOR_MIMALLOC;
669 #endif
670
671 putenv("PYTHONUTF8=0");
672 Py_UTF8Mode = 0;
673 preconfig.utf8_mode = 1;
674
675 PyStatus status = Py_PreInitialize(&preconfig);
676 if (PyStatus_Exception(status)) {
677 Py_ExitStatusException(status);
678 }
679
680 PyConfig config;
681 _PyConfig_InitCompatConfig(&config);
682
683 config.install_signal_handlers = 0;
684
685 /* FIXME: test use_environment */
686
687 putenv("PYTHONHASHSEED=42");
688 config.use_hash_seed = 1;
689 config.hash_seed = 123;
690
691 /* dev_mode=1 is tested in test_init_dev_mode() */
692
693 putenv("PYTHONFAULTHANDLER=");
694 config.faulthandler = 1;
695
696 putenv("PYTHONTRACEMALLOC=0");
697 config.tracemalloc = 2;
698
699 putenv("PYTHONPROFILEIMPORTTIME=0");
700 config.import_time = 1;
701
702 putenv("PYTHONNODEBUGRANGES=0");
703 config.code_debug_ranges = 0;
704
705 config.show_ref_count = 1;
706 /* FIXME: test dump_refs: bpo-34223 */
707
708 putenv("PYTHONMALLOCSTATS=0");
709 config.malloc_stats = 1;
710
711 putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
712 config_set_string(&config, &config.pycache_prefix, L"conf_pycache_prefix");
713
714 Py_SetProgramName(L"./globalvar");
715 config_set_string(&config, &config.program_name, L"./conf_program_name");
716
717 wchar_t* argv[] = {
718 L"python3",
719 L"-W",
720 L"cmdline_warnoption",
721 L"-X",
722 L"cmdline_xoption",
723 L"-c",
724 L"pass",
725 L"arg2",
726 };
727 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
728 config.parse_argv = 1;
729
730 wchar_t* xoptions[3] = {
731 L"config_xoption1=3",
732 L"config_xoption2=",
733 L"config_xoption3",
734 };
735 config_set_wide_string_list(&config, &config.xoptions,
736 Py_ARRAY_LENGTH(xoptions), xoptions);
737
738 wchar_t* warnoptions[1] = {
739 L"config_warnoption",
740 };
741 config_set_wide_string_list(&config, &config.warnoptions,
742 Py_ARRAY_LENGTH(warnoptions), warnoptions);
743
744 /* FIXME: test pythonpath_env */
745 /* FIXME: test home */
746 /* FIXME: test path config: module_search_path .. dll_path */
747
748 putenv("PYTHONPLATLIBDIR=env_platlibdir");
749 config_set_string(&config, &config.platlibdir, L"my_platlibdir");
750
751 putenv("PYTHONVERBOSE=0");
752 Py_VerboseFlag = 0;
753 config.verbose = 1;
754
755 Py_NoSiteFlag = 0;
756 config.site_import = 0;
757
758 Py_BytesWarningFlag = 0;
759 config.bytes_warning = 1;
760
761 putenv("PYTHONINSPECT=");
762 Py_InspectFlag = 0;
763 config.inspect = 1;
764
765 Py_InteractiveFlag = 0;
766 config.interactive = 1;
767
768 putenv("PYTHONOPTIMIZE=0");
769 Py_OptimizeFlag = 1;
770 config.optimization_level = 2;
771
772 /* FIXME: test parser_debug */
773
774 putenv("PYTHONDONTWRITEBYTECODE=");
775 Py_DontWriteBytecodeFlag = 0;
776 config.write_bytecode = 0;
777
778 Py_QuietFlag = 0;
779 config.quiet = 1;
780
781 config.configure_c_stdio = 1;
782
783 putenv("PYTHONUNBUFFERED=");
784 Py_UnbufferedStdioFlag = 0;
785 config.buffered_stdio = 0;
786
787 putenv("PYTHONIOENCODING=cp424");
788 config_set_string(&config, &config.stdio_encoding, L"iso8859-1");
789 config_set_string(&config, &config.stdio_errors, L"replace");
790
791 putenv("PYTHONNOUSERSITE=");
792 Py_NoUserSiteDirectory = 0;
793 config.user_site_directory = 0;
794
795 config_set_string(&config, &config.check_hash_pycs_mode, L"always");
796
797 Py_FrozenFlag = 0;
798 config.pathconfig_warnings = 0;
799
800 config.safe_path = 1;
801 #ifdef Py_STATS
802 putenv("PYTHONSTATS=");
803 config._pystats = 1;
804 #endif
805
806 putenv("PYTHONINTMAXSTRDIGITS=6666");
807 config.int_max_str_digits = 31337;
808 config.cpu_count = 4321;
809
810 init_from_config_clear(&config);
811
812 dump_config();
813 Py_Finalize();
814 return 0;
815 }
816
817
818 static int check_init_parse_argv(int parse_argv)
819 {
820 PyConfig config;
821 PyConfig_InitPythonConfig(&config);
822
823 config.parse_argv = parse_argv;
824
825 wchar_t* argv[] = {
826 L"./argv0",
827 L"-E",
828 L"-c",
829 L"pass",
830 L"arg1",
831 L"-v",
832 L"arg3",
833 };
834 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
835 init_from_config_clear(&config);
836
837 dump_config();
838 Py_Finalize();
839 return 0;
840 }
841
842
843 static int test_init_parse_argv(void)
844 {
845 return check_init_parse_argv(1);
846 }
847
848
849 static int test_init_dont_parse_argv(void)
850 {
851 return check_init_parse_argv(0);
852 }
853
854
855 static void set_most_env_vars(void)
856 {
857 putenv("PYTHONHASHSEED=42");
858 #ifndef Py_GIL_DISABLED
859 putenv("PYTHONMALLOC=malloc");
860 #else
861 putenv("PYTHONMALLOC=mimalloc");
862 #endif
863 putenv("PYTHONTRACEMALLOC=2");
864 putenv("PYTHONPROFILEIMPORTTIME=1");
865 putenv("PYTHONNODEBUGRANGES=1");
866 putenv("PYTHONMALLOCSTATS=1");
867 putenv("PYTHONUTF8=1");
868 putenv("PYTHONVERBOSE=1");
869 putenv("PYTHONINSPECT=1");
870 putenv("PYTHONOPTIMIZE=2");
871 putenv("PYTHONDONTWRITEBYTECODE=1");
872 putenv("PYTHONUNBUFFERED=1");
873 putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
874 putenv("PYTHONNOUSERSITE=1");
875 putenv("PYTHONFAULTHANDLER=1");
876 putenv("PYTHONIOENCODING=iso8859-1:replace");
877 putenv("PYTHONPLATLIBDIR=env_platlibdir");
878 putenv("PYTHONSAFEPATH=1");
879 putenv("PYTHONINTMAXSTRDIGITS=4567");
880 #ifdef Py_STATS
881 putenv("PYTHONSTATS=1");
882 #endif
883 }
884
885
886 static void set_all_env_vars(void)
887 {
888 set_most_env_vars();
889
890 putenv("PYTHONWARNINGS=EnvVar");
891 putenv("PYTHONPATH=/my/path");
892 }
893
894
895 static int test_init_compat_env(void)
896 {
897 /* Test initialization from environment variables */
898 Py_IgnoreEnvironmentFlag = 0;
899 set_all_env_vars();
900 _testembed_Py_InitializeFromConfig();
901 dump_config();
902 Py_Finalize();
903 return 0;
904 }
905
906
907 static int test_init_python_env(void)
908 {
909 set_all_env_vars();
910
911 PyConfig config;
912 PyConfig_InitPythonConfig(&config);
913
914 config_set_program_name(&config);
915 init_from_config_clear(&config);
916
917 dump_config();
918 Py_Finalize();
919 return 0;
920 }
921
922
923 static void set_all_env_vars_dev_mode(void)
924 {
925 putenv("PYTHONMALLOC=");
926 putenv("PYTHONFAULTHANDLER=");
927 putenv("PYTHONDEVMODE=1");
928 }
929
930
931 static int test_init_env_dev_mode(void)
932 {
933 /* Test initialization from environment variables */
934 Py_IgnoreEnvironmentFlag = 0;
935 set_all_env_vars_dev_mode();
936 _testembed_Py_InitializeFromConfig();
937 dump_config();
938 Py_Finalize();
939 return 0;
940 }
941
942
943 static int test_init_env_dev_mode_alloc(void)
944 {
945 /* Test initialization from environment variables */
946 Py_IgnoreEnvironmentFlag = 0;
947 set_all_env_vars_dev_mode();
948 #ifndef Py_GIL_DISABLED
949 putenv("PYTHONMALLOC=malloc");
950 #else
951 putenv("PYTHONMALLOC=mimalloc");
952 #endif
953 _testembed_Py_InitializeFromConfig();
954 dump_config();
955 Py_Finalize();
956 return 0;
957 }
958
959
960 static int test_init_isolated_flag(void)
961 {
962 /* Test PyConfig.isolated=1 */
963 PyConfig config;
964 PyConfig_InitPythonConfig(&config);
965
966 Py_IsolatedFlag = 0;
967 config.isolated = 1;
968 // These options are set to 1 by isolated=1
969 config.safe_path = 0;
970 config.use_environment = 1;
971 config.user_site_directory = 1;
972
973 config_set_program_name(&config);
974 set_all_env_vars();
975 init_from_config_clear(&config);
976
977 dump_config();
978 Py_Finalize();
979 return 0;
980 }
981
982
983 /* PyPreConfig.isolated=1, PyConfig.isolated=0 */
984 static int test_preinit_isolated1(void)
985 {
986 PyPreConfig preconfig;
987 _PyPreConfig_InitCompatConfig(&preconfig);
988
989 preconfig.isolated = 1;
990
991 PyStatus status = Py_PreInitialize(&preconfig);
992 if (PyStatus_Exception(status)) {
993 Py_ExitStatusException(status);
994 }
995
996 PyConfig config;
997 _PyConfig_InitCompatConfig(&config);
998
999 config_set_program_name(&config);
1000 set_all_env_vars();
1001 init_from_config_clear(&config);
1002
1003 dump_config();
1004 Py_Finalize();
1005 return 0;
1006 }
1007
1008
1009 /* PyPreConfig.isolated=0, PyConfig.isolated=1 */
1010 static int test_preinit_isolated2(void)
1011 {
1012 PyPreConfig preconfig;
1013 _PyPreConfig_InitCompatConfig(&preconfig);
1014
1015 preconfig.isolated = 0;
1016
1017 PyStatus status = Py_PreInitialize(&preconfig);
1018 if (PyStatus_Exception(status)) {
1019 Py_ExitStatusException(status);
1020 }
1021
1022 /* Test PyConfig.isolated=1 */
1023 PyConfig config;
1024 _PyConfig_InitCompatConfig(&config);
1025
1026 Py_IsolatedFlag = 0;
1027 config.isolated = 1;
1028
1029 config_set_program_name(&config);
1030 set_all_env_vars();
1031 init_from_config_clear(&config);
1032
1033 dump_config();
1034 Py_Finalize();
1035 return 0;
1036 }
1037
1038
1039 static int test_preinit_dont_parse_argv(void)
1040 {
1041 PyPreConfig preconfig;
1042 PyPreConfig_InitIsolatedConfig(&preconfig);
1043
1044 preconfig.isolated = 0;
1045
1046 /* -X dev must be ignored by isolated preconfiguration */
1047 wchar_t *argv[] = {L"python3",
1048 L"-E",
1049 L"-I",
1050 L"-P",
1051 L"-X", L"dev",
1052 L"-X", L"utf8",
1053 L"script.py"};
1054 PyStatus status = Py_PreInitializeFromArgs(&preconfig,
1055 Py_ARRAY_LENGTH(argv), argv);
1056 if (PyStatus_Exception(status)) {
1057 Py_ExitStatusException(status);
1058 }
1059
1060 PyConfig config;
1061 PyConfig_InitIsolatedConfig(&config);
1062
1063 config.isolated = 0;
1064
1065 /* Pre-initialize implicitly using argv: make sure that -X dev
1066 is used to configure the allocation in preinitialization */
1067 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
1068 config_set_program_name(&config);
1069 init_from_config_clear(&config);
1070
1071 dump_config();
1072 Py_Finalize();
1073 return 0;
1074 }
1075
1076
1077 static int test_preinit_parse_argv(void)
1078 {
1079 PyConfig config;
1080 PyConfig_InitPythonConfig(&config);
1081
1082 /* Pre-initialize implicitly using argv: make sure that -X dev
1083 is used to configure the allocation in preinitialization */
1084 wchar_t *argv[] = {L"python3", L"-X", L"dev", L"-P", L"script.py"};
1085 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
1086 config_set_program_name(&config);
1087 init_from_config_clear(&config);
1088
1089 dump_config();
1090 Py_Finalize();
1091 return 0;
1092 }
1093
1094
1095
1096
1097 static void set_all_global_config_variables(void)
1098 {
1099 Py_IsolatedFlag = 0;
1100 Py_IgnoreEnvironmentFlag = 0;
1101 Py_BytesWarningFlag = 2;
1102 Py_InspectFlag = 1;
1103 Py_InteractiveFlag = 1;
1104 Py_OptimizeFlag = 1;
1105 Py_DebugFlag = 1;
1106 Py_VerboseFlag = 1;
1107 Py_QuietFlag = 1;
1108 Py_FrozenFlag = 0;
1109 Py_UnbufferedStdioFlag = 1;
1110 Py_NoSiteFlag = 1;
1111 Py_DontWriteBytecodeFlag = 1;
1112 Py_NoUserSiteDirectory = 1;
1113 #ifdef MS_WINDOWS
1114 Py_LegacyWindowsStdioFlag = 1;
1115 #endif
1116 }
1117
1118
1119 static int check_preinit_isolated_config(int preinit)
1120 {
1121 PyStatus status;
1122 PyPreConfig *rt_preconfig;
1123
1124 /* environment variables must be ignored */
1125 set_all_env_vars();
1126
1127 /* global configuration variables must be ignored */
1128 set_all_global_config_variables();
1129
1130 if (preinit) {
1131 PyPreConfig preconfig;
1132 PyPreConfig_InitIsolatedConfig(&preconfig);
1133
1134 status = Py_PreInitialize(&preconfig);
1135 if (PyStatus_Exception(status)) {
1136 Py_ExitStatusException(status);
1137 }
1138
1139 rt_preconfig = &_PyRuntime.preconfig;
1140 assert(rt_preconfig->isolated == 1);
1141 assert(rt_preconfig->use_environment == 0);
1142 }
1143
1144 PyConfig config;
1145 PyConfig_InitIsolatedConfig(&config);
1146
1147 config_set_program_name(&config);
1148 init_from_config_clear(&config);
1149
1150 rt_preconfig = &_PyRuntime.preconfig;
1151 assert(rt_preconfig->isolated == 1);
1152 assert(rt_preconfig->use_environment == 0);
1153
1154 dump_config();
1155 Py_Finalize();
1156 return 0;
1157 }
1158
1159
1160 static int test_preinit_isolated_config(void)
1161 {
1162 return check_preinit_isolated_config(1);
1163 }
1164
1165
1166 static int test_init_isolated_config(void)
1167 {
1168 return check_preinit_isolated_config(0);
1169 }
1170
1171
1172 static int check_init_python_config(int preinit)
1173 {
1174 /* global configuration variables must be ignored */
1175 set_all_global_config_variables();
1176 Py_IsolatedFlag = 1;
1177 Py_IgnoreEnvironmentFlag = 1;
1178 Py_FrozenFlag = 1;
1179 Py_UnbufferedStdioFlag = 1;
1180 Py_NoSiteFlag = 1;
1181 Py_DontWriteBytecodeFlag = 1;
1182 Py_NoUserSiteDirectory = 1;
1183 #ifdef MS_WINDOWS
1184 Py_LegacyWindowsStdioFlag = 1;
1185 #endif
1186
1187 if (preinit) {
1188 PyPreConfig preconfig;
1189 PyPreConfig_InitPythonConfig(&preconfig);
1190
1191 PyStatus status = Py_PreInitialize(&preconfig);
1192 if (PyStatus_Exception(status)) {
1193 Py_ExitStatusException(status);
1194 }
1195 }
1196
1197 PyConfig config;
1198 PyConfig_InitPythonConfig(&config);
1199
1200 config_set_program_name(&config);
1201 init_from_config_clear(&config);
1202
1203 dump_config();
1204 Py_Finalize();
1205 return 0;
1206 }
1207
1208
1209 static int test_preinit_python_config(void)
1210 {
1211 return check_init_python_config(1);
1212 }
1213
1214
1215 static int test_init_python_config(void)
1216 {
1217 return check_init_python_config(0);
1218 }
1219
1220
1221 static int test_init_dont_configure_locale(void)
1222 {
1223 PyPreConfig preconfig;
1224 PyPreConfig_InitPythonConfig(&preconfig);
1225
1226 preconfig.configure_locale = 0;
1227 preconfig.coerce_c_locale = 1;
1228 preconfig.coerce_c_locale_warn = 1;
1229
1230 PyStatus status = Py_PreInitialize(&preconfig);
1231 if (PyStatus_Exception(status)) {
1232 Py_ExitStatusException(status);
1233 }
1234
1235 PyConfig config;
1236 PyConfig_InitPythonConfig(&config);
1237
1238 config_set_program_name(&config);
1239 init_from_config_clear(&config);
1240
1241 dump_config();
1242 Py_Finalize();
1243 return 0;
1244 }
1245
1246
1247 static int test_init_dev_mode(void)
1248 {
1249 PyConfig config;
1250 PyConfig_InitPythonConfig(&config);
1251
1252 putenv("PYTHONFAULTHANDLER=");
1253 putenv("PYTHONMALLOC=");
1254 config.dev_mode = 1;
1255 config_set_program_name(&config);
1256 init_from_config_clear(&config);
1257
1258 dump_config();
1259 Py_Finalize();
1260 return 0;
1261 }
1262
1263 static PyObject *_open_code_hook(PyObject *path, void *data)
1264 {
1265 if (PyUnicode_CompareWithASCIIString(path, "$$test-filename") == 0) {
1266 return PyLong_FromVoidPtr(data);
1267 }
1268 PyObject *io = PyImport_ImportModule("_io");
1269 if (!io) {
1270 return NULL;
1271 }
1272 return PyObject_CallMethod(io, "open", "Os", path, "rb");
1273 }
1274
1275 static int test_open_code_hook(void)
1276 {
1277 int result = 0;
1278
1279 /* Provide a hook */
1280 result = PyFile_SetOpenCodeHook(_open_code_hook, &result);
1281 if (result) {
1282 printf("Failed to set hook\n");
1283 return 1;
1284 }
1285 /* A second hook should fail */
1286 result = PyFile_SetOpenCodeHook(_open_code_hook, &result);
1287 if (!result) {
1288 printf("Should have failed to set second hook\n");
1289 return 2;
1290 }
1291
1292 Py_IgnoreEnvironmentFlag = 0;
1293 _testembed_Py_InitializeFromConfig();
1294 result = 0;
1295
1296 PyObject *r = PyFile_OpenCode("$$test-filename");
1297 if (!r) {
1298 PyErr_Print();
1299 result = 3;
1300 } else {
1301 void *cmp = PyLong_AsVoidPtr(r);
1302 Py_DECREF(r);
1303 if (cmp != &result) {
1304 printf("Did not get expected result from hook\n");
1305 result = 4;
1306 }
1307 }
1308
1309 if (!result) {
1310 PyObject *io = PyImport_ImportModule("_io");
1311 PyObject *r = io
1312 ? PyObject_CallMethod(io, "open_code", "s", "$$test-filename")
1313 : NULL;
1314 if (!r) {
1315 PyErr_Print();
1316 result = 5;
1317 } else {
1318 void *cmp = PyLong_AsVoidPtr(r);
1319 Py_DECREF(r);
1320 if (cmp != &result) {
1321 printf("Did not get expected result from hook\n");
1322 result = 6;
1323 }
1324 }
1325 Py_XDECREF(io);
1326 }
1327
1328 Py_Finalize();
1329 return result;
1330 }
1331
1332 static int _audit_hook_clear_count = 0;
1333
1334 static int _audit_hook(const char *event, PyObject *args, void *userdata)
1335 {
1336 assert(args && PyTuple_CheckExact(args));
1337 if (strcmp(event, "_testembed.raise") == 0) {
1338 PyErr_SetString(PyExc_RuntimeError, "Intentional error");
1339 return -1;
1340 } else if (strcmp(event, "_testembed.set") == 0) {
1341 if (!PyArg_ParseTuple(args, "n", userdata)) {
1342 return -1;
1343 }
1344 return 0;
1345 } else if (strcmp(event, "cpython._PySys_ClearAuditHooks") == 0) {
1346 _audit_hook_clear_count += 1;
1347 }
1348 return 0;
1349 }
1350
1351 static int _test_audit(Py_ssize_t setValue)
1352 {
1353 Py_ssize_t sawSet = 0;
1354
1355 Py_IgnoreEnvironmentFlag = 0;
1356 PySys_AddAuditHook(_audit_hook, &sawSet);
1357 _testembed_Py_InitializeFromConfig();
1358
1359 if (PySys_Audit("_testembed.raise", NULL) == 0) {
1360 printf("No error raised");
1361 return 1;
1362 }
1363 if (PySys_Audit("_testembed.nop", NULL) != 0) {
1364 printf("Nop event failed");
1365 /* Exception from above may still remain */
1366 PyErr_Clear();
1367 return 2;
1368 }
1369 if (!PyErr_Occurred()) {
1370 printf("Exception not preserved");
1371 return 3;
1372 }
1373 PyErr_Clear();
1374
1375 if (PySys_Audit("_testembed.set", "n", setValue) != 0) {
1376 PyErr_Print();
1377 printf("Set event failed");
1378 return 4;
1379 }
1380 if (PyErr_Occurred()) {
1381 printf("Exception raised");
1382 return 5;
1383 }
1384
1385 if (sawSet != 42) {
1386 printf("Failed to see *userData change\n");
1387 return 6;
1388 }
1389
1390 return 0;
1391 }
1392
1393 static int test_audit(void)
1394 {
1395 int result = _test_audit(42);
1396 Py_Finalize();
1397 if (_audit_hook_clear_count != 1) {
1398 return 0x1000 | _audit_hook_clear_count;
1399 }
1400 return result;
1401 }
1402
1403 static int test_audit_tuple(void)
1404 {
1405 #define ASSERT(TEST, EXITCODE) \
1406 if (!(TEST)) { \
1407 printf("ERROR test failed at %s:%i\n", __FILE__, __LINE__); \
1408 return (EXITCODE); \
1409 }
1410
1411 Py_ssize_t sawSet = 0;
1412
1413 // we need at least one hook, otherwise code checking for
1414 // PySys_AuditTuple() is skipped.
1415 PySys_AddAuditHook(_audit_hook, &sawSet);
1416 _testembed_Py_InitializeFromConfig();
1417
1418 ASSERT(!PyErr_Occurred(), 0);
1419
1420 // pass Python tuple object
1421 PyObject *tuple = Py_BuildValue("(i)", 444);
1422 if (tuple == NULL) {
1423 goto error;
1424 }
1425 ASSERT(PySys_AuditTuple("_testembed.set", tuple) == 0, 10);
1426 ASSERT(!PyErr_Occurred(), 11);
1427 ASSERT(sawSet == 444, 12);
1428 Py_DECREF(tuple);
1429
1430 // pass Python int object
1431 PyObject *int_arg = PyLong_FromLong(555);
1432 if (int_arg == NULL) {
1433 goto error;
1434 }
1435 ASSERT(PySys_AuditTuple("_testembed.set", int_arg) == -1, 20);
1436 ASSERT(PyErr_ExceptionMatches(PyExc_TypeError), 21);
1437 PyErr_Clear();
1438 Py_DECREF(int_arg);
1439
1440 // NULL is accepted and means "no arguments"
1441 ASSERT(PySys_AuditTuple("_testembed.test_audit_tuple", NULL) == 0, 30);
1442 ASSERT(!PyErr_Occurred(), 31);
1443
1444 Py_Finalize();
1445 return 0;
1446
1447 error:
1448 PyErr_Print();
1449 return 1;
1450
1451 #undef ASSERT
1452 }
1453
1454 static volatile int _audit_subinterpreter_interpreter_count = 0;
1455
1456 static int _audit_subinterpreter_hook(const char *event, PyObject *args, void *userdata)
1457 {
1458 printf("%s\n", event);
1459 if (strcmp(event, "cpython.PyInterpreterState_New") == 0) {
1460 _audit_subinterpreter_interpreter_count += 1;
1461 }
1462 return 0;
1463 }
1464
1465 static int test_audit_subinterpreter(void)
1466 {
1467 Py_IgnoreEnvironmentFlag = 0;
1468 PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
1469 _testembed_Py_InitializeFromConfig();
1470
1471 Py_NewInterpreter();
1472 Py_NewInterpreter();
1473 Py_NewInterpreter();
1474
1475 Py_Finalize();
1476
1477 switch (_audit_subinterpreter_interpreter_count) {
1478 case 3: return 0;
1479 case 0: return -1;
1480 default: return _audit_subinterpreter_interpreter_count;
1481 }
1482 }
1483
1484 typedef struct {
1485 const char* expected;
1486 int exit;
1487 } AuditRunCommandTest;
1488
1489 static int _audit_hook_run(const char *eventName, PyObject *args, void *userData)
1490 {
1491 AuditRunCommandTest *test = (AuditRunCommandTest*)userData;
1492 if (strcmp(eventName, test->expected)) {
1493 return 0;
1494 }
1495
1496 if (test->exit) {
1497 PyObject *msg = PyUnicode_FromFormat("detected %s(%R)", eventName, args);
1498 if (msg) {
1499 printf("%s\n", PyUnicode_AsUTF8(msg));
1500 Py_DECREF(msg);
1501 }
1502 exit(test->exit);
1503 }
1504
1505 PyErr_Format(PyExc_RuntimeError, "detected %s(%R)", eventName, args);
1506 return -1;
1507 }
1508
1509 static int test_audit_run_command(void)
1510 {
1511 AuditRunCommandTest test = {"cpython.run_command"};
1512 wchar_t *argv[] = {PROGRAM_NAME, L"-c", L"pass"};
1513
1514 Py_IgnoreEnvironmentFlag = 0;
1515 PySys_AddAuditHook(_audit_hook_run, (void*)&test);
1516
1517 return Py_Main(Py_ARRAY_LENGTH(argv), argv);
1518 }
1519
1520 static int test_audit_run_file(void)
1521 {
1522 AuditRunCommandTest test = {"cpython.run_file"};
1523 wchar_t *argv[] = {PROGRAM_NAME, L"filename.py"};
1524
1525 Py_IgnoreEnvironmentFlag = 0;
1526 PySys_AddAuditHook(_audit_hook_run, (void*)&test);
1527
1528 return Py_Main(Py_ARRAY_LENGTH(argv), argv);
1529 }
1530
1531 static int run_audit_run_test(int argc, wchar_t **argv, void *test)
1532 {
1533 PyConfig config;
1534 PyConfig_InitPythonConfig(&config);
1535
1536 config.argv.length = argc;
1537 config.argv.items = argv;
1538 config.parse_argv = 1;
1539 config.program_name = argv[0];
1540 config.interactive = 1;
1541 config.isolated = 0;
1542 config.use_environment = 1;
1543 config.quiet = 1;
1544
1545 PySys_AddAuditHook(_audit_hook_run, test);
1546
1547 PyStatus status = Py_InitializeFromConfig(&config);
1548 if (PyStatus_Exception(status)) {
1549 Py_ExitStatusException(status);
1550 }
1551
1552 return Py_RunMain();
1553 }
1554
1555 static int test_audit_run_interactivehook(void)
1556 {
1557 AuditRunCommandTest test = {"cpython.run_interactivehook", 10};
1558 wchar_t *argv[] = {PROGRAM_NAME};
1559 return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
1560 }
1561
1562 static int test_audit_run_startup(void)
1563 {
1564 AuditRunCommandTest test = {"cpython.run_startup", 10};
1565 wchar_t *argv[] = {PROGRAM_NAME};
1566 return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
1567 }
1568
1569 static int test_audit_run_stdin(void)
1570 {
1571 AuditRunCommandTest test = {"cpython.run_stdin"};
1572 wchar_t *argv[] = {PROGRAM_NAME};
1573 return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
1574 }
1575
1576 static int test_init_read_set(void)
1577 {
1578 PyStatus status;
1579 PyConfig config;
1580 PyConfig_InitPythonConfig(&config);
1581
1582 config_set_string(&config, &config.program_name, L"./init_read_set");
1583
1584 status = PyConfig_Read(&config);
1585 if (PyStatus_Exception(status)) {
1586 goto fail;
1587 }
1588
1589 status = PyWideStringList_Insert(&config.module_search_paths,
1590 1, L"test_path_insert1");
1591 if (PyStatus_Exception(status)) {
1592 goto fail;
1593 }
1594
1595 status = PyWideStringList_Append(&config.module_search_paths,
1596 L"test_path_append");
1597 if (PyStatus_Exception(status)) {
1598 goto fail;
1599 }
1600
1601 /* override executable computed by PyConfig_Read() */
1602 config_set_string(&config, &config.executable, L"my_executable");
1603 init_from_config_clear(&config);
1604
1605 dump_config();
1606 Py_Finalize();
1607 return 0;
1608
1609 fail:
1610 PyConfig_Clear(&config);
1611 Py_ExitStatusException(status);
1612 }
1613
1614
1615 static int test_init_sys_add(void)
1616 {
1617 PySys_AddXOption(L"sysadd_xoption");
1618 PySys_AddXOption(L"faulthandler");
1619 PySys_AddWarnOption(L"ignore:::sysadd_warnoption");
1620
1621 PyConfig config;
1622 PyConfig_InitPythonConfig(&config);
1623
1624 wchar_t* argv[] = {
1625 L"python3",
1626 L"-W",
1627 L"ignore:::cmdline_warnoption",
1628 L"-X",
1629 L"cmdline_xoption",
1630 };
1631 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
1632 config.parse_argv = 1;
1633
1634 PyStatus status;
1635 status = PyWideStringList_Append(&config.xoptions,
1636 L"config_xoption");
1637 if (PyStatus_Exception(status)) {
1638 goto fail;
1639 }
1640
1641 status = PyWideStringList_Append(&config.warnoptions,
1642 L"ignore:::config_warnoption");
1643 if (PyStatus_Exception(status)) {
1644 goto fail;
1645 }
1646
1647 config_set_program_name(&config);
1648 init_from_config_clear(&config);
1649
1650 dump_config();
1651 Py_Finalize();
1652 return 0;
1653
1654 fail:
1655 PyConfig_Clear(&config);
1656 Py_ExitStatusException(status);
1657 }
1658
1659
1660 static int test_init_setpath(void)
1661 {
1662 wchar_t *path = py_getenv("TESTPATH");
1663 if (path == NULL) {
1664 return 1;
1665 }
1666 Py_SetPath(path);
1667 PyMem_RawFree(path);
1668 putenv("TESTPATH=");
1669
1670 Py_Initialize();
1671 dump_config();
1672 Py_Finalize();
1673 return 0;
1674 }
1675
1676
1677 static int test_init_setpath_config(void)
1678 {
1679 PyPreConfig preconfig;
1680 PyPreConfig_InitPythonConfig(&preconfig);
1681
1682 /* Explicitly preinitializes with Python preconfiguration to avoid
1683 Py_SetPath() implicit preinitialization with compat preconfiguration. */
1684 PyStatus status = Py_PreInitialize(&preconfig);
1685 if (PyStatus_Exception(status)) {
1686 Py_ExitStatusException(status);
1687 }
1688
1689 wchar_t *path = py_getenv("TESTPATH");
1690 if (path == NULL) {
1691 return 1;
1692 }
1693 Py_SetPath(path);
1694 PyMem_RawFree(path);
1695 putenv("TESTPATH=");
1696
1697 PyConfig config;
1698 PyConfig_InitPythonConfig(&config);
1699
1700 config_set_string(&config, &config.program_name, L"conf_program_name");
1701 config_set_string(&config, &config.executable, L"conf_executable");
1702 init_from_config_clear(&config);
1703
1704 dump_config();
1705 Py_Finalize();
1706 return 0;
1707 }
1708
1709
1710 static int test_init_setpythonhome(void)
1711 {
1712 wchar_t *home = py_getenv("TESTHOME");
1713 if (home == NULL) {
1714 return 1;
1715 }
1716 Py_SetPythonHome(home);
1717 PyMem_RawFree(home);
1718 putenv("TESTHOME=");
1719
1720 Py_Initialize();
1721 dump_config();
1722 Py_Finalize();
1723 return 0;
1724 }
1725
1726
1727 static int test_init_is_python_build(void)
1728 {
1729 // gh-91985: in-tree builds fail to check for build directory landmarks
1730 // under the effect of 'home' or PYTHONHOME environment variable.
1731 wchar_t *home = py_getenv("TESTHOME");
1732 if (home == NULL) {
1733 return 1;
1734 }
1735
1736 PyConfig config;
1737 _PyConfig_InitCompatConfig(&config);
1738 config_set_program_name(&config);
1739 config_set_string(&config, &config.home, home);
1740 PyMem_RawFree(home);
1741 putenv("TESTHOME=");
1742
1743 // Use an impossible value so we can detect whether it isn't updated
1744 // during initialization.
1745 config._is_python_build = INT_MAX;
1746 char *env = getenv("NEGATIVE_ISPYTHONBUILD");
1747 if (env && strcmp(env, "0") != 0) {
1748 config._is_python_build = INT_MIN;
1749 }
1750 init_from_config_clear(&config);
1751 Py_Finalize();
1752 // Second initialization
1753 config._is_python_build = -1;
1754 init_from_config_clear(&config);
1755 dump_config(); // home and _is_python_build are cached in _Py_path_config
1756 Py_Finalize();
1757 return 0;
1758 }
1759
1760
1761 static int test_init_warnoptions(void)
1762 {
1763 putenv("PYTHONWARNINGS=ignore:::env1,ignore:::env2");
1764
1765 PySys_AddWarnOption(L"ignore:::PySys_AddWarnOption1");
1766 PySys_AddWarnOption(L"ignore:::PySys_AddWarnOption2");
1767
1768 PyConfig config;
1769 PyConfig_InitPythonConfig(&config);
1770
1771 config.dev_mode = 1;
1772 config.bytes_warning = 1;
1773
1774 config_set_program_name(&config);
1775
1776 PyStatus status;
1777 status = PyWideStringList_Append(&config.warnoptions,
1778 L"ignore:::PyConfig_BeforeRead");
1779 if (PyStatus_Exception(status)) {
1780 Py_ExitStatusException(status);
1781 }
1782
1783 wchar_t* argv[] = {
1784 L"python3",
1785 L"-Wignore:::cmdline1",
1786 L"-Wignore:::cmdline2"};
1787 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
1788 config.parse_argv = 1;
1789
1790 status = PyConfig_Read(&config);
1791 if (PyStatus_Exception(status)) {
1792 Py_ExitStatusException(status);
1793 }
1794
1795 status = PyWideStringList_Append(&config.warnoptions,
1796 L"ignore:::PyConfig_AfterRead");
1797 if (PyStatus_Exception(status)) {
1798 Py_ExitStatusException(status);
1799 }
1800
1801 status = PyWideStringList_Insert(&config.warnoptions,
1802 0, L"ignore:::PyConfig_Insert0");
1803 if (PyStatus_Exception(status)) {
1804 Py_ExitStatusException(status);
1805 }
1806
1807 init_from_config_clear(&config);
1808 dump_config();
1809 Py_Finalize();
1810 return 0;
1811 }
1812
1813
1814 static int tune_config(void)
1815 {
1816 PyConfig config;
1817 PyConfig_InitPythonConfig(&config);
1818 if (_PyInterpreterState_GetConfigCopy(&config) < 0) {
1819 PyConfig_Clear(&config);
1820 PyErr_Print();
1821 return -1;
1822 }
1823
1824 config.bytes_warning = 2;
1825
1826 if (_PyInterpreterState_SetConfig(&config) < 0) {
1827 PyConfig_Clear(&config);
1828 return -1;
1829 }
1830 PyConfig_Clear(&config);
1831 return 0;
1832 }
1833
1834
1835 static int test_init_set_config(void)
1836 {
1837 // Initialize core
1838 PyConfig config;
1839 PyConfig_InitIsolatedConfig(&config);
1840 config_set_string(&config, &config.program_name, PROGRAM_NAME);
1841 config._init_main = 0;
1842 config.bytes_warning = 0;
1843 init_from_config_clear(&config);
1844
1845 // Tune the configuration using _PyInterpreterState_SetConfig()
1846 if (tune_config() < 0) {
1847 PyErr_Print();
1848 return 1;
1849 }
1850
1851 // Finish initialization: main part
1852 PyStatus status = _Py_InitializeMain();
1853 if (PyStatus_Exception(status)) {
1854 Py_ExitStatusException(status);
1855 }
1856
1857 dump_config();
1858 Py_Finalize();
1859 return 0;
1860 }
1861
1862
1863 static void configure_init_main(PyConfig *config)
1864 {
1865 wchar_t* argv[] = {
1866 L"python3", L"-c",
1867 (L"import _testinternalcapi, json; "
1868 L"print(json.dumps(_testinternalcapi.get_configs()))"),
1869 L"arg2"};
1870
1871 config->parse_argv = 1;
1872
1873 config_set_argv(config, Py_ARRAY_LENGTH(argv), argv);
1874 config_set_string(config, &config->program_name, L"./python3");
1875 }
1876
1877
1878 static int test_init_run_main(void)
1879 {
1880 PyConfig config;
1881 PyConfig_InitPythonConfig(&config);
1882
1883 configure_init_main(&config);
1884 init_from_config_clear(&config);
1885
1886 return Py_RunMain();
1887 }
1888
1889
1890 static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
1891 {
1892 PyConfig config;
1893 PyConfig_InitPythonConfig(&config);
1894
1895 config.parse_argv = 1;
1896 config_set_argv(&config, argc, argv);
1897 config_set_string(&config, &config.program_name, L"./python3");
1898
1899 init_from_config_clear(&config);
1900
1901 int exitcode = Py_RunMain();
1902 if (exitcode != 123) {
1903 error_fmt("Py_RunMain() returned %i, expected 123", exitcode);
1904 return 1;
1905 }
1906
1907 // If Py_RunMain() calls Py_Exit(), this message is not written to stdout
1908 printf("ok! Py_RunMain() returned 123\n");
1909
1910 return 0;
1911 }
1912
1913
1914 static int test_init_run_main_script_exitcode(void)
1915 {
1916 wchar_t *filename = get_cmdline_arg("FILENAME");
1917 if (filename == NULL) {
1918 return 1;
1919 }
1920
1921 wchar_t* argv[] = {L"python3", filename};
1922 int res = test_init_run_main_exitcode(Py_ARRAY_LENGTH(argv), argv);
1923 PyMem_RawFree(filename);
1924
1925 return res;
1926 }
1927
1928
1929 static int test_init_run_main_module_exitcode(void)
1930 {
1931 wchar_t *module = get_cmdline_arg("MODULE");
1932 if (module == NULL) {
1933 return 1;
1934 }
1935
1936 wchar_t* argv[] = {L"python3", L"-m", module};
1937 int res = test_init_run_main_exitcode(Py_ARRAY_LENGTH(argv), argv);
1938 PyMem_RawFree(module);
1939
1940 return res;
1941 }
1942
1943
1944 static int test_init_run_main_interactive_exitcode(void)
1945 {
1946 wchar_t* argv[] = {L"python3", L"-i"};
1947 return test_init_run_main_exitcode(Py_ARRAY_LENGTH(argv), argv);
1948 }
1949
1950
1951 static int test_init_run_main_code_exitcode(void)
1952 {
1953 wchar_t *code = get_cmdline_arg("CODE");
1954 if (code == NULL) {
1955 return 1;
1956 }
1957
1958 wchar_t* argv[] = {L"python3", L"-c", code};
1959 int res = test_init_run_main_exitcode(Py_ARRAY_LENGTH(argv), argv);
1960 PyMem_RawFree(code);
1961
1962 return res;
1963 }
1964
1965
1966 static int test_init_main(void)
1967 {
1968 PyConfig config;
1969 PyConfig_InitPythonConfig(&config);
1970
1971 configure_init_main(&config);
1972 config._init_main = 0;
1973 init_from_config_clear(&config);
1974
1975 /* sys.stdout don't exist yet: it is created by _Py_InitializeMain() */
1976 int res = PyRun_SimpleString(
1977 "import sys; "
1978 "print('Run Python code before _Py_InitializeMain', "
1979 "file=sys.stderr)");
1980 if (res < 0) {
1981 exit(1);
1982 }
1983
1984 PyStatus status = _Py_InitializeMain();
1985 if (PyStatus_Exception(status)) {
1986 Py_ExitStatusException(status);
1987 }
1988
1989 return Py_RunMain();
1990 }
1991
1992
1993 static int test_run_main(void)
1994 {
1995 PyConfig config;
1996 PyConfig_InitPythonConfig(&config);
1997
1998 wchar_t *argv[] = {L"python3", L"-c",
1999 (L"import sys; "
2000 L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
2001 L"arg2"};
2002 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2003 config_set_string(&config, &config.program_name, L"./python3");
2004 init_from_config_clear(&config);
2005
2006 return Py_RunMain();
2007 }
2008
2009
2010 static int test_run_main_loop(void)
2011 {
2012 // bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
2013 // times must not crash.
2014 for (int i=0; i<5; i++) {
2015 int exitcode = test_run_main();
2016 if (exitcode != 0) {
2017 return exitcode;
2018 }
2019 }
2020 return 0;
2021 }
2022
2023
2024 static int test_get_argc_argv(void)
2025 {
2026 PyConfig config;
2027 PyConfig_InitPythonConfig(&config);
2028
2029 wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
2030 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2031 config_set_string(&config, &config.program_name, L"./python3");
2032
2033 // Calling PyConfig_Read() twice must not change Py_GetArgcArgv() result.
2034 // The second call is done by Py_InitializeFromConfig().
2035 PyStatus status = PyConfig_Read(&config);
2036 if (PyStatus_Exception(status)) {
2037 PyConfig_Clear(&config);
2038 Py_ExitStatusException(status);
2039 }
2040
2041 init_from_config_clear(&config);
2042
2043 int get_argc;
2044 wchar_t **get_argv;
2045 Py_GetArgcArgv(&get_argc, &get_argv);
2046 printf("argc: %i\n", get_argc);
2047 assert(get_argc == Py_ARRAY_LENGTH(argv));
2048 for (int i=0; i < get_argc; i++) {
2049 printf("argv[%i]: %ls\n", i, get_argv[i]);
2050 assert(wcscmp(get_argv[i], argv[i]) == 0);
2051 }
2052
2053 Py_Finalize();
2054
2055 printf("\n");
2056 printf("test ok\n");
2057 return 0;
2058 }
2059
2060
2061 static int check_use_frozen_modules(const char *rawval)
2062 {
2063 wchar_t optval[100];
2064 if (rawval == NULL) {
2065 wcscpy(optval, L"frozen_modules");
2066 }
2067 else {
2068 wchar_t *val = Py_DecodeLocale(rawval, NULL);
2069 if (val == NULL) {
2070 error("unable to decode TESTFROZEN");
2071 return -1;
2072 }
2073 wcscpy(optval, L"frozen_modules=");
2074 if ((wcslen(optval) + wcslen(val)) >= Py_ARRAY_LENGTH(optval)) {
2075 error("TESTFROZEN is too long");
2076 PyMem_RawFree(val);
2077 return -1;
2078 }
2079 wcscat(optval, val);
2080 PyMem_RawFree(val);
2081 }
2082
2083 PyConfig config;
2084 PyConfig_InitPythonConfig(&config);
2085
2086 config.parse_argv = 1;
2087
2088 wchar_t* argv[] = {
2089 L"./argv0",
2090 L"-X",
2091 optval,
2092 L"-c",
2093 L"pass",
2094 };
2095 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2096 init_from_config_clear(&config);
2097
2098 dump_config();
2099 Py_Finalize();
2100 return 0;
2101 }
2102
2103 static int test_init_use_frozen_modules(void)
2104 {
2105 const char *envvar = getenv("TESTFROZEN");
2106 return check_use_frozen_modules(envvar);
2107 }
2108
2109
2110 static int test_unicode_id_init(void)
2111 {
2112 // bpo-42882: Test that _PyUnicode_FromId() works
2113 // when Python is initialized multiples times.
2114
2115 // This is equivalent to `_Py_IDENTIFIER(test_unicode_id_init)`
2116 // but since `_Py_IDENTIFIER` is disabled when `Py_BUILD_CORE`
2117 // is defined, it is manually expanded here.
2118 static _Py_Identifier PyId_test_unicode_id_init = {
2119 .string = "test_unicode_id_init",
2120 .index = -1,
2121 };
2122
2123 // Initialize Python once without using the identifier
2124 _testembed_Py_InitializeFromConfig();
2125 Py_Finalize();
2126
2127 // Now initialize Python multiple times and use the identifier.
2128 // The first _PyUnicode_FromId() call initializes the identifier index.
2129 for (int i=0; i<3; i++) {
2130 _testembed_Py_InitializeFromConfig();
2131
2132 PyObject *str1, *str2;
2133
2134 str1 = _PyUnicode_FromId(&PyId_test_unicode_id_init);
2135 assert(str1 != NULL);
2136 assert(_Py_IsImmortal(str1));
2137
2138 str2 = PyUnicode_FromString("test_unicode_id_init");
2139 assert(str2 != NULL);
2140
2141 assert(PyUnicode_Compare(str1, str2) == 0);
2142
2143 Py_DECREF(str2);
2144
2145 Py_Finalize();
2146 }
2147 return 0;
2148 }
2149
2150
2151 static int test_init_main_interpreter_settings(void)
2152 {
2153 _testembed_Py_Initialize();
2154 (void) PyRun_SimpleStringFlags(
2155 "import _testinternalcapi, json; "
2156 "print(json.dumps(_testinternalcapi.get_interp_settings(0)))",
2157 0);
2158 Py_Finalize();
2159 return 0;
2160 }
2161
2162 static void do_init(void *unused)
2163 {
2164 _testembed_Py_Initialize();
2165 Py_Finalize();
2166 }
2167
2168 static int test_init_in_background_thread(void)
2169 {
2170 PyThread_handle_t handle;
2171 PyThread_ident_t ident;
2172 if (PyThread_start_joinable_thread(&do_init, NULL, &ident, &handle) < 0) {
2173 return -1;
2174 }
2175 return PyThread_join_thread(handle);
2176 }
2177
2178
2179 #ifndef MS_WINDOWS
2180 #include "test_frozenmain.h" // M_test_frozenmain
2181
2182 static int test_frozenmain(void)
2183 {
2184 static struct _frozen frozen_modules[4] = {
2185 {"__main__", M_test_frozenmain, sizeof(M_test_frozenmain)},
2186 {0, 0, 0} // sentinel
2187 };
2188
2189 char* argv[] = {
2190 "./argv0",
2191 "-E",
2192 "arg1",
2193 "arg2",
2194 };
2195 PyImport_FrozenModules = frozen_modules;
2196 return Py_FrozenMain(Py_ARRAY_LENGTH(argv), argv);
2197 }
2198 #endif // !MS_WINDOWS
2199
2200 static int test_repeated_init_and_inittab(void)
2201 {
2202 // bpo-44441: Py_RunMain() must reset PyImport_Inittab at exit.
2203 // It must be possible to call PyImport_AppendInittab() or
2204 // PyImport_ExtendInittab() before each Python initialization.
2205 for (int i=1; i <= INIT_LOOPS; i++) {
2206 printf("--- Pass %d ---\n", i);
2207
2208 // Call PyImport_AppendInittab() at each iteration
2209 if (PyImport_AppendInittab(EMBEDDED_EXT_NAME,
2210 &PyInit_embedded_ext) != 0) {
2211 fprintf(stderr, "PyImport_AppendInittab() failed\n");
2212 return 1;
2213 }
2214
2215 // Initialize Python
2216 wchar_t* argv[] = {PROGRAM_NAME, L"-c", L"pass"};
2217 PyConfig config;
2218 PyConfig_InitPythonConfig(&config);
2219 config.isolated = 1;
2220 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2221 init_from_config_clear(&config);
2222
2223 // Py_RunMain() calls _PyImport_Fini2() which resets PyImport_Inittab
2224 int exitcode = Py_RunMain();
2225 if (exitcode != 0) {
2226 return exitcode;
2227 }
2228 }
2229 return 0;
2230 }
2231
2232 /// Multi-phase initialization package & submodule ///
2233
2234 int
2235 mp_pkg_exec(PyObject *mod)
2236 {
2237 // make this a namespace package
2238 // empty list = namespace package
2239 if (PyModule_Add(mod, "__path__", PyList_New(0)) < 0) {
2240 return -1;
2241 }
2242 if (PyModule_AddStringConstant(mod, "mp_pkg_exec_slot_ran", "yes") < 0) {
2243 return -1;
2244 }
2245 return 0;
2246 }
2247
2248 static PyModuleDef_Slot mp_pkg_slots[] = {
2249 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
2250 {Py_mod_exec, mp_pkg_exec},
2251 {0, NULL}
2252 };
2253
2254 static struct PyModuleDef mp_pkg_def = {
2255 PyModuleDef_HEAD_INIT,
2256 .m_name = "mp_pkg",
2257 .m_size = 0,
2258 .m_slots = mp_pkg_slots,
2259 };
2260
2261 PyMODINIT_FUNC
2262 PyInit_mp_pkg(void)
2263 {
2264 return PyModuleDef_Init(&mp_pkg_def);
2265 }
2266
2267 static PyObject *
2268 submod_greet(PyObject *self, PyObject *Py_UNUSED(ignored))
2269 {
2270 return PyUnicode_FromString("Hello from sub-module");
2271 }
2272
2273 static PyMethodDef submod_methods[] = {
2274 {"greet", submod_greet, METH_NOARGS, NULL},
2275 {NULL},
2276 };
2277
2278 int
2279 mp_submod_exec(PyObject *mod)
2280 {
2281 return PyModule_AddStringConstant(mod, "mp_submod_exec_slot_ran", "yes");
2282 }
2283
2284 static PyModuleDef_Slot mp_submod_slots[] = {
2285 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
2286 {Py_mod_exec, mp_submod_exec},
2287 {0, NULL}
2288 };
2289
2290 static struct PyModuleDef mp_submod_def = {
2291 PyModuleDef_HEAD_INIT,
2292 .m_name = "mp_pkg.mp_submod",
2293 .m_size = 0,
2294 .m_methods = submod_methods,
2295 .m_slots = mp_submod_slots,
2296 };
2297
2298 PyMODINIT_FUNC
2299 PyInit_mp_submod(void)
2300 {
2301 return PyModuleDef_Init(&mp_submod_def);
2302 }
2303
2304 static int
2305 test_inittab_submodule_multiphase(void)
2306 {
2307 wchar_t* argv[] = {
2308 PROGRAM_NAME,
2309 L"-c",
2310 L"import sys;"
2311 L"import mp_pkg.mp_submod;"
2312 L"print(mp_pkg.mp_submod);"
2313 L"print(sys.modules['mp_pkg.mp_submod']);"
2314 L"print(mp_pkg.mp_submod.greet());"
2315 L"print(f'{mp_pkg.mp_submod.mp_submod_exec_slot_ran=}');"
2316 L"print(f'{mp_pkg.mp_pkg_exec_slot_ran=}');"
2317 };
2318 PyConfig config;
2319 if (PyImport_AppendInittab("mp_pkg",
2320 &PyInit_mp_pkg) != 0) {
2321 fprintf(stderr, "PyImport_AppendInittab() failed\n");
2322 return 1;
2323 }
2324 if (PyImport_AppendInittab("mp_pkg.mp_submod",
2325 &PyInit_mp_submod) != 0) {
2326 fprintf(stderr, "PyImport_AppendInittab() failed\n");
2327 return 1;
2328 }
2329 PyConfig_InitPythonConfig(&config);
2330 config.isolated = 1;
2331 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2332 init_from_config_clear(&config);
2333 return Py_RunMain();
2334 }
2335
2336 /// Single-phase initialization package & submodule ///
2337
2338 static struct PyModuleDef sp_pkg_def = {
2339 PyModuleDef_HEAD_INIT,
2340 .m_name = "sp_pkg",
2341 .m_size = 0,
2342 };
2343
2344 PyMODINIT_FUNC
2345 PyInit_sp_pkg(void)
2346 {
2347 PyObject *mod = PyModule_Create(&sp_pkg_def);
2348 if (mod == NULL) {
2349 return NULL;
2350 }
2351 // make this a namespace package
2352 // empty list = namespace package
2353 if (PyModule_Add(mod, "__path__", PyList_New(0)) < 0) {
2354 Py_DECREF(mod);
2355 return NULL;
2356 }
2357 return mod;
2358 }
2359
2360 static struct PyModuleDef sp_submod_def = {
2361 PyModuleDef_HEAD_INIT,
2362 .m_name = "sp_pkg.sp_submod",
2363 .m_size = 0,
2364 .m_methods = submod_methods,
2365 };
2366
2367 PyMODINIT_FUNC
2368 PyInit_sp_submod(void)
2369 {
2370 return PyModule_Create(&sp_submod_def);
2371 }
2372
2373 static int
2374 test_inittab_submodule_singlephase(void)
2375 {
2376 wchar_t* argv[] = {
2377 PROGRAM_NAME,
2378 L"-c",
2379 L"import sys;"
2380 L"import sp_pkg.sp_submod;"
2381 L"print(sp_pkg.sp_submod);"
2382 L"print(sys.modules['sp_pkg.sp_submod']);"
2383 L"print(sp_pkg.sp_submod.greet());"
2384 };
2385 PyConfig config;
2386 if (PyImport_AppendInittab("sp_pkg",
2387 &PyInit_sp_pkg) != 0) {
2388 fprintf(stderr, "PyImport_AppendInittab() failed\n");
2389 return 1;
2390 }
2391 if (PyImport_AppendInittab("sp_pkg.sp_submod",
2392 &PyInit_sp_submod) != 0) {
2393 fprintf(stderr, "PyImport_AppendInittab() failed\n");
2394 return 1;
2395 }
2396 PyConfig_InitPythonConfig(&config);
2397 config.isolated = 1;
2398 config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
2399 init_from_config_clear(&config);
2400 return Py_RunMain();
2401 }
2402
2403 static void wrap_allocator(PyMemAllocatorEx *allocator);
2404 static void unwrap_allocator(PyMemAllocatorEx *allocator);
2405
2406 static void *
2407 malloc_wrapper(void *ctx, size_t size)
2408 {
2409 PyMemAllocatorEx *allocator = (PyMemAllocatorEx *)ctx;
2410 unwrap_allocator(allocator);
2411 PyEval_GetFrame(); // BOOM!
2412 wrap_allocator(allocator);
2413 return allocator->malloc(allocator->ctx, size);
2414 }
2415
2416 static void *
2417 calloc_wrapper(void *ctx, size_t nelem, size_t elsize)
2418 {
2419 PyMemAllocatorEx *allocator = (PyMemAllocatorEx *)ctx;
2420 return allocator->calloc(allocator->ctx, nelem, elsize);
2421 }
2422
2423 static void *
2424 realloc_wrapper(void *ctx, void *ptr, size_t new_size)
2425 {
2426 PyMemAllocatorEx *allocator = (PyMemAllocatorEx *)ctx;
2427 return allocator->realloc(allocator->ctx, ptr, new_size);
2428 }
2429
2430 static void
2431 free_wrapper(void *ctx, void *ptr)
2432 {
2433 PyMemAllocatorEx *allocator = (PyMemAllocatorEx *)ctx;
2434 allocator->free(allocator->ctx, ptr);
2435 }
2436
2437 static void
2438 wrap_allocator(PyMemAllocatorEx *allocator)
2439 {
2440 PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, allocator);
2441 PyMemAllocatorEx wrapper = {
2442 .malloc = &malloc_wrapper,
2443 .calloc = &calloc_wrapper,
2444 .realloc = &realloc_wrapper,
2445 .free = &free_wrapper,
2446 .ctx = allocator,
2447 };
2448 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &wrapper);
2449 }
2450
2451 static void
2452 unwrap_allocator(PyMemAllocatorEx *allocator)
2453 {
2454 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, allocator);
2455 }
2456
2457 static int
2458 test_get_incomplete_frame(void)
2459 {
2460 _testembed_Py_InitializeFromConfig();
2461 PyMemAllocatorEx allocator;
2462 wrap_allocator(&allocator);
2463 // Force an allocation with an incomplete (generator) frame:
2464 int result = PyRun_SimpleString("(_ for _ in ())");
2465 unwrap_allocator(&allocator);
2466 Py_Finalize();
2467 return result;
2468 }
2469
2470
2471 /* *********************************************************
2472 * List of test cases and the function that implements it.
2473 *
2474 * Names are compared case-sensitively with the first
2475 * argument. If no match is found, or no first argument was
2476 * provided, the names of all test cases are printed and
2477 * the exit code will be -1.
2478 *
2479 * The int returned from test functions is used as the exit
2480 * code, and test_capi treats all non-zero exit codes as a
2481 * failed test.
2482 *********************************************************/
2483 struct TestCase
2484 {
2485 const char *name;
2486 int (*func)(void);
2487 };
2488
2489 static struct TestCase TestCases[] = {
2490 // Python initialization
2491 {"test_repeated_init_exec", test_repeated_init_exec},
2492 {"test_repeated_simple_init", test_repeated_simple_init},
2493 {"test_forced_io_encoding", test_forced_io_encoding},
2494 {"test_import_in_subinterpreters", test_import_in_subinterpreters},
2495 {"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
2496 {"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
2497 {"test_pre_initialization_api", test_pre_initialization_api},
2498 {"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
2499 {"test_bpo20891", test_bpo20891},
2500 {"test_initialize_twice", test_initialize_twice},
2501 {"test_initialize_pymain", test_initialize_pymain},
2502 {"test_init_initialize_config", test_init_initialize_config},
2503 {"test_preinit_compat_config", test_preinit_compat_config},
2504 {"test_init_compat_config", test_init_compat_config},
2505 {"test_init_global_config", test_init_global_config},
2506 {"test_init_from_config", test_init_from_config},
2507 {"test_init_parse_argv", test_init_parse_argv},
2508 {"test_init_dont_parse_argv", test_init_dont_parse_argv},
2509 {"test_init_compat_env", test_init_compat_env},
2510 {"test_init_python_env", test_init_python_env},
2511 {"test_init_env_dev_mode", test_init_env_dev_mode},
2512 {"test_init_env_dev_mode_alloc", test_init_env_dev_mode_alloc},
2513 {"test_init_dont_configure_locale", test_init_dont_configure_locale},
2514 {"test_init_dev_mode", test_init_dev_mode},
2515 {"test_init_isolated_flag", test_init_isolated_flag},
2516 {"test_preinit_isolated_config", test_preinit_isolated_config},
2517 {"test_init_isolated_config", test_init_isolated_config},
2518 {"test_preinit_python_config", test_preinit_python_config},
2519 {"test_init_python_config", test_init_python_config},
2520 {"test_preinit_isolated1", test_preinit_isolated1},
2521 {"test_preinit_isolated2", test_preinit_isolated2},
2522 {"test_preinit_parse_argv", test_preinit_parse_argv},
2523 {"test_preinit_dont_parse_argv", test_preinit_dont_parse_argv},
2524 {"test_init_read_set", test_init_read_set},
2525 {"test_init_run_main", test_init_run_main},
2526 {"test_init_run_main_code_exitcode", test_init_run_main_code_exitcode},
2527 {"test_init_run_main_script_exitcode", test_init_run_main_script_exitcode},
2528 {"test_init_run_main_module_exitcode", test_init_run_main_module_exitcode},
2529 {"test_init_run_main_interactive_exitcode", test_init_run_main_interactive_exitcode},
2530 {"test_init_main", test_init_main},
2531 {"test_init_sys_add", test_init_sys_add},
2532 {"test_init_setpath", test_init_setpath},
2533 {"test_init_setpath_config", test_init_setpath_config},
2534 {"test_init_setpythonhome", test_init_setpythonhome},
2535 {"test_init_is_python_build", test_init_is_python_build},
2536 {"test_init_warnoptions", test_init_warnoptions},
2537 {"test_init_set_config", test_init_set_config},
2538 {"test_run_main", test_run_main},
2539 {"test_run_main_loop", test_run_main_loop},
2540 {"test_get_argc_argv", test_get_argc_argv},
2541 {"test_init_use_frozen_modules", test_init_use_frozen_modules},
2542 {"test_init_main_interpreter_settings", test_init_main_interpreter_settings},
2543 {"test_init_in_background_thread", test_init_in_background_thread},
2544
2545 // Audit
2546 {"test_open_code_hook", test_open_code_hook},
2547 {"test_audit", test_audit},
2548 {"test_audit_tuple", test_audit_tuple},
2549 {"test_audit_subinterpreter", test_audit_subinterpreter},
2550 {"test_audit_run_command", test_audit_run_command},
2551 {"test_audit_run_file", test_audit_run_file},
2552 {"test_audit_run_interactivehook", test_audit_run_interactivehook},
2553 {"test_audit_run_startup", test_audit_run_startup},
2554 {"test_audit_run_stdin", test_audit_run_stdin},
2555
2556 // Specific C API
2557 {"test_unicode_id_init", test_unicode_id_init},
2558 #ifndef MS_WINDOWS
2559 {"test_frozenmain", test_frozenmain},
2560 #endif
2561 {"test_get_incomplete_frame", test_get_incomplete_frame},
2562 {"test_inittab_submodule_multiphase", test_inittab_submodule_multiphase},
2563 {"test_inittab_submodule_singlephase", test_inittab_submodule_singlephase},
2564 {NULL, NULL}
2565 };
2566
2567
2568 int main(int argc, char *argv[])
2569 {
2570 main_argc = argc;
2571 main_argv = argv;
2572
2573 if (argc > 1) {
2574 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
2575 if (strcmp(argv[1], tc->name) == 0)
2576 return (*tc->func)();
2577 }
2578 }
2579
2580 /* No match found, or no test name provided, so display usage */
2581 printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
2582 "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
2583 "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
2584 for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
2585 printf(" %s\n", tc->name);
2586 }
2587
2588 /* Non-zero exit code will cause test_embed.py tests to fail.
2589 This is intentional. */
2590 return -1;
2591 }