GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Programs/_testembed.c at main · Python-Repository-Hub/cpython Expand file tree/ Copy path_testembed.cMore file actions 2184 lines (1758 loc) · 56.1 KB / Copy path_testembed.cFile metadata and controls2184 lines (1758 loc) · 56.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #ifndef Py_BUILD_CORE_MODULE # define Py_BUILD_CORE_MODULE #endif /* Always enable assertion (even in release mode) */ #undef NDEBUG #include <Python.h> #include "pycore_initconfig.h" // _PyConfig_InitCompatConfig() #include "pycore_runtime.h" // _PyRuntime #include "pycore_import.h" // _PyImport_FrozenBootstrap #include <inttypes.h> #include <stdio.h> #include <stdlib.h> // putenv() #include <wchar.h> // These functions were removed from Python 3.13 API but are still exported // for the stable ABI. We want to test them in this program. extern void Py_SetProgramName(const wchar_t *program_name); extern void PySys_AddWarnOption(const wchar_t *s); extern void PySys_AddXOption(const wchar_t *s); extern void Py_SetPath(const wchar_t *path); extern void Py_SetPythonHome(const wchar_t *home); int main_argc; char **main_argv; /********************************************************* * Embedded interpreter tests that need a custom exe * * Executed via Lib/test/test_embed.py *********************************************************/ // Use to display the usage #define PROGRAM "test_embed" /* Use path starting with "./" avoids a search along the PATH */ #define PROGRAM_NAME L"./_testembed" #define INIT_LOOPS 4 // Ignore Py_DEPRECATED() compiler warnings: deprecated functions are // tested on purpose here. _Py_COMP_DIAG_PUSH _Py_COMP_DIAG_IGNORE_DEPR_DECLS static void error(const char *msg) { fprintf(stderr, "ERROR: %s\n", msg); fflush(stderr); } static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str) { PyStatus status = PyConfig_SetString(config, config_str, str); if (PyStatus_Exception(status)) { PyConfig_Clear(config); Py_ExitStatusException(status); } } static void config_set_program_name(PyConfig *config) { const wchar_t *program_name = PROGRAM_NAME; config_set_string(config, &config->program_name, program_name); } static void init_from_config_clear(PyConfig *config) { PyStatus status = Py_InitializeFromConfig(config); PyConfig_Clear(config); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } } static void _testembed_Py_InitializeFromConfig(void) { PyConfig config; _PyConfig_InitCompatConfig(&config); config_set_program_name(&config); init_from_config_clear(&config); } static void _testembed_Py_Initialize(void) { Py_SetProgramName(PROGRAM_NAME); Py_Initialize(); } /***************************************************** * Test repeated initialisation and subinterpreters *****************************************************/ static void print_subinterp(void) { /* Output information about the interpreter in the format expected in Lib/test/test_capi.py (test_subinterps). */ PyThreadState *ts = PyThreadState_Get(); PyInterpreterState *interp = ts->interp; int64_t id = PyInterpreterState_GetID(interp); printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ", id, (uintptr_t)interp, (uintptr_t)ts); fflush(stdout); PyRun_SimpleString( "import sys;" "print('id(modules) =', id(sys.modules));" "sys.stdout.flush()" ); } static int test_repeated_init_and_subinterpreters(void) { PyThreadState *mainstate, *substate; PyGILState_STATE gilstate; for (int i=1; i <= INIT_LOOPS; i++) { printf("--- Pass %d ---\n", i); _testembed_Py_InitializeFromConfig(); mainstate = PyThreadState_Get(); PyEval_ReleaseThread(mainstate); gilstate = PyGILState_Ensure(); print_subinterp(); PyThreadState_Swap(NULL); for (int j=0; j<3; j++) { substate = Py_NewInterpreter(); print_subinterp(); Py_EndInterpreter(substate); } PyThreadState_Swap(mainstate); print_subinterp(); PyGILState_Release(gilstate); PyEval_RestoreThread(mainstate); Py_Finalize(); } return 0; } #define EMBEDDED_EXT_NAME "embedded_ext" static PyModuleDef embedded_ext = { PyModuleDef_HEAD_INIT, .m_name = EMBEDDED_EXT_NAME, .m_size = 0, }; static PyObject* PyInit_embedded_ext(void) { return PyModule_Create(&embedded_ext); } /**************************************************************************** * Call Py_Initialize()/Py_Finalize() multiple times and execute Python code ***************************************************************************/ // Used by bpo-46417 to test that structseq types used by the sys module are // cleared properly and initialized again properly when Python is finalized // multiple times. static int test_repeated_init_exec(void) { if (main_argc < 3) { fprintf(stderr, "usage: %s test_repeated_init_exec CODE\n", PROGRAM); exit(1); } const char *code = main_argv[2]; for (int i=1; i <= INIT_LOOPS; i++) { fprintf(stderr, "--- Loop #%d ---\n", i); fflush(stderr); _testembed_Py_InitializeFromConfig(); int err = PyRun_SimpleString(code); Py_Finalize(); if (err) { return 1; } } return 0; } /**************************************************************************** * Test the Py_Initialize(Ex) convenience/compatibility wrappers ***************************************************************************/ // This is here to help ensure there are no wrapper resource leaks (gh-96853) static int test_repeated_simple_init(void) { for (int i=1; i <= INIT_LOOPS; i++) { fprintf(stderr, "--- Loop #%d ---\n", i); fflush(stderr); _testembed_Py_Initialize(); Py_Finalize(); printf("Finalized\n"); // Give test_embed some output to check } return 0; } /***************************************************** * Test forcing a particular IO encoding *****************************************************/ static void check_stdio_details(const wchar_t *encoding, const wchar_t *errors) { /* Output info for the test case to check */ if (encoding) { printf("Expected encoding: %ls\n", encoding); } else { printf("Expected encoding: default\n"); } if (errors) { printf("Expected errors: %ls\n", errors); } else { printf("Expected errors: default\n"); } fflush(stdout); PyConfig config; _PyConfig_InitCompatConfig(&config); /* Force the given IO encoding */ if (encoding) { config_set_string(&config, &config.stdio_encoding, encoding); } if (errors) { config_set_string(&config, &config.stdio_errors, errors); } #ifdef MS_WINDOWS // gh-106659: On Windows, don't use _io._WindowsConsoleIO which always // announce UTF-8 for sys.stdin.encoding. config.legacy_windows_stdio = 1; #endif config_set_program_name(&config); init_from_config_clear(&config); PyRun_SimpleString( "import sys;" "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));" "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));" "sys.stdout.flush()" ); Py_Finalize(); } static int test_forced_io_encoding(void) { /* Check various combinations */ printf("--- Use defaults ---\n"); check_stdio_details(NULL, NULL); printf("--- Set errors only ---\n"); check_stdio_details(NULL, L"ignore"); printf("--- Set encoding only ---\n"); check_stdio_details(L"iso8859-1", NULL); printf("--- Set encoding and errors ---\n"); check_stdio_details(L"iso8859-1", L"replace"); return 0; } /********************************************************* * Test parts of the C-API that work before initialization *********************************************************/ /* The pre-initialization tests tend to break by segfaulting, so explicitly * flushed progress messages make the broken API easier to find when they fail. */ #define _Py_EMBED_PREINIT_CHECK(msg) \ do {printf(msg); fflush(stdout);} while (0); static int test_pre_initialization_api(void) { /* the test doesn't support custom memory allocators */ putenv("PYTHONMALLOC="); /* Leading "./" ensures getpath.c can still find the standard library */ _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n"); wchar_t *program = Py_DecodeLocale("./spam", NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode program name\n"); return 1; } _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n"); Py_SetProgramName(program); _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); Py_Initialize(); _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); PyRun_SimpleString("import sys; " "print('sys.executable:', sys.executable)"); _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); Py_Finalize(); _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n"); PyMem_RawFree(program); return 0; } /* bpo-33042: Ensure embedding apps can predefine sys module options */ static int test_pre_initialization_sys_options(void) { /* We allocate a couple of the options dynamically, and then delete * them before calling Py_Initialize. This ensures the interpreter isn't * relying on the caller to keep the passed in strings alive. */ const wchar_t *static_warnoption = L"once"; const wchar_t *static_xoption = L"also_not_an_option=2"; size_t warnoption_len = wcslen(static_warnoption); size_t xoption_len = wcslen(static_xoption); wchar_t *dynamic_once_warnoption = \ (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t)); wchar_t *dynamic_xoption = \ (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t)); wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1); wcsncpy(dynamic_xoption, static_xoption, xoption_len+1); _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n"); PySys_AddWarnOption(L"default"); _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n"); PySys_ResetWarnOptions(); _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n"); PySys_AddWarnOption(dynamic_once_warnoption); PySys_AddWarnOption(L"module"); PySys_AddWarnOption(L"default"); _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n"); PySys_AddXOption(L"not_an_option=1"); PySys_AddXOption(dynamic_xoption); /* Delete the dynamic options early */ free(dynamic_once_warnoption); dynamic_once_warnoption = NULL; free(dynamic_xoption); dynamic_xoption = NULL; _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); _testembed_Py_InitializeFromConfig(); _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); PyRun_SimpleString("import sys; " "print('sys.warnoptions:', sys.warnoptions); " "print('sys._xoptions:', sys._xoptions); " "warnings = sys.modules['warnings']; " "latest_filters = [f[0] for f in warnings.filters[:3]]; " "print('warnings.filters[:3]:', latest_filters)"); _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); Py_Finalize(); return 0; } /* bpo-20891: Avoid race condition when initialising the GIL */ static void bpo20891_thread(void *lockp) { PyThread_type_lock lock = *((PyThread_type_lock*)lockp); PyGILState_STATE state = PyGILState_Ensure(); if (!PyGILState_Check()) { error("PyGILState_Check failed!"); abort(); } PyGILState_Release(state); PyThread_release_lock(lock); } static int test_bpo20891(void) { /* the test doesn't support custom memory allocators */ putenv("PYTHONMALLOC="); /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not crash. */ PyThread_type_lock lock = PyThread_allocate_lock(); if (!lock) { error("PyThread_allocate_lock failed!"); return 1; } _testembed_Py_InitializeFromConfig(); unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); if (thrd == PYTHREAD_INVALID_THREAD_ID) { error("PyThread_start_new_thread failed!"); return 1; } PyThread_acquire_lock(lock, WAIT_LOCK); Py_BEGIN_ALLOW_THREADS /* wait until the thread exit */ PyThread_acquire_lock(lock, WAIT_LOCK); Py_END_ALLOW_THREADS PyThread_free_lock(lock); Py_Finalize(); return 0; } static int test_initialize_twice(void) { _testembed_Py_InitializeFromConfig(); /* bpo-33932: Calling Py_Initialize() twice should do nothing * (and not crash!). */ Py_Initialize(); Py_Finalize(); return 0; } static int test_initialize_pymain(void) { wchar_t *argv[] = {L"PYTHON", L"-c", (L"import sys; " L"print(f'Py_Main() after Py_Initialize: " L"sys.argv={sys.argv}')"), L"arg2"}; _testembed_Py_InitializeFromConfig(); /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */ Py_Main(Py_ARRAY_LENGTH(argv), argv); Py_Finalize(); return 0; } static void dump_config(void) { (void) PyRun_SimpleStringFlags( "import _testinternalcapi, json; " "print(json.dumps(_testinternalcapi.get_configs()))", 0); } static int test_init_initialize_config(void) { _testembed_Py_InitializeFromConfig(); dump_config(); Py_Finalize(); return 0; } static void config_set_argv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv) { PyStatus status = PyConfig_SetArgv(config, argc, argv); if (PyStatus_Exception(status)) { PyConfig_Clear(config); Py_ExitStatusException(status); } } static void config_set_wide_string_list(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items) { PyStatus status = PyConfig_SetWideStringList(config, list, length, items); if (PyStatus_Exception(status)) { PyConfig_Clear(config); Py_ExitStatusException(status); } } static int check_init_compat_config(int preinit) { PyStatus status; if (preinit) { PyPreConfig preconfig; _PyPreConfig_InitCompatConfig(&preconfig); status = Py_PreInitialize(&preconfig); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } } PyConfig config; _PyConfig_InitCompatConfig(&config); config_set_program_name(&config); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static int test_preinit_compat_config(void) { return check_init_compat_config(1); } static int test_init_compat_config(void) { return check_init_compat_config(0); } static int test_init_global_config(void) { /* FIXME: test Py_IgnoreEnvironmentFlag */ putenv("PYTHONUTF8=0"); Py_UTF8Mode = 1; /* Test initialization from global configuration variables (Py_xxx) */ Py_SetProgramName(L"./globalvar"); /* Py_IsolatedFlag is not tested */ Py_NoSiteFlag = 1; Py_BytesWarningFlag = 1; putenv("PYTHONINSPECT="); Py_InspectFlag = 1; putenv("PYTHONOPTIMIZE=0"); Py_InteractiveFlag = 1; putenv("PYTHONDEBUG=0"); Py_OptimizeFlag = 2; /* Py_DebugFlag is not tested */ putenv("PYTHONDONTWRITEBYTECODE="); Py_DontWriteBytecodeFlag = 1; putenv("PYTHONVERBOSE=0"); Py_VerboseFlag = 1; Py_QuietFlag = 1; Py_NoUserSiteDirectory = 1; putenv("PYTHONUNBUFFERED="); Py_UnbufferedStdioFlag = 1; Py_FrozenFlag = 1; /* FIXME: test Py_LegacyWindowsFSEncodingFlag */ /* FIXME: test Py_LegacyWindowsStdioFlag */ Py_Initialize(); dump_config(); Py_Finalize(); return 0; } static int test_init_from_config(void) { PyPreConfig preconfig; _PyPreConfig_InitCompatConfig(&preconfig); putenv("PYTHONMALLOC=malloc_debug"); preconfig.allocator = PYMEM_ALLOCATOR_MALLOC; putenv("PYTHONUTF8=0"); Py_UTF8Mode = 0; preconfig.utf8_mode = 1; PyStatus status = Py_PreInitialize(&preconfig); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } PyConfig config; _PyConfig_InitCompatConfig(&config); config.install_signal_handlers = 0; /* FIXME: test use_environment */ putenv("PYTHONHASHSEED=42"); config.use_hash_seed = 1; config.hash_seed = 123; /* dev_mode=1 is tested in test_init_dev_mode() */ putenv("PYTHONFAULTHANDLER="); config.faulthandler = 1; putenv("PYTHONTRACEMALLOC=0"); config.tracemalloc = 2; putenv("PYTHONPROFILEIMPORTTIME=0"); config.import_time = 1; putenv("PYTHONNODEBUGRANGES=0"); config.code_debug_ranges = 0; config.show_ref_count = 1; /* FIXME: test dump_refs: bpo-34223 */ putenv("PYTHONMALLOCSTATS=0"); config.malloc_stats = 1; putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); config_set_string(&config, &config.pycache_prefix, L"conf_pycache_prefix"); Py_SetProgramName(L"./globalvar"); config_set_string(&config, &config.program_name, L"./conf_program_name"); wchar_t* argv[] = { L"python3", L"-W", L"cmdline_warnoption", L"-X", L"cmdline_xoption", L"-c", L"pass", L"arg2", }; config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); config.parse_argv = 1; wchar_t* xoptions[3] = { L"config_xoption1=3", L"config_xoption2=", L"config_xoption3", }; config_set_wide_string_list(&config, &config.xoptions, Py_ARRAY_LENGTH(xoptions), xoptions); wchar_t* warnoptions[1] = { L"config_warnoption", }; config_set_wide_string_list(&config, &config.warnoptions, Py_ARRAY_LENGTH(warnoptions), warnoptions); /* FIXME: test pythonpath_env */ /* FIXME: test home */ /* FIXME: test path config: module_search_path .. dll_path */ putenv("PYTHONPLATLIBDIR=env_platlibdir"); config_set_string(&config, &config.platlibdir, L"my_platlibdir"); putenv("PYTHONVERBOSE=0"); Py_VerboseFlag = 0; config.verbose = 1; Py_NoSiteFlag = 0; config.site_import = 0; Py_BytesWarningFlag = 0; config.bytes_warning = 1; putenv("PYTHONINSPECT="); Py_InspectFlag = 0; config.inspect = 1; Py_InteractiveFlag = 0; config.interactive = 1; putenv("PYTHONOPTIMIZE=0"); Py_OptimizeFlag = 1; config.optimization_level = 2; /* FIXME: test parser_debug */ putenv("PYTHONDONTWRITEBYTECODE="); Py_DontWriteBytecodeFlag = 0; config.write_bytecode = 0; Py_QuietFlag = 0; config.quiet = 1; config.configure_c_stdio = 1; putenv("PYTHONUNBUFFERED="); Py_UnbufferedStdioFlag = 0; config.buffered_stdio = 0; putenv("PYTHONIOENCODING=cp424"); config_set_string(&config, &config.stdio_encoding, L"iso8859-1"); config_set_string(&config, &config.stdio_errors, L"replace"); putenv("PYTHONNOUSERSITE="); Py_NoUserSiteDirectory = 0; config.user_site_directory = 0; config_set_string(&config, &config.check_hash_pycs_mode, L"always"); Py_FrozenFlag = 0; config.pathconfig_warnings = 0; config.safe_path = 1; #ifdef Py_STATS putenv("PYTHONSTATS="); config._pystats = 1; #endif putenv("PYTHONINTMAXSTRDIGITS=6666"); config.int_max_str_digits = 31337; init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static int check_init_parse_argv(int parse_argv) { PyConfig config; PyConfig_InitPythonConfig(&config); config.parse_argv = parse_argv; wchar_t* argv[] = { L"./argv0", L"-E", L"-c", L"pass", L"arg1", L"-v", L"arg3", }; config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static int test_init_parse_argv(void) { return check_init_parse_argv(1); } static int test_init_dont_parse_argv(void) { return check_init_parse_argv(0); } static void set_most_env_vars(void) { putenv("PYTHONHASHSEED=42"); putenv("PYTHONMALLOC=malloc"); putenv("PYTHONTRACEMALLOC=2"); putenv("PYTHONPROFILEIMPORTTIME=1"); putenv("PYTHONNODEBUGRANGES=1"); putenv("PYTHONMALLOCSTATS=1"); putenv("PYTHONUTF8=1"); putenv("PYTHONVERBOSE=1"); putenv("PYTHONINSPECT=1"); putenv("PYTHONOPTIMIZE=2"); putenv("PYTHONDONTWRITEBYTECODE=1"); putenv("PYTHONUNBUFFERED=1"); putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); putenv("PYTHONNOUSERSITE=1"); putenv("PYTHONFAULTHANDLER=1"); putenv("PYTHONIOENCODING=iso8859-1:replace"); putenv("PYTHONPLATLIBDIR=env_platlibdir"); putenv("PYTHONSAFEPATH=1"); putenv("PYTHONINTMAXSTRDIGITS=4567"); #ifdef Py_STATS putenv("PYTHONSTATS=1"); #endif } static void set_all_env_vars(void) { set_most_env_vars(); putenv("PYTHONWARNINGS=EnvVar"); putenv("PYTHONPATH=/my/path"); } static int test_init_compat_env(void) { /* Test initialization from environment variables */ Py_IgnoreEnvironmentFlag = 0; set_all_env_vars(); _testembed_Py_InitializeFromConfig(); dump_config(); Py_Finalize(); return 0; } static int test_init_python_env(void) { set_all_env_vars(); PyConfig config; PyConfig_InitPythonConfig(&config); config_set_program_name(&config); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static void set_all_env_vars_dev_mode(void) { putenv("PYTHONMALLOC="); putenv("PYTHONFAULTHANDLER="); putenv("PYTHONDEVMODE=1"); } static int test_init_env_dev_mode(void) { /* Test initialization from environment variables */ Py_IgnoreEnvironmentFlag = 0; set_all_env_vars_dev_mode(); _testembed_Py_InitializeFromConfig(); dump_config(); Py_Finalize(); return 0; } static int test_init_env_dev_mode_alloc(void) { /* Test initialization from environment variables */ Py_IgnoreEnvironmentFlag = 0; set_all_env_vars_dev_mode(); putenv("PYTHONMALLOC=malloc"); _testembed_Py_InitializeFromConfig(); dump_config(); Py_Finalize(); return 0; } static int test_init_isolated_flag(void) { /* Test PyConfig.isolated=1 */ PyConfig config; PyConfig_InitPythonConfig(&config); Py_IsolatedFlag = 0; config.isolated = 1; // These options are set to 1 by isolated=1 config.safe_path = 0; config.use_environment = 1; config.user_site_directory = 1; config_set_program_name(&config); set_all_env_vars(); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } /* PyPreConfig.isolated=1, PyConfig.isolated=0 */ static int test_preinit_isolated1(void) { PyPreConfig preconfig; _PyPreConfig_InitCompatConfig(&preconfig); preconfig.isolated = 1; PyStatus status = Py_PreInitialize(&preconfig); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } PyConfig config; _PyConfig_InitCompatConfig(&config); config_set_program_name(&config); set_all_env_vars(); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } /* PyPreConfig.isolated=0, PyConfig.isolated=1 */ static int test_preinit_isolated2(void) { PyPreConfig preconfig; _PyPreConfig_InitCompatConfig(&preconfig); preconfig.isolated = 0; PyStatus status = Py_PreInitialize(&preconfig); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } /* Test PyConfig.isolated=1 */ PyConfig config; _PyConfig_InitCompatConfig(&config); Py_IsolatedFlag = 0; config.isolated = 1; config_set_program_name(&config); set_all_env_vars(); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static int test_preinit_dont_parse_argv(void) { PyPreConfig preconfig; PyPreConfig_InitIsolatedConfig(&preconfig); preconfig.isolated = 0; /* -X dev must be ignored by isolated preconfiguration */ wchar_t *argv[] = {L"python3", L"-E", L"-I", L"-P", L"-X", L"dev", L"-X", L"utf8", L"script.py"}; PyStatus status = Py_PreInitializeFromArgs(&preconfig, Py_ARRAY_LENGTH(argv), argv); if (PyStatus_Exception(status)) { Py_ExitStatusException(status); } PyConfig config; PyConfig_InitIsolatedConfig(&config); config.isolated = 0; /* Pre-initialize implicitly using argv: make sure that -X dev is used to configure the allocation in preinitialization */ config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); config_set_program_name(&config); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static int test_preinit_parse_argv(void) { PyConfig config; PyConfig_InitPythonConfig(&config); /* Pre-initialize implicitly using argv: make sure that -X dev is used to configure the allocation in preinitialization */ wchar_t *argv[] = {L"python3", L"-X", L"dev", L"-P", L"script.py"}; config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); config_set_program_name(&config); init_from_config_clear(&config); dump_config(); Py_Finalize(); return 0; } static void set_all_global_config_variables(void) { Py_IsolatedFlag = 0; |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |