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 — ircpython/Python/pylifecycle.c at main · 2union/ircpython Expand file tree/ Copy pathpylifecycle.cMore file actions 3565 lines (3007 loc) · 102 KB / Copy pathpylifecycle.cFile metadata and controls3565 lines (3007 loc) · 102 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 /* Python interpreter top-level routines, including init/exit */ #include "Python.h" #include "pycore_audit.h" // _PySys_ClearAuditHooks() #include "pycore_call.h" // _PyObject_CallMethod() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_codecs.h" // _PyCodec_Lookup() #include "pycore_context.h" // _PyContext_Init() #include "pycore_dict.h" // _PyDict_Fini() #include "pycore_exceptions.h" // _PyExc_InitTypes() #include "pycore_fileutils.h" // _Py_ResetForceASCII() #include "pycore_freelist.h" // _PyObject_ClearFreeLists() #include "pycore_floatobject.h" // _PyFloat_InitTypes() #include "pycore_global_objects_fini_generated.h" // "_PyStaticObjects_CheckRefcnt() #include "pycore_import.h" // _PyImport_BootstrapImp() #include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_list.h" // _PyList_Fini() #include "pycore_long.h" // _PyLong_InitTypes() #include "pycore_object.h" // _PyDebug_PrintTotalRefs() #include "pycore_pathconfig.h" // _PyPathConfig_UpdateGlobal() #include "pycore_pyerrors.h" // _PyErr_Occurred() #include "pycore_pylifecycle.h" // _PyErr_Print() #include "pycore_pymem.h" // _PyObject_DebugMallocStats() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime.h" // _Py_ID() #include "pycore_runtime_init.h" // _PyRuntimeState_INIT #include "pycore_setobject.h" // _PySet_NextEntry() #include "pycore_sliceobject.h" // _PySlice_Fini() #include "pycore_sysmodule.h" // _PySys_GetAttr() #include "pycore_traceback.h" // _Py_DumpTracebackThreads() #include "pycore_uniqueid.h" // _PyObject_FinalizeUniqueIdPool() #include "pycore_typeobject.h" // _PyTypes_InitTypes() #include "pycore_typevarobject.h" // _Py_clear_generic_types() #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes() #include "pycore_weakref.h" // _PyWeakref_GET_REF() #include "pycore_obmalloc.h" // _PyMem_init_obmalloc() #include "opcode.h" #include <locale.h> // setlocale() #include <stdlib.h> // getenv() #ifdef HAVE_UNISTD_H # include <unistd.h> // isatty() #endif #if defined(__APPLE__) # include <AvailabilityMacros.h> # include <TargetConditionals.h> # include <mach-o/loader.h> // The os_log unified logging APIs were introduced in macOS 10.12, iOS 10.0, // tvOS 10.0, and watchOS 3.0; # if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE # define HAS_APPLE_SYSTEM_LOG 1 # elif defined(TARGET_OS_OSX) && TARGET_OS_OSX # if defined(MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 # define HAS_APPLE_SYSTEM_LOG 1 # else # define HAS_APPLE_SYSTEM_LOG 0 # endif # else # define HAS_APPLE_SYSTEM_LOG 0 # endif # if HAS_APPLE_SYSTEM_LOG # include <os/log.h> # endif #endif #ifdef HAVE_SIGNAL_H # include <signal.h> // SIG_IGN #endif #ifdef HAVE_LANGINFO_H # include <langinfo.h> // nl_langinfo(CODESET) #endif #ifdef HAVE_FCNTL_H # include <fcntl.h> // F_GETFD #endif #ifdef MS_WINDOWS # undef BYTE #endif #define PUTS(fd, str) (void)_Py_write_noraise(fd, str, (int)strlen(str)) /* Forward declarations */ static PyStatus add_main_module(PyInterpreterState *interp); static PyStatus init_import_site(void); static PyStatus init_set_builtins_open(void); static PyStatus init_sys_streams(PyThreadState *tstate); #ifdef __ANDROID__ static PyStatus init_android_streams(PyThreadState *tstate); #endif #if defined(__APPLE__) && HAS_APPLE_SYSTEM_LOG static PyStatus init_apple_streams(PyThreadState *tstate); #endif static void wait_for_thread_shutdown(PyThreadState *tstate); static void finalize_subinterpreters(void); static void call_ll_exitfuncs(_PyRuntimeState *runtime); /* The following places the `_PyRuntime` structure in a location that can be * found without any external information. This is meant to ease access to the * interpreter state for various runtime debugging tools, but is *not* an * officially supported feature */ /* Suppress deprecation warning for PyBytesObject.ob_shash */ _Py_COMP_DIAG_PUSH _Py_COMP_DIAG_IGNORE_DEPR_DECLS GENERATE_DEBUG_SECTION(PyRuntime, _PyRuntimeState _PyRuntime) = _PyRuntimeState_INIT(_PyRuntime, _Py_Debug_Cookie); _Py_COMP_DIAG_POP static int runtime_initialized = 0; PyStatus _PyRuntime_Initialize(void) { /* XXX We only initialize once in the process, which aligns with the static initialization of the former globals now found in _PyRuntime. However, _PyRuntime *should* be initialized with every Py_Initialize() call, but doing so breaks the runtime. This is because the runtime state is not properly finalized currently. */ if (runtime_initialized) { return _PyStatus_OK(); } runtime_initialized = 1; return _PyRuntimeState_Init(&_PyRuntime); } void _PyRuntime_Finalize(void) { _PyRuntimeState_Fini(&_PyRuntime); runtime_initialized = 0; } int Py_IsFinalizing(void) { return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL; } /* Hack to force loading of object files */ int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ PyOS_mystrnicmp; /* Python/pystrcmp.o */ /* APIs to access the initialization flags * * Can be called prior to Py_Initialize. */ int _Py_IsCoreInitialized(void) { return _PyRuntime.core_initialized; } int Py_IsInitialized(void) { return _PyRuntime.initialized; } /* Helper functions to better handle the legacy C locale * * The legacy C locale assumes ASCII as the default text encoding, which * causes problems not only for the CPython runtime, but also other * components like GNU readline. * * Accordingly, when the CLI detects it, it attempts to coerce it to a * more capable UTF-8 based alternative as follows: * * if (_Py_LegacyLocaleDetected()) { * _Py_CoerceLegacyLocale(); * } * * See the documentation of the PYTHONCOERCECLOCALE setting for more details. * * Locale coercion also impacts the default error handler for the standard * streams: while the usual default is "strict", the default for the legacy * C locale and for any of the coercion target locales is "surrogateescape". */ int _Py_LegacyLocaleDetected(int warn) { #ifndef MS_WINDOWS if (!warn) { const char *locale_override = getenv("LC_ALL"); if (locale_override != NULL && *locale_override != '\0') { /* Don't coerce C locale if the LC_ALL environment variable is set */ return 0; } } /* On non-Windows systems, the C locale is considered a legacy locale */ /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat * the POSIX locale as a simple alias for the C locale, so * we may also want to check for that explicitly. */ const char *ctype_loc = setlocale(LC_CTYPE, NULL); return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0; #else /* Windows uses code pages instead of locales, so no locale is legacy */ return 0; #endif } #ifndef MS_WINDOWS static const char *_C_LOCALE_WARNING = "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII " "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, " "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible " "locales is recommended.\n"; static void emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime) { const PyPreConfig *preconfig = &runtime->preconfig; if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) { PySys_FormatStderr("%s", _C_LOCALE_WARNING); } } #endif /* !defined(MS_WINDOWS) */ typedef struct _CandidateLocale { const char *locale_name; /* The locale to try as a coercion target */ } _LocaleCoercionTarget; static _LocaleCoercionTarget _TARGET_LOCALES[] = { {"C.UTF-8"}, {"C.utf8"}, {"UTF-8"}, {NULL} }; int _Py_IsLocaleCoercionTarget(const char *ctype_loc) { const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { if (strcmp(ctype_loc, target->locale_name) == 0) { return 1; } } return 0; } #ifdef PY_COERCE_C_LOCALE static const char C_LOCALE_COERCION_WARNING[] = "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; static int _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target) { const char *newloc = target->locale_name; /* Reset locale back to currently configured defaults */ _Py_SetLocaleFromEnv(LC_ALL); /* Set the relevant locale environment variable */ if (setenv("LC_CTYPE", newloc, 1)) { fprintf(stderr, "Error setting LC_CTYPE, skipping C locale coercion\n"); return 0; } if (warn) { fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc); } /* Reconfigure with the overridden environment variables */ _Py_SetLocaleFromEnv(LC_ALL); return 1; } #endif int _Py_CoerceLegacyLocale(int warn) { int coerced = 0; #ifdef PY_COERCE_C_LOCALE char *oldloc = NULL; oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL)); if (oldloc == NULL) { return coerced; } const char *locale_override = getenv("LC_ALL"); if (locale_override == NULL || *locale_override == '\0') { /* LC_ALL is also not set (or is set to an empty string) */ const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { const char *new_locale = setlocale(LC_CTYPE, target->locale_name); if (new_locale != NULL) { #if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET) /* Also ensure that nl_langinfo works in this locale */ char *codeset = nl_langinfo(CODESET); if (!codeset || *codeset == '\0') { /* CODESET is not set or empty, so skip coercion */ new_locale = NULL; _Py_SetLocaleFromEnv(LC_CTYPE); continue; } #endif /* Successfully configured locale, so make it the default */ coerced = _coerce_default_locale_settings(warn, target); goto done; } } } /* No C locale warning here, as Py_Initialize will emit one later */ setlocale(LC_CTYPE, oldloc); done: PyMem_RawFree(oldloc); #endif return coerced; } /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to * isolate the idiosyncrasies of different libc implementations. It reads the * appropriate environment variable and uses its value to select the locale for * 'category'. */ char * _Py_SetLocaleFromEnv(int category) { char *res; #ifdef __ANDROID__ const char *locale; const char **pvar; #ifdef PY_COERCE_C_LOCALE const char *coerce_c_locale; #endif const char *utf8_locale = "C.UTF-8"; const char *env_var_set[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL, }; /* Android setlocale(category, "") doesn't check the environment variables * and incorrectly sets the "C" locale at API 24 and older APIs. We only * check the environment variables listed in env_var_set. */ for (pvar=env_var_set; *pvar; pvar++) { locale = getenv(*pvar); if (locale != NULL && *locale != '\0') { if (strcmp(locale, utf8_locale) == 0 || strcmp(locale, "en_US.UTF-8") == 0) { return setlocale(category, utf8_locale); } return setlocale(category, "C"); } } /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string. * Quote from POSIX section "8.2 Internationalization Variables": * "4. If the LANG environment variable is not set or is set to the empty * string, the implementation-defined default locale shall be used." */ #ifdef PY_COERCE_C_LOCALE coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) { /* Some other ported code may check the environment variables (e.g. in * extension modules), so we make sure that they match the locale * configuration */ if (setenv("LC_CTYPE", utf8_locale, 1)) { fprintf(stderr, "Warning: failed setting the LC_CTYPE " "environment variable to %s\n", utf8_locale); } } #endif res = setlocale(category, utf8_locale); #else /* !defined(__ANDROID__) */ res = setlocale(category, ""); #endif _Py_ResetForceASCII(); return res; } static int interpreter_update_config(PyThreadState *tstate, int only_update_path_config) { const PyConfig *config = &tstate->interp->config; if (!only_update_path_config) { PyStatus status = _PyConfig_Write(config, tstate->interp->runtime); if (_PyStatus_EXCEPTION(status)) { _PyErr_SetFromPyStatus(status); return -1; } } if (_Py_IsMainInterpreter(tstate->interp)) { PyStatus status = _PyPathConfig_UpdateGlobal(config); if (_PyStatus_EXCEPTION(status)) { _PyErr_SetFromPyStatus(status); return -1; } } tstate->interp->long_state.max_str_digits = config->int_max_str_digits; // Update the sys module for the new configuration if (_PySys_UpdateConfig(tstate) < 0) { return -1; } return 0; } /* Global initializations. Can be undone by Py_Finalize(). Don't call this twice without an intervening Py_Finalize() call. Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx must have a corresponding call to Py_Finalize. Locking: you must hold the interpreter lock while calling these APIs. (If the lock has not yet been initialized, that's equivalent to having the lock, but you cannot use multiple threads.) */ static PyStatus pyinit_core_reconfigure(_PyRuntimeState *runtime, PyThreadState **tstate_p, const PyConfig *config) { PyStatus status; PyThreadState *tstate = _PyThreadState_GET(); if (!tstate) { return _PyStatus_ERR("failed to read thread state"); } *tstate_p = tstate; PyInterpreterState *interp = tstate->interp; if (interp == NULL) { return _PyStatus_ERR("can't make main interpreter"); } assert(interp->_ready); status = _PyConfig_Write(config, runtime); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyConfig_Copy(&interp->config, config); if (_PyStatus_EXCEPTION(status)) { return status; } config = _PyInterpreterState_GetConfig(interp); if (config->_install_importlib) { status = _PyPathConfig_UpdateGlobal(config); if (_PyStatus_EXCEPTION(status)) { return status; } } return _PyStatus_OK(); } static PyStatus pycore_init_runtime(_PyRuntimeState *runtime, const PyConfig *config) { if (runtime->initialized) { return _PyStatus_ERR("main interpreter already initialized"); } PyStatus status = _PyConfig_Write(config, runtime); if (_PyStatus_EXCEPTION(status)) { return status; } /* Py_Finalize leaves _Py_Finalizing set in order to help daemon * threads behave a little more gracefully at interpreter shutdown. * We clobber it here so the new interpreter can start with a clean * slate. * * However, this may still lead to misbehaviour if there are daemon * threads still hanging around from a previous Py_Initialize/Finalize * pair :( */ _PyRuntimeState_SetFinalizing(runtime, NULL); _Py_InitVersion(); status = _Py_HashRandomization_Init(config); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyImport_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyInterpreterState_Enable(runtime); if (_PyStatus_EXCEPTION(status)) { return status; } return _PyStatus_OK(); } static PyStatus init_interp_settings(PyInterpreterState *interp, const PyInterpreterConfig *config) { assert(interp->feature_flags == 0); if (config->use_main_obmalloc) { interp->feature_flags |= Py_RTFLAGS_USE_MAIN_OBMALLOC; } else if (!config->check_multi_interp_extensions) { /* The reason: PyModuleDef.m_base.m_copy leaks objects between interpreters. */ return _PyStatus_ERR("per-interpreter obmalloc does not support " "single-phase init extension modules"); } #ifdef Py_GIL_DISABLED if (!_Py_IsMainInterpreter(interp) && !config->check_multi_interp_extensions) { return _PyStatus_ERR("The free-threaded build does not support " "single-phase init extension modules in " "subinterpreters"); } #endif if (config->allow_fork) { interp->feature_flags |= Py_RTFLAGS_FORK; } if (config->allow_exec) { interp->feature_flags |= Py_RTFLAGS_EXEC; } // Note that fork+exec is always allowed. if (config->allow_threads) { interp->feature_flags |= Py_RTFLAGS_THREADS; } if (config->allow_daemon_threads) { interp->feature_flags |= Py_RTFLAGS_DAEMON_THREADS; } if (config->check_multi_interp_extensions) { interp->feature_flags |= Py_RTFLAGS_MULTI_INTERP_EXTENSIONS; } switch (config->gil) { case PyInterpreterConfig_DEFAULT_GIL: break; case PyInterpreterConfig_SHARED_GIL: break; case PyInterpreterConfig_OWN_GIL: break; default: return _PyStatus_ERR("invalid interpreter config 'gil' value"); } return _PyStatus_OK(); } static void init_interp_create_gil(PyThreadState *tstate, int gil) { /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is only called here. */ // XXX This is broken with a per-interpreter GIL. _PyEval_FiniGIL(tstate->interp); /* Auto-thread-state API */ _PyGILState_SetTstate(tstate); int own_gil = (gil == PyInterpreterConfig_OWN_GIL); /* Create the GIL and take it */ _PyEval_InitGIL(tstate, own_gil); } static int builtins_dict_watcher(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value) { PyInterpreterState *interp = _PyInterpreterState_GET(); #ifdef _Py_TIER2 if (interp->rare_events.builtin_dict < _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) { _Py_Executors_InvalidateAll(interp, 1); } #endif RARE_EVENT_INTERP_INC(interp, builtin_dict); return 0; } static PyStatus pycore_create_interpreter(_PyRuntimeState *runtime, const PyConfig *src_config, PyThreadState **tstate_p) { PyStatus status; PyInterpreterState *interp; status = _PyInterpreterState_New(NULL, &interp); if (_PyStatus_EXCEPTION(status)) { return status; } assert(interp != NULL); assert(_Py_IsMainInterpreter(interp)); _PyInterpreterState_SetWhence(interp, _PyInterpreterState_WHENCE_RUNTIME); interp->_ready = 1; status = _PyConfig_Copy(&interp->config, src_config); if (_PyStatus_EXCEPTION(status)) { return status; } /* Auto-thread-state API */ status = _PyGILState_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT; // The main interpreter always has its own GIL and supports single-phase // init extensions. config.gil = PyInterpreterConfig_OWN_GIL; config.check_multi_interp_extensions = 0; status = init_interp_settings(interp, &config); if (_PyStatus_EXCEPTION(status)) { return status; } // This could be done in init_interpreter() (in pystate.c) if it // didn't depend on interp->feature_flags being set already. status = _PyObject_InitState(interp); if (_PyStatus_EXCEPTION(status)) { return status; } // initialize the interp->obmalloc state. This must be done after // the settings are loaded (so that feature_flags are set) but before // any calls are made to obmalloc functions. if (_PyMem_init_obmalloc(interp) < 0) { return _PyStatus_NO_MEMORY(); } status = _PyTraceMalloc_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } PyThreadState *tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INIT); if (tstate == NULL) { return _PyStatus_ERR("can't make first thread"); } runtime->main_tstate = tstate; _PyThreadState_Bind(tstate); init_interp_create_gil(tstate, config.gil); *tstate_p = tstate; return _PyStatus_OK(); } static PyStatus pycore_init_global_objects(PyInterpreterState *interp) { PyStatus status; _PyFloat_InitState(interp); status = _PyUnicode_InitGlobalObjects(interp); if (_PyStatus_EXCEPTION(status)) { return status; } _PyUnicode_InitState(interp); if (_Py_IsMainInterpreter(interp)) { _Py_GetConstant_Init(); } return _PyStatus_OK(); } static PyStatus pycore_init_types(PyInterpreterState *interp) { PyStatus status; status = _PyTypes_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyLong_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyUnicode_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyFloat_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } if (_PyExc_InitTypes(interp) < 0) { return _PyStatus_ERR("failed to initialize an exception type"); } status = _PyExc_InitGlobalObjects(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyExc_InitState(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyErr_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyContext_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyXI_InitTypes(interp); if (_PyStatus_EXCEPTION(status)) { return status; } return _PyStatus_OK(); } static PyStatus pycore_init_builtins(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; PyObject *bimod = _PyBuiltin_Init(interp); if (bimod == NULL) { goto error; } PyObject *modules = _PyImport_GetModules(interp); if (_PyImport_FixupBuiltin(tstate, bimod, "builtins", modules) < 0) { goto error; } PyObject *builtins_dict = PyModule_GetDict(bimod); if (builtins_dict == NULL) { goto error; } interp->builtins = Py_NewRef(builtins_dict); PyObject *isinstance = PyDict_GetItemWithError(builtins_dict, &_Py_ID(isinstance)); if (!isinstance) { goto error; } interp->callable_cache.isinstance = isinstance; PyObject *len = PyDict_GetItemWithError(builtins_dict, &_Py_ID(len)); if (!len) { goto error; } interp->callable_cache.len = len; PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append)); if (list_append == NULL) { goto error; } interp->callable_cache.list_append = list_append; PyObject *object__getattribute__ = _PyType_Lookup(&PyBaseObject_Type, &_Py_ID(__getattribute__)); if (object__getattribute__ == NULL) { goto error; } interp->callable_cache.object__getattribute__ = object__getattribute__; if (_PyBuiltins_AddExceptions(bimod) < 0) { return _PyStatus_ERR("failed to add exceptions to builtins"); } interp->builtins_copy = PyDict_Copy(interp->builtins); if (interp->builtins_copy == NULL) { goto error; } Py_DECREF(bimod); if (_PyImport_InitDefaultImportFunc(interp) < 0) { goto error; } assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); error: Py_XDECREF(bimod); return _PyStatus_ERR("can't initialize builtins module"); } static PyStatus pycore_interp_init(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; PyStatus status; PyObject *sysmod = NULL; // Create singletons before the first PyType_Ready() call, since // PyType_Ready() uses singletons like the Unicode empty string (tp_doc) // and the empty tuple singletons (tp_bases). status = pycore_init_global_objects(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyCode_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyDtoa_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } // The GC must be initialized before the first GC collection. status = _PyGC_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = pycore_init_types(interp); if (_PyStatus_EXCEPTION(status)) { goto done; } if (_PyWarnings_InitState(interp) < 0) { return _PyStatus_ERR("can't initialize warnings"); } status = _PyAtExit_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PySys_Create(tstate, &sysmod); if (_PyStatus_EXCEPTION(status)) { goto done; } status = pycore_init_builtins(tstate); if (_PyStatus_EXCEPTION(status)) { goto done; } status = _PyXI_Init(interp); if (_PyStatus_EXCEPTION(status)) { goto done; } const PyConfig *config = _PyInterpreterState_GetConfig(interp); status = _PyImport_InitCore(tstate, sysmod, config->_install_importlib); if (_PyStatus_EXCEPTION(status)) { goto done; } done: /* sys.modules['sys'] contains a strong reference to the module */ Py_XDECREF(sysmod); return status; } static PyStatus pyinit_config(_PyRuntimeState *runtime, PyThreadState **tstate_p, const PyConfig *config) { PyStatus status = pycore_init_runtime(runtime, config); if (_PyStatus_EXCEPTION(status)) { return status; } PyThreadState *tstate; status = pycore_create_interpreter(runtime, config, &tstate); if (_PyStatus_EXCEPTION(status)) { return status; } *tstate_p = tstate; status = pycore_interp_init(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } /* Only when we get here is the runtime core fully initialized */ runtime->core_initialized = 1; return _PyStatus_OK(); } PyStatus _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args) { PyStatus status; if (src_config == NULL) { return _PyStatus_ERR("preinitialization config is NULL"); } status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; if (runtime->preinitialized) { /* If it's already configured: ignored the new configuration */ return _PyStatus_OK(); } /* Note: preinitialized remains 1 on error, it is only set to 0 at exit on success. */ runtime->preinitializing = 1; PyPreConfig config; status = _PyPreConfig_InitFromPreConfig(&config, src_config); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyPreConfig_Read(&config, args); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyPreConfig_Write(&config); if (_PyStatus_EXCEPTION(status)) { return status; } runtime->preinitializing = 0; runtime->preinitialized = 1; return _PyStatus_OK(); } PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv) { _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv}; return _Py_PreInitializeFromPyArgv(src_config, &args); } PyStatus Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv) { _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv}; return _Py_PreInitializeFromPyArgv(src_config, &args); } PyStatus Py_PreInitialize(const PyPreConfig *src_config) { return _Py_PreInitializeFromPyArgv(src_config, NULL); } |
| You are visitor 01402775 — 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. |