static void pathconfig_global_init(void) { if (_Py_path_config.module_search_path != NULL) { /* Already initialized */ return; } _PyInitError err; _PyCoreConfig config = _PyCoreConfig_INIT; err = _PyCoreConfig_Read(&config); if (_Py_INIT_FAILED(err)) { goto error; } err = _PyCoreConfig_SetPathConfig(&config); if (_Py_INIT_FAILED(err)) { goto error; } _PyCoreConfig_Clear(&config); return; error: _PyCoreConfig_Clear(&config); _Py_FatalInitError(err); }
static int test_init_dev_mode(void) { _PyCoreConfig config = _PyCoreConfig_INIT; putenv("PYTHONFAULTHANDLER="); putenv("PYTHONMALLOC="); config.dev_mode = 1; config.program_name = L"./_testembed"; _PyInitError err = _Py_InitializeFromConfig(&config); if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } dump_config(); Py_Finalize(); return 0; }
static int test_init_isolated(void) { /* Test _PyCoreConfig.isolated=1 */ _PyCoreConfig config = _PyCoreConfig_INIT; /* Set coerce_c_locale and utf8_mode to not depend on the locale */ config.coerce_c_locale = 0; config.utf8_mode = 0; /* Use path starting with "./" avoids a search along the PATH */ config.program_name = L"./_testembed"; Py_IsolatedFlag = 0; config.isolated = 1; test_init_env_putenvs(); _PyInitError err = _Py_InitializeFromConfig(&config); if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } dump_config(); Py_Finalize(); return 0; }
int Py_FrozenMain(int argc, char **argv) { _PyInitError err = _PyRuntime_Initialize(); if (_Py_INIT_FAILED(err)) { fprintf(stderr, "Fatal Python error: %s\n", err.msg); fflush(stderr); exit(1); } const char *p; int i, n, sts = 1; int inspect = 0; int unbuffered = 0; char *oldloc = NULL; wchar_t **argv_copy = NULL; /* We need a second copies, as Python might modify the first one. */ wchar_t **argv_copy2 = NULL; if (argc > 0) { argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc); argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc); if (!argv_copy || !argv_copy2) { fprintf(stderr, "out of memory\n"); goto error; } } _PyCoreConfig config = _PyCoreConfig_INIT; config._frozen = 1; /* Suppress errors from getpath.c */ if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') inspect = 1; if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') unbuffered = 1; if (unbuffered) { setbuf(stdin, (char *)NULL); setbuf(stdout, (char *)NULL); setbuf(stderr, (char *)NULL); } oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); if (!oldloc) { fprintf(stderr, "out of memory\n"); goto error; } setlocale(LC_ALL, ""); for (i = 0; i < argc; i++) { argv_copy[i] = Py_DecodeLocale(argv[i], NULL); argv_copy2[i] = argv_copy[i]; if (!argv_copy[i]) { fprintf(stderr, "Unable to decode the command line argument #%i\n", i + 1); argc = i; goto error; } } setlocale(LC_ALL, oldloc); PyMem_RawFree(oldloc); oldloc = NULL; #ifdef MS_WINDOWS PyInitFrozenExtensions(); #endif /* MS_WINDOWS */ if (argc >= 1) Py_SetProgramName(argv_copy[0]); err = _Py_InitializeFromConfig(&config); /* No need to call _PyCoreConfig_Clear() since we didn't allocate any memory: program_name is a constant string. */ if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } #ifdef MS_WINDOWS PyWinFreeze_ExeInit(); #endif if (Py_VerboseFlag) fprintf(stderr, "Python %s\n%s\n", Py_GetVersion(), Py_GetCopyright()); PySys_SetArgv(argc, argv_copy); n = PyImport_ImportFrozenModule("__main__"); if (n == 0) Py_FatalError("__main__ not frozen"); if (n < 0) { PyErr_Print(); sts = 1; } else sts = 0; if (inspect && isatty((int)fileno(stdin))) sts = PyRun_AnyFile(stdin, "<stdin>") != 0; #ifdef MS_WINDOWS PyWinFreeze_ExeTerm(); #endif if (Py_FinalizeEx() < 0) { sts = 120; } error: PyMem_RawFree(argv_copy); if (argv_copy2) { for (i = 0; i < argc; i++) PyMem_RawFree(argv_copy2[i]); PyMem_RawFree(argv_copy2); } PyMem_RawFree(oldloc); return sts; }
static int test_init_from_config(void) { /* Test _Py_InitializeFromConfig() */ _PyCoreConfig config = _PyCoreConfig_INIT; config.install_signal_handlers = 0; /* FIXME: test use_environment */ putenv("PYTHONHASHSEED=42"); config.use_hash_seed = 1; config.hash_seed = 123; putenv("PYTHONMALLOC=malloc"); config.allocator = "malloc_debug"; /* 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; config.show_ref_count = 1; config.show_alloc_count = 1; /* FIXME: test dump_refs: bpo-34223 */ putenv("PYTHONMALLOCSTATS=0"); config.malloc_stats = 1; /* FIXME: test coerce_c_locale and coerce_c_locale_warn */ putenv("PYTHONUTF8=0"); Py_UTF8Mode = 0; config.utf8_mode = 1; putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); config.pycache_prefix = L"conf_pycache_prefix"; Py_SetProgramName(L"./globalvar"); config.program_name = L"./conf_program_name"; static wchar_t* argv[2] = { L"-c", L"pass", }; config.argc = Py_ARRAY_LENGTH(argv); config.argv = argv; config.program = L"conf_program"; static wchar_t* xoptions[3] = { L"core_xoption1=3", L"core_xoption2=", L"core_xoption3", }; config.nxoption = Py_ARRAY_LENGTH(xoptions); config.xoptions = xoptions; static wchar_t* warnoptions[2] = { L"default", L"error::ResourceWarning", }; config.nwarnoption = Py_ARRAY_LENGTH(warnoptions); config.warnoptions = warnoptions; /* FIXME: test module_search_path_env */ /* FIXME: test home */ /* FIXME: test path config: module_search_path .. dll_path */ 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; putenv("PYTHONUNBUFFERED="); Py_UnbufferedStdioFlag = 0; config.buffered_stdio = 0; putenv("PYTHONIOENCODING=cp424"); Py_SetStandardStreamEncoding("ascii", "ignore"); #ifdef MS_WINDOWS /* Py_SetStandardStreamEncoding() sets Py_LegacyWindowsStdioFlag to 1. Force it to 0 through the config. */ config.legacy_windows_stdio = 0; #endif config.stdio_encoding = "iso8859-1"; config.stdio_errors = "replace"; putenv("PYTHONNOUSERSITE="); Py_NoUserSiteDirectory = 0; config.user_site_directory = 0; config._check_hash_pycs_mode = "always"; Py_FrozenFlag = 0; config._frozen = 1; _PyInitError err = _Py_InitializeFromConfig(&config); /* Don't call _PyCoreConfig_Clear() since all strings are static */ if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } dump_config(); Py_Finalize(); return 0; }