static _PyInitError _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) { memset(runtime, 0, sizeof(*runtime)); _PyGC_Initialize(&runtime->gc); _PyEval_Initialize(&runtime->ceval); runtime->gilstate.check_enabled = 1; /* A TSS key must be initialized with Py_tss_NEEDS_INIT in accordance with the specification. */ Py_tss_t initial = Py_tss_NEEDS_INIT; runtime->gilstate.autoTSSkey = initial; runtime->interpreters.mutex = PyThread_allocate_lock(); if (runtime->interpreters.mutex == NULL) { return _Py_INIT_ERR("Can't initialize threads for interpreter"); } runtime->interpreters.next_id = -1; runtime->xidregistry.mutex = PyThread_allocate_lock(); if (runtime->xidregistry.mutex == NULL) { return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry"); } return _Py_INIT_OK(); }
static _PyInitError canonicalize(wchar_t *buffer, const wchar_t *path) { if (buffer == NULL) { return _Py_INIT_NO_MEMORY(); } if (_PathCchCanonicalizeEx_Initialized == 0) { HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll"); if (pathapi) { _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx"); } else { _PathCchCanonicalizeEx = NULL; } _PathCchCanonicalizeEx_Initialized = 1; } if (_PathCchCanonicalizeEx) { if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) { return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()"); } } else { if (!PathCanonicalizeW(buffer, path)) { return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()"); } } return _Py_INIT_OK(); }
_PyInitError _PyInterpreterState_Enable(_PyRuntimeState *runtime) { runtime->interpreters.next_id = 0; /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex. Create a new mutex if needed. */ if (runtime->interpreters.mutex == NULL) { /* Force default allocator, since _PyRuntimeState_Fini() must use the same allocator than this function. */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); runtime->interpreters.mutex = PyThread_allocate_lock(); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (runtime->interpreters.mutex == NULL) { return _Py_INIT_ERR("Can't initialize threads for interpreter"); } } return _Py_INIT_OK(); }
static _PyInitError get_program_full_path(const _PyCoreConfig *core_config, PyCalculatePath *calculate, _PyPathConfig *config) { const wchar_t *pyvenv_launcher; wchar_t program_full_path[MAXPATHLEN+1]; memset(program_full_path, 0, sizeof(program_full_path)); /* The launcher may need to force the executable path to a * different environment, so override it here. */ pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__"); if (pyvenv_launcher && pyvenv_launcher[0]) { wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher); } else if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) { /* GetModuleFileName should never fail when passed NULL */ return _Py_INIT_ERR("Cannot determine program path"); } config->program_full_path = PyMem_RawMalloc( sizeof(wchar_t) * (MAXPATHLEN + 1)); return canonicalize(config->program_full_path, program_full_path); }