int init(char *frozen) { return init_with_instance(NULL, frozen); }
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; }