/* * returns an error code if initialization fails */ int init_with_instance(HMODULE hmod, char *frozen) { int rc; if (!_LocateScript(hmod)) return 255; if (!_LoadPythonDLL(hmod)) return 255; if (p_script_info->unbuffered) { #if defined(MS_WINDOWS) || defined(__CYGWIN__) _setmode(fileno(stdin), O_BINARY); _setmode(fileno(stdout), O_BINARY); #endif #ifdef HAVE_SETVBUF setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); #else /* !HAVE_SETVBUF */ setbuf(stdin, (char *)NULL); setbuf(stdout, (char *)NULL); setbuf(stderr, (char *)NULL); #endif /* !HAVE_SETVBUF */ } if (getenv("PY2EXE_VERBOSE")) Py_VerboseFlag = atoi(getenv("PY2EXE_VERBOSE")); else Py_VerboseFlag = 0; Py_IgnoreEnvironmentFlag = 1; Py_NoSiteFlag = 1; Py_OptimizeFlag = p_script_info->optimize; Py_SetProgramName(modulename); calc_path(); if (!Py_IsInitialized()) { // First time round and the usual case - set sys.path // statically. rc = set_path_early(); if (rc != 0) return rc; // printf("Path before Py_Initialize(): %s\n", Py_GetPath()); Py_Initialize(); // printf("Path after Py_Initialize(): %s\n", PyString_AsString(PyObject_Str(PySys_GetObject("path")))); } else { // Python already initialized. This likely means there are // 2 py2exe based apps in the same process (eg, 2 COM objects // in a single host, 2 ISAPI filters in the same site, ...) // Until we get a better answer, add what we need to sys.path rc = set_path_late(); if (rc != 0) return rc; } /* Set sys.frozen so apps that care can tell. If the caller did pass NULL, sys.frozen will be set zo True. If a string is passed this is used as the frozen attribute. run.c passes "console_exe", run_w.c passes "windows_exe", run_dll.c passes "dll" This falls apart when you consider that in some cases, a single process may end up with two py2exe generated apps - but still, we reset frozen to the correct 'current' value for the newly initializing app. */ if (frozen == NULL) PySys_SetObject("frozen", PyBool_FromLong(1)); else { PyObject *o = PyString_FromString(frozen); if (o) { PySys_SetObject("frozen", o); Py_DECREF(o); } } _TryLoadZlib(hmod); return 0; }
BOOL check_init() { BOOL ok = TRUE; // if already initialized all is good. if (!have_init) { EnterCriticalSection(&csInit); // Check the flag again - another thread may have beat us to it! if (!have_init) { PyGILState_STATE restore_state = PyGILState_UNLOCKED; PyObject *frozen; char dll_path[1024]; char *slash; ok = FALSE; // must be reset after good init. // We must ensure Python is loaded, and therefore the // function pointers are non-NULL, before we can check // if Python is initialized! Further, as our // ISAPI dll depends on python, we must load Python // before loading our module. if (!_LoadPythonDLL(gInstance)) goto done; // Find and load the pyisapi DLL. GetModuleFileName(gInstance, dll_path, sizeof(dll_path)/sizeof(dll_path[0])); slash = strrchr(dll_path, '\\'); if (slash) { // insert an underscore. char *pos_move = dll_path + strlen(dll_path); while (pos_move > slash) { *(pos_move+1) = *pos_move; pos_move --; } *(slash+1) = '_'; hmodPyISAPI = LoadLibrary(dll_path); if (hmodPyISAPI) { pGetExtensionVersion = (__PROC__GetExtensionVersion)GetProcAddress(hmodPyISAPI, "GetExtensionVersion"); pHttpExtensionProc = (__PROC__HttpExtensionProc)GetProcAddress(hmodPyISAPI, "HttpExtensionProc"); pTerminateExtension = (__PROC__TerminateExtension)GetProcAddress(hmodPyISAPI, "TerminateExtension"); pGetFilterVersion = (__PROC__GetFilterVersion)GetProcAddress(hmodPyISAPI, "GetFilterVersion"); pHttpFilterProc = (__PROC__HttpFilterProc)GetProcAddress(hmodPyISAPI, "HttpFilterProc"); pTerminateFilter = (__PROC__TerminateFilter)GetProcAddress(hmodPyISAPI, "TerminateFilter"); pPyISAPISetOptions = (__PROC__PyISAPISetOptions)GetProcAddress(hmodPyISAPI, "PyISAPISetOptions"); pWriteEventLogMessage = (__PROC__WriteEventLogMessage)GetProcAddress(hmodPyISAPI, "WriteEventLogMessage"); } else { SystemError(GetLastError(), "Failed to load the extension DLL"); } } else { SystemError(GetLastError(), "Failed to locate my own DLL"); } if (Py_IsInitialized && Py_IsInitialized()) restore_state = PyGILState_Ensure(); // a little DLL magic. Set sys.frozen='dll' if (init_with_instance(gInstance, "dll") != 0) { PyGILState_Release(restore_state); goto done; } init_memimporter(); frozen = PyInt_FromLong((LONG)gInstance); if (frozen) { PySys_SetObject("frozendllhandle", frozen); Py_DECREF(frozen); } // Now run the generic script - this always returns in a DLL. run_script(); // Let the ISAPI extension know about the frozen environment. // (the ISAPI boot code currently has no way of talking directly // to the pyISAPI dll, so we fetch it directly from the py2exe // 'variable' which is already in module '__main__'. if (pPyISAPISetOptions) { PyObject *main = PyImport_ImportModule("__main__"); if (main) { PyObject *name = PyObject_GetAttrString(main, "isapi_module_name"); char *str; if (name && (str = PyString_AsString(name))) (*pPyISAPISetOptions)(str, TRUE); else PyErr_Clear(); // In case PyString_AsString fails Py_XDECREF(name); } Py_DECREF(main); } have_init = TRUE; PyGILState_Release(restore_state); ok = TRUE; } done: LeaveCriticalSection(&csInit); } return ok; }