/* * Create a new instance of the plugin i.e. allocate our private storage */ static bRC newPlugin(bpContext *ctx) { struct plugin_ctx *p_ctx; p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx)); if (!p_ctx) { return bRC_Error; } memset(p_ctx, 0, sizeof(struct plugin_ctx)); ctx->pContext = (void *)p_ctx; /* set our context pointer */ /* * For each plugin instance we instantiate a new Python interpreter. */ PyEval_AcquireLock(); p_ctx->interpreter = Py_NewInterpreter(); PyEval_ReleaseThread(p_ctx->interpreter); /* * Always register some events the python plugin itself can register * any other events it is interested in. */ bfuncs->registerBareosEvents(ctx, 1, bsdEventNewPluginOptions); return bRC_OK; }
/* ---------------------------------------------------------------------------*/ char * get_ec2_imgname(struct ec2_subscription* subptr, char *imgname,char * zone) { char srcdir[1024]; PyObject *pName=NULL, *pModule=NULL, *pDict=NULL, *pFunc=NULL,*result=NULL; PyThreadState* pythr=NULL; char * response; //python interface sprintf(srcdir,"%s/pyaccords/pysrc",PYPATH); pythr = Py_NewInterpreter(); python_path(srcdir); pName = PyString_FromString("ec2client"); if(pName == NULL) printf("erro: in ec2client.py no such file name\n"); else pModule = PyImport_Import(pName); if(pModule == NULL) printf("error: failed to load ec2client module\n"); else pDict = PyModule_GetDict(pModule); if(pDict == NULL) printf("error: failed to load dict name in ec2client module\n"); else pFunc = PyDict_GetItemString(pDict,"ec2_get_image"); if(pFunc == NULL) printf("error: failed to load ec2_get_image function in ec2client module\n"); else result=PyObject_CallFunction(pFunc,"ssss",subptr->accesskey,subptr->secretkey,imgname,zone); if (!result || PyErr_Occurred()) { PyErr_Print(); return (0); } response=allocate_string(PyString_AsString( result )); Py_DECREF(pModule); Py_DECREF(pName); Py_EndInterpreter(pythr); return response; }
static int test_repeated_init_and_subinterpreters(void) { PyThreadState *mainstate, *substate; PyGILState_STATE gilstate; int i, j; for (i=0; i<15; i++) { printf("--- Pass %d ---\n", i); _testembed_Py_Initialize(); mainstate = PyThreadState_Get(); PyEval_InitThreads(); PyEval_ReleaseThread(mainstate); gilstate = PyGILState_Ensure(); print_subinterp(); PyThreadState_Swap(NULL); for (j=0; j<3; j++) { substate = Py_NewInterpreter(); print_subinterp(); Py_EndInterpreter(substate); } PyThreadState_Swap(mainstate); print_subinterp(); PyGILState_Release(gilstate); PyEval_RestoreThread(mainstate); Py_Finalize(); } return 0; }
int main(int argc, char *argv[]) { int ret = 0; /* Must be the first thing we do to get everything else started * We can do this here even though Py_Main will call those again. * However, we must run them before creating our sub-interpreter. * * See comments on ARGV0 as to why we set it like this. */ Py_SetProgramName(ARGV0); Py_Initialize(); /* Create sub-interpreter */ #ifdef DEBUG fprintf(stderr, "starting new interpreter\n"); #endif shd_py_interpreter = Py_NewInterpreter(); if(!shd_py_interpreter) { PyErr_Print(); return 1; } PyThreadState *old = PyThreadState_Swap(shd_py_interpreter); ret = python_main(argc, argv); #ifdef DEBUG fprintf(stderr, "shutting down interpreter\n"); #endif PyThreadState_Swap(shd_py_interpreter); Py_EndInterpreter(shd_py_interpreter); shd_py_interpreter = NULL; PyThreadState_Swap(old); Py_Finalize(); return ret; #undef PYERR }
/* ----------------------------------------------------------------- */ char * get_ec2_flavor(int memory, int cores, int speed, int storage, char * architecture) { char srcdir[1024]; PyObject *pName=NULL, *pModule=NULL, *pDict=NULL, *pFunc=NULL,*result=NULL; PyThreadState* pythr=NULL; char * response; //python interface sprintf(srcdir,"%s/pyaccords/pysrc",PYPATH); pythr = Py_NewInterpreter(); python_path(srcdir); pName = PyString_FromString("ec2client"); if(pName == NULL) printf("erro: in ec2client.py no such file name\n"); else pModule = PyImport_Import(pName); if(pModule == NULL) printf("error: failed to load ec2client module\n"); else pDict = PyModule_GetDict(pModule); if(pDict == NULL) printf("error: failed to load dict name in ec2client module\n"); else pFunc = PyDict_GetItemString(pDict,"ec2_get_flavor"); if(pFunc == NULL) printf("error: failed to load ec2_get_flavor function in ec2client module\n"); else result=PyObject_CallFunction(pFunc,"iiiis",memory,cores,speed,storage,architecture); if (!result || PyErr_Occurred()) { PyErr_Print(); return (0); } response=allocate_string(PyString_AsString( result )); Py_DECREF(pModule); Py_DECREF(pName); Py_EndInterpreter(pythr); return response; }
int main() { Py_Initialize(); PyEval_InitThreads(); PyRun_SimpleString(cmd); Py_EndInterpreter(PyThreadState_Get()); Py_NewInterpreter(); PyRun_SimpleString(cmd); Py_Finalize(); return 0; }
void Effect::run() { // switch to the main thread state and acquire the GIL PyEval_RestoreThread(_mainThreadState); // Initialize a new thread state _interpreterThreadState = Py_NewInterpreter(); // import the buildtin Hyperion module PyObject * module = PyImport_ImportModule("hyperion"); // add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr)); // add ledCount variable to the interpreter PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount())); // add a args variable to the interpreter PyObject_SetAttrString(module, "args", json2python(_args)); // decref the module Py_XDECREF(module); // Set the end time if applicable if (_timeout > 0) { _endTime = QDateTime::currentMSecsSinceEpoch() + _timeout; } // Run the effect script FILE* file = fopen(_script.c_str(), "r"); if (file != nullptr) { PyRun_SimpleFile(file, _script.c_str()); } else { std::cerr << "EFFECTENGINE ERROR: Unable to open script file " << _script << std::endl; } fclose(file); // Clean up the thread state Py_EndInterpreter(_interpreterThreadState); _interpreterThreadState = nullptr; PyEval_ReleaseLock(); }
static void pygwy_plugin_run(GwyContainer *data, GwyRunType run, const gchar *name) { PygwyPluginInfo *info; PyThreadState* py_thread_state = NULL; FILE *pyfile; if (!(info = pygwy_find_plugin(name))) { g_warning("cannot find plugin."); return; } /* open script file */ pyfile = fopen(info->filename, "r"); if (!pyfile) { g_warning("Cannot find pygwy script file '%s'", info->filename); return; } /* Initialize the Python interpreter. Required. */ if (!Py_IsInitialized()) { gwy_debug("Initializing Python interpreter" ); // Do not register signal handlers Py_InitializeEx(0); gwy_debug("Initializing Pygwy classes."); initpygwy(data); } else { gwy_debug("Python interpreter already initialized"); } /* initialize module class */ gwy_debug("Running plugin '%s', filename '%s'", info->name, info->filename); py_thread_state = Py_NewInterpreter(); initpygwy(data); /* Run pyfile */ PyRun_AnyFile(pyfile, info->filename); gwy_debug("Clear interpreter"); /* Cleaning interpreter */ Py_EndInterpreter(py_thread_state); // Py_Finalize(); fclose(pyfile); }
PythonInterpreter::PythonInterpreter(PythonOutputStream* pyStdOut, PythonOutputStream* pyStdErr) { std::lock_guard<std::mutex> lock(globalMutex); // Acquire the global interpreter lock. if (pythonInitialised) PyEval_AcquireLock(); else { PyEval_InitThreads(); Py_Initialize(); pythonInitialised = true; } // Create the new interpreter. state = Py_NewInterpreter(); // Record the main module. mainModule = PyImport_AddModule("__main__"); // Borrowed ref. mainNamespace = PyModule_GetDict(mainModule); // Borrowed ref. // Redirect stdout and stderr if appropriate. if (pyStdOut || pyStdErr) { boost::python::class_<PythonOutputStream, boost::noncopyable> ("PythonOutputStream", boost::python::no_init) .def("write", &PythonOutputStream::write) .def("flush", &PythonOutputStream::flush); boost::python::reference_existing_object:: apply<PythonOutputStream*>::type conv; if (pyStdOut) PySys_SetObject(const_cast<char*>("stdout"), conv(pyStdOut)); if (pyStdErr) PySys_SetObject(const_cast<char*>("stderr"), conv(pyStdErr)); } // Release the global interpreter lock. PyEval_SaveThread(); }
static bRC newPlugin(bpContext *ctx) { bRC retval = bRC_Error; struct plugin_ctx *p_ctx; p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx)); if (!p_ctx) { return bRC_Error; } memset(p_ctx, 0, sizeof(struct plugin_ctx)); ctx->pContext = (void *)p_ctx; /* set our context pointer */ /* * For each plugin instance we instantiate a new Python interpreter. */ PyEval_AcquireLock(); p_ctx->interpreter = Py_NewInterpreter(); retval = PyLoadModule(ctx); PyEval_ReleaseThread(p_ctx->interpreter); return retval; }
//------------------------------------------------------------------------------------- PyThreadState* Script::createInterpreter() { PyThreadState* pCurInterpreter = PyThreadState_Get(); PyObject * pCurPath = PySys_GetObject( "path" ); PyThreadState* pNewInterpreter = Py_NewInterpreter(); if (pNewInterpreter) { PySys_SetObject( "path", pCurPath ); #ifndef KBE_SINGLE_THREADED PyDict_Merge( PySys_GetObject( "modules" ), s_pOurInitTimeModules, 0 ); #endif PyThreadState* pSwapped = PyThreadState_Swap( pCurInterpreter ); if( pSwapped != pNewInterpreter ) { KBE_EXIT( "error creating new python interpreter" ); } } return pNewInterpreter; }
/* * Create a new instance of the plugin i.e. allocate our private storage */ static bRC newPlugin(bpContext *ctx) { #ifdef HAVE_PYTHON struct plugin_ctx *p_ctx; p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx)); if (!p_ctx) { return bRC_Error; } memset(p_ctx, 0, sizeof(struct plugin_ctx)); ctx->pContext = (void *)p_ctx; /* set our context pointer */ /* * For each plugin instance we instantiate a new Python interpreter. */ PyEval_AcquireLock(); p_ctx->interpreter = Py_NewInterpreter(); PyEval_ReleaseThread(p_ctx->interpreter); #endif return bRC_OK; }
/** * z_policy_thread_new: * @policy: The policy to create a thread for * * Constructor of ZPolicyThread. If the policy already has a main thread, * only acquire a new ThreadState instance (start a new thread in that * interpreter), else start a new interpreter. * * Returns: * The new instance */ ZPolicyThread * z_policy_thread_new(ZPolicy *policy) { ZPolicyThread *self = g_new0(ZPolicyThread, 1); /* NOTE: requires the interpreter lock to be held */ self->startable = FALSE; self->startable_lock = g_mutex_new(); self->startable_signal = g_cond_new(); self->policy = z_policy_ref(policy); if (policy->main_thread) { self->thread = PyThreadState_New(self->policy->main_thread->thread->interp); } else { /* initialize a new interpreter instance */ self->thread = Py_NewInterpreter(); PyThreadState_Swap(NULL); } return self; }
void IvrPython::onNotify(AmSessionEvent* event) { if (onNotifyCallback == NULL) { DBG("IvrPython::onNotify, but script did not set onNotify callback!\n"); return; } DBG("IvrPython::onNotify(): calling onNotifyCallback ...\n"); #ifndef IVR_PERL PyThreadState* pyThreadState; if ( (pyThreadState = Py_NewInterpreter()) != NULL){ PyObject *arglist; PyObject *result; arglist = Py_BuildValue("(s)", event->request.getBody().c_str());; result = PyEval_CallObject(onNotifyCallback, arglist); Py_DECREF(arglist); if (result == NULL) { DBG("Calling IVR" SCRIPT_TYPE "onNotify failed.\n"); // PyErr_Print(); return ; } Py_DECREF(result); } Py_EndInterpreter(pyThreadState); #else //IVR_PERL PERL_SET_CONTEXT(my_perl_interp); DBG("context is %ld\n", (long) Perl_get_context()); dSP ; ENTER ; SAVETMPS ; PUSHMARK(SP) ; XPUSHs(sv_2mortal(newSVpv((event->request.getBody().c_str()), 0))); PUTBACK ; call_pv(onNotifyCallback, G_DISCARD); FREETMPS ; LEAVE ; #endif //IVR_PERL }
void Script::initThread( bool plusOwnInterpreter ) { if( s_defaultContext != NULL ) { KBE_EXIT( "trying to initialise scripting when already initialised" ); } PyEval_AcquireLock(); PyThreadState * newTState = NULL; if (plusOwnInterpreter) { newTState = Py_NewInterpreter(); PyObject * pMainPyPath = PyDict_GetItemString( s_pMainThreadState->interp->sysdict, "path" ); PySys_SetObject( "path", pMainPyPath ); PyDict_Merge( PySys_GetObject( "modules" ), s_pOurInitTimeModules, 0); } else { newTState = PyThreadState_New( s_pMainThreadState->interp ); } if( newTState == NULL ) { KBE_EXIT( "failed to create a new thread object" ); } PyEval_ReleaseLock(); s_defaultContext = newTState; Script::acquireLock(); }
pythonInterpreterWrapper::pythonInterpreterWrapper ( const objectRegistry& obr, const dictionary& dict, bool forceToNamespace ): generalInterpreterWrapperCRTP<pythonInterpreterWrapper>( obr, dict, forceToNamespace, "python" ), pythonState_(NULL), useNumpy_(dict.lookupOrDefault<bool>("useNumpy",true)), useIPython_(dict.lookupOrDefault<bool>("useIPython",true)), triedIPython_(false), oldIPython_(false) { if(generalInterpreterWrapper::debug>debug) { debug=1; } Pbug << "Starting constructor" << endl; syncParallel(); #ifdef FOAM_HAS_LOCAL_DEBUGSWITCHES debug=dict.lookupOrDefault<label>("debugPythonWrapper",debug()); #else debug=dict.lookupOrDefault<label>("debugPythonWrapper",debug); #endif if(!dict.found("useNumpy")) { WarningIn("pythonInterpreterWrapper::pythonInterpreterWrapper") << "Switch 'useNumpy' not found in " << dict.name() << nl << "Assuming it to be 'true' (if that is not what you want " << "set it. Also set it to make this warning go away)" << endl; } if(!dict.found("useIPython")) { WarningIn("pythonInterpreterWrapper::pythonInterpreterWrapper") << "Switch 'useIPython' not found in " << dict.name() << nl << "Assuming it to be 'true' (if that is not what you want " << "set it. Also set it to make this warning go away)" << endl; } if(interpreterCount==0) { Pbug << "Initializing Python" << endl; Py_Initialize(); if(debug) { PyThreadState *current=PyGILState_GetThisThreadState(); Pbug << "GIL-state before thread" << getHex(current) << endl; } PyEval_InitThreads(); if(debug) { PyThreadState *current=PyGILState_GetThisThreadState(); Pbug << "GIL-state after thread" << getHex(current) << endl; } // importLib("scipy.stats","stats"); - OK mainThreadState = PyEval_SaveThread(); // importLib("scipy.stats","stats"); - segFault Pbug << "Main thread state: " << getHex(mainThreadState) << endl; // PyRun_SimpleString("import IPython\n" // here it works as expected // "IPython.embed()\n"); } if(Pstream::parRun()) { Pbug << "This is a parallel run" << endl; parallelMasterOnly_=readBool(dict.lookup("parallelMasterOnly")); } if(parallelNoRun(true)) { Pbug << "Getting out because of 'parallelNoRun'" << endl; return; } interpreterCount++; Pbug << "Getting new interpreter" << endl; pythonState_=Py_NewInterpreter(); Pbug << "Interpreter state: " << getHex(pythonState_) << endl; // interactiveLoop("Clean"); initIPython(); Pbug << "Currently " << interpreterCount << " Python interpreters (created one)" << endl; if( interactiveAfterExecute_ || interactiveAfterException_ ) { } else { if(useIPython_) { WarningIn("pythonInterpreterWrapper::pythonInterpreterWrapper") << "'useIPython' not needed in " << dict.name() << " if there is no interactivity" << endl; } } if(useNumpy_) { Dbug << "Attempting to import numpy" << endl; static bool warnedNumpy=false; if(!warnedNumpy) { if(getEnv("FOAM_SIGFPE")!="false") { WarningInFunction << "Attempting to import numpy. On some platforms that will raise a " << "(harmless) floating point exception. To avoid switch off " << "by setting the environment variable 'FOAM_SIGFPE' to 'false'" << endl; } warnedNumpy=true; } int fail=!importLib("numpy"); if(fail) { FatalErrorIn("pythonInterpreterWrapper::pythonInterpreterWrapper") << "Problem during import of numpy." << nl << "Switch if off with 'useNumpy false;' if it is not needed" << endl << exit(FatalError); } fail=PyRun_SimpleString( // "def _swak_wrapOpenFOAMField_intoNumpy(address,typestr,size,nr=None):\n" // " class iWrap(object):\n" // " def __init__(self):\n" // " self.__array_interface__={}\n" // " self.__array_interface__['data']=(int(address,16),False)\n" // " if nr:\n" // " self.__array_interface__['shape']=(size,nr)\n" // " else:\n" // " self.__array_interface__['shape']=(size,)\n" // " self.__array_interface__['version']=3\n" // " self.__array_interface__['typestr']=typestr\n" // " return numpy.asarray(iWrap())\n" "class OpenFOAMFieldArray(numpy.ndarray):\n" " def __new__(cls,address,typestr,size,nr=None,names=None):\n" " obj=type('Temporary',(object,),{})\n" " obj.__array_interface__={}\n" " obj.__array_interface__['data']=(address,False)\n" " if nr:\n" " obj.__array_interface__['shape']=(size,nr)\n" " else:\n" " obj.__array_interface__['shape']=(size,)\n" // " obj.__array_interface__['descr']=[('x',typestr)]\n" " obj.__array_interface__['version']=3\n" " obj.__array_interface__['typestr']=typestr\n" " obj=numpy.asarray(obj).view(cls)\n" " if names:\n" " for i,n in enumerate(names):\n" " def f(ind):\n" " return obj[:,ind]\n" " setattr(obj,n,f(i))\n" " return obj\n" " def __array_finalize__(self,obj):\n" " if obj is None: return\n" ); } if(dict.found("importLibs")) { const dictionary &libList=dict.subDict("importLibs"); forAllConstIter(dictionary,libList,iter) { word as=(*iter).keyword(); word full((*iter).stream()); if(full=="") { full=as; } importLib(full,as,true); } } else {
int MPyEmbed_RunThread(char *file) { int i; FILE *fp; if (!PythonAvailable) { return 0; } fp = fopen(file, "r"); if (!fp) { perror("fopen"); return 0; } PyThread_acquire_lock(threaddatalock, WAIT_LOCK); for (i = 0; i < THREADS; i++) { if (!VALID(threaddata[i])) { if (threaddata[i].exitlock != 0) PyThread_free_lock(threaddata[i].exitlock); threaddata[i].thread_id = PyThread_get_thread_ident(); PyEval_AcquireLock(); threaddata[i].threadstate = Py_NewInterpreter(); initpydega(); PySys_SetArgv(argc, argv); PyEval_ReleaseThread(threaddata[i].threadstate); threaddata[i].mainstate = PyThreadState_New(threaddata[i].threadstate->interp); threaddata[i].mainstate->thread_id = mainstate->thread_id; threaddata[i].exitlock = PyThread_allocate_lock(); PyThread_acquire_lock(threaddata[i].exitlock, WAIT_LOCK); break; } } PyThread_release_lock(threaddatalock); if (i == THREADS) { fclose(fp); return 0; } PyEval_AcquireThread(threaddata[i].threadstate); PyRun_SimpleFile(fp, file); PyEval_ReleaseThread(threaddata[i].threadstate); fclose(fp); PyThread_acquire_lock(threaddatalock, WAIT_LOCK); PyEval_AcquireThread(threaddata[i].threadstate); PyThread_release_lock(threaddatalock); Py_XDECREF(threaddata[i].postframe); PyThread_acquire_lock(threaddatalock, WAIT_LOCK); PyThreadState_Clear(threaddata[i].mainstate); PyThreadState_Delete(threaddata[i].mainstate); Py_EndInterpreter(threaddata[i].threadstate); PyEval_ReleaseLock(); threaddata[i].mainstate = threaddata[i].threadstate = 0; threaddata[i].postframe = 0; PyThread_release_lock(threaddatalock); PyThread_release_lock(threaddata[i].exitlock); return 1; }
private int intercloudGW_create(struct occi_category * optr, void * vptr, struct rest_request * rptr) { struct occi_kind_node * nptr; struct intercloudGW * pptr; char sendstr[1024]; char strtmp[1024]; char srcdir[1024]; char * response = NULL; char * token; listcc categoryAtr; PyObject *pName=NULL, *pModule=NULL, *pDict=NULL, *pFunc=NULL,*result=NULL; PyThreadState* pythr=NULL; if (!( nptr = vptr )) return(0); else if (!( pptr = nptr->contents )) return(0); if(!(pptr->name)) strcpy(sendstr," "); else if(pptr->name[0]=='\0') strcpy(sendstr," "); else strcpy(sendstr,pptr->name); if(!(pptr->node)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->node[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->node,','); if(!(pptr->account)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->account[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->account,','); if(!(pptr->price)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->price[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->price,','); if(!(pptr->state)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->state[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->state,','); // python interface sprintf(srcdir,"%s/pyaccords/pysrc",PYPATH); pythr = Py_NewInterpreter(); python_path(srcdir); pName = PyString_FromString("intercloudGW"); if(pName == NULL) printf("erro: in intercloudGW no such file name\n"); else pModule = PyImport_Import(pName); if(pModule == NULL) printf("error: failed to load intercloudGW module\n"); else pDict = PyModule_GetDict(pModule); if(pDict == NULL) printf("error: failed to load dict name in intercloudGW module\n"); else pFunc = PyDict_GetItemString(pDict,"create"); if(pFunc == NULL) printf("error: failed to load create function in intercloudGW module\n"); else result=PyObject_CallFunction(pFunc,"s",sendstr); if(result) response=allocate_string(PyString_AsString( result )); Py_DECREF(pModule); Py_DECREF(pName); Py_EndInterpreter(pythr); resetListe(&categoryAtr); token= strtok(response,","); for(; token != NULL ;) { addBacke(&categoryAtr,token); token=strtok(NULL, ","); } elemm *pelem = categoryAtr.first; if(pelem){ pptr->name = pelem->value; pelem = pelem->next; } if(pelem){ pptr->node = pelem->value; pelem = pelem->next; } if(pelem){ pptr->account = pelem->value; pelem = pelem->next; } if(pelem){ pptr->price = pelem->value; pelem = pelem->next; } if(pelem){ pptr->state = pelem->value; } return 1; }
int launch(char const * archivePath, char const * archiveName) { PyObject *obHandle; int loadedNew = 0; char pathnm[_MAX_PATH]; VS("START"); strcpy(pathnm, archivePath); strcat(pathnm, archiveName); /* Set up paths */ if (setPaths(archivePath, archiveName)) return -1; VS("Got Paths"); /* Open the archive */ if (openArchive()) return -1; VS("Opened Archive"); /* Load Python DLL */ if (attachPython(&loadedNew)) return -1; if (loadedNew) { /* Start Python with silly command line */ PyEval_InitThreads(); if (startPython(1, (char**)&pathnm)) return -1; VS("Started new Python"); thisthread = PyThreadState_Swap(NULL); PyThreadState_Swap(thisthread); } else { VS("Attached to existing Python"); /* start a mew interp */ thisthread = PyThreadState_Swap(NULL); PyThreadState_Swap(thisthread); if (thisthread == NULL) { thisthread = Py_NewInterpreter(); VS("created thisthread"); } else VS("grabbed thisthread"); PyRun_SimpleString("import sys;sys.argv=[]"); } /* a signal to scripts */ PyRun_SimpleString("import sys;sys.frozen='dll'\n"); VS("set sys.frozen"); /* Create a 'frozendllhandle' as a counterpart to sys.dllhandle (which is the Pythonxx.dll handle) */ obHandle = Py_BuildValue("i", gInstance); PySys_SetObject("frozendllhandle", obHandle); Py_XDECREF(obHandle); /* Import modules from archive - this is to bootstrap */ if (importModules()) return -1; VS("Imported Modules"); /* Install zlibs - now import hooks are in place */ if (installZlibs()) return -1; VS("Installed Zlibs"); /* Run scripts */ if (runScripts()) return -1; VS("All scripts run"); if (PyErr_Occurred()) { // PyErr_Print(); //PyErr_Clear(); VS("Some error occurred"); } VS("PGL released"); // Abandon our thread state. PyEval_ReleaseThread(thisthread); VS("OK."); return 0; }
int weechat_python_load (const char *filename) { char *argv[] = { "__weechat_plugin__" , NULL }; #if PY_MAJOR_VERSION >= 3 wchar_t *wargv[] = { NULL, NULL }; #endif FILE *fp; PyObject *weechat_outputs, *python_path, *path; const char *weechat_home; char *str_home; int len; if ((fp = fopen (filename, "r")) == NULL) { weechat_printf (NULL, weechat_gettext ("%s%s: script \"%s\" not found"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); return 0; } if ((weechat_python_plugin->debug >= 2) || !python_quiet) { weechat_printf (NULL, weechat_gettext ("%s: loading script \"%s\""), PYTHON_PLUGIN_NAME, filename); } python_current_script = NULL; python_registered_script = NULL; /* PyEval_AcquireLock (); */ python_current_interpreter = Py_NewInterpreter (); #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ len = mbstowcs (NULL, argv[0], 0) + 1; wargv[0] = malloc ((len + 1) * sizeof (wargv[0][0])); if (wargv[0]) { if (mbstowcs (wargv[0], argv[0], len) == (size_t)(-1)) { free (wargv[0]); wargv[0] = NULL; } PySys_SetArgv(1, wargv); if (wargv[0]) free (wargv[0]); } #else /* python <= 2.x */ PySys_SetArgv(1, argv); #endif if (!python_current_interpreter) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to create new " "sub-interpreter"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); fclose (fp); /* PyEval_ReleaseLock (); */ return 0; } PyThreadState_Swap (python_current_interpreter); /* adding $weechat_dir/python in $PYTHONPATH */ python_path = PySys_GetObject ("path"); weechat_home = weechat_info_get ("weechat_dir", ""); if (weechat_home) { len = strlen (weechat_home) + 1 + strlen (PYTHON_PLUGIN_NAME) + 1; str_home = malloc (len); if (str_home) { snprintf (str_home, len, "%s/python", weechat_home); #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ path = PyUnicode_FromString(str_home); #else /* python <= 2.x */ path = PyBytes_FromString (str_home); #endif if (path != NULL) { PyList_Insert (python_path, 0, path); Py_DECREF (path); } free (str_home); } } #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ weechat_outputs = PyModule_Create (&moduleDefOutputs); #else /* python <= 2.x */ weechat_outputs = Py_InitModule("weechatOutputs", weechat_python_output_funcs); #endif if (!weechat_outputs) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stdout and " "stderr"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } else { if (PySys_SetObject("stdout", weechat_outputs) == -1) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stdout"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } if (PySys_SetObject("stderr", weechat_outputs) == -1) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stderr"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } } python_current_script_filename = filename; if (PyRun_SimpleFile (fp, filename) != 0) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to parse file \"%s\""), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); fclose (fp); if (PyErr_Occurred ()) PyErr_Print (); /* if script was registered, remove it from list */ if (python_current_script != NULL) { plugin_script_remove (weechat_python_plugin, &python_scripts, &last_python_script, python_current_script); } Py_EndInterpreter (python_current_interpreter); /* PyEval_ReleaseLock (); */ return 0; } if (PyErr_Occurred ()) PyErr_Print (); fclose (fp); if (!python_registered_script) { weechat_printf (NULL, weechat_gettext ("%s%s: function \"register\" not " "found (or failed) in file \"%s\""), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); if (PyErr_Occurred ()) PyErr_Print (); Py_EndInterpreter (python_current_interpreter); /* PyEval_ReleaseLock (); */ return 0; } python_current_script = python_registered_script; /* PyEval_ReleaseThread (python_current_script->interpreter); */ /* * set input/close callbacks for buffers created by this script * (to restore callbacks after upgrade) */ plugin_script_set_buffer_callbacks (weechat_python_plugin, python_scripts, python_current_script, &weechat_python_api_buffer_input_data_cb, &weechat_python_api_buffer_close_cb); (void) weechat_hook_signal_send ("python_script_loaded", WEECHAT_HOOK_SIGNAL_STRING, python_current_script->filename); return 1; }
void ScripterCore::slotRunScriptFile(QString fileName, QStringList arguments, bool inMainInterpreter) /** run "filename" python script with the additional arguments provided in "arguments" */ { // Prevent two scripts to be run concurrently or face crash! if (ScCore->primaryMainWindow()->scriptIsRunning()) return; disableMainWindowMenu(); PyThreadState *state = NULL; QFileInfo fi(fileName); QByteArray na = fi.fileName().toLocal8Bit(); // Set up a sub-interpreter if needed: PyThreadState* global_state = NULL; if (!inMainInterpreter) { ScCore->primaryMainWindow()->propertiesPalette->unsetDoc(); ScCore->primaryMainWindow()->pagePalette->setView(NULL); ScCore->primaryMainWindow()->setScriptRunning(true); qApp->setOverrideCursor(QCursor(Qt::WaitCursor)); // Create the sub-interpreter // FIXME: This calls abort() in a Python debug build. We're doing something wrong. //stateo = PyEval_SaveThread(); global_state = PyThreadState_Get(); state = Py_NewInterpreter(); // Init the scripter module in the sub-interpreter initscribus(ScCore->primaryMainWindow()); } // Make sure sys.argv[0] is the path to the script arguments.prepend(na.data()); //convert arguments (QListString) to char** for Python bridge /* typically arguments == ['path/to/script.py','--argument1','valueforarg1','--flag']*/ char **comm = new char*[arguments.size()]; for (int i = 0; i < arguments.size(); i++) { QByteArray localStr = arguments.at(i).toLocal8Bit(); comm[i] = new char[localStr.size() + 1]; //+1 to allow adding '\0'. may be useless, don't know how to check. comm[i][localStr.size()] = 0; strncpy(comm[i], localStr.data(), localStr.size()); } PySys_SetArgv(arguments.size(), comm); for (int i = 0; i < arguments.size(); i++) delete[] comm[i]; delete[] comm; // call python script PyObject* m = PyImport_AddModule((char*)"__main__"); if (m == NULL) qDebug("Failed to get __main__ - aborting script"); else { // Path separators need to be escaped on Windows QString escapedAbsPath = QDir::toNativeSeparators(fi.absolutePath()).replace("\\", "\\\\"); QString escapedFileName = QDir::toNativeSeparators(fileName).replace("\\", "\\\\"); // FIXME: If filename contains chars outside 7bit ascii, might be problems PyObject* globals = PyModule_GetDict(m); // Build the Python code to run the script //QString cm = QString("from __future__ import division\n"); removed due #5252 PV QString cm = QString("import sys\n"); cm += QString("import cStringIO\n"); /* Implementation of the help() in pydoc.py reads some OS variables * for output settings. I use ugly hack to stop freezing calling help() * in script. pv. */ cm += QString("import os\nos.environ['PAGER'] = '/bin/false'\n"); // HACK cm += QString("sys.path[0] = \"%1\"\n").arg(escapedAbsPath); // Replace sys.stdin with a dummy StringIO that always returns // "" for read cm += QString("sys.stdin = cStringIO.StringIO()\n"); // tell the script if it's running in the main intepreter or a subinterpreter cm += QString("import scribus\n"); if (inMainInterpreter) cm+= QString("scribus.mainInterpreter = True\n"); else cm+= QString("scribus.mainInterpreter = False\n"); cm += QString("try:\n"); cm += QString(" execfile(\"%1\")\n").arg(escapedFileName); cm += QString("except SystemExit:\n"); cm += QString(" pass\n"); // Capture the text of any other exception that's raised by the interpreter // into a StringIO buffer for later extraction. cm += QString("except:\n"); cm += QString(" import traceback\n"); cm += QString(" _errorMsg = traceback.format_exc()\n"); if (!ScCore->usingGUI()) cm += QString(" traceback.print_exc()\n"); // We re-raise the exception so the return value of PyRun_StringFlags reflects // the fact that an exception has ocurred. cm += QString(" raise\n"); // FIXME: if cmd contains chars outside 7bit ascii, might be problems QByteArray cmd = cm.toUtf8(); // Now run the script in the interpreter's global scope. It'll run in a // sub-interpreter if we created and switched to one earlier, otherwise // it'll run in the main interpreter. PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals); // NULL is returned if an exception is set. We don't care about any // other return value (most likely None anyway) and can ignore it. if (result == NULL) { PyObject* errorMsgPyStr = PyMapping_GetItemString(globals, (char*)"_errorMsg"); if (errorMsgPyStr == NULL) { // It's rather unlikely that this will ever be reached - to get here // we'd have to fail to retrive the string we just created. qDebug("Error retrieving error message content after script exception!"); qDebug("Exception was:"); PyErr_Print(); } else if (ScCore->usingGUI()) { QString errorMsg = PyString_AsString(errorMsgPyStr); // Display a dialog to the user with the exception QClipboard *cp = QApplication::clipboard(); cp->setText(errorMsg); ScCore->closeSplash(); qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); ScMessageBox::warning(ScCore->primaryMainWindow(), tr("Script error"), "<qt><p>" + tr("If you are running an official script report it at <a href=\"http://bugs.scribus.net\">bugs.scribus.net</a> please.") + "</p><pre>" + errorMsg.toHtmlEscaped() + "</pre><p>" + tr("This message is in your clipboard too. Use Ctrl+V to paste it into bug tracker.") + "</p></qt>"); } // We've already processed the exception text, so clear the exception PyErr_Clear(); } // end if result == NULL // Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF Py_XDECREF(result); } // end if m == NULL if (!inMainInterpreter) { Py_EndInterpreter(state); PyThreadState_Swap(global_state); //PyEval_RestoreThread(stateo); qApp->restoreOverrideCursor(); ScCore->primaryMainWindow()->setScriptRunning(false); } enableMainWindowMenu(); }
bool PythonScript::execute(TWScriptAPI *tw) const { PyObject * tmp; // Load the script QFile scriptFile(m_Filename); if (!scriptFile.open(QIODevice::ReadOnly)) { // handle error return false; } QString contents = m_Codec->toUnicode(scriptFile.readAll()); scriptFile.close(); // Python seems to require Unix style line endings if (contents.contains("\r")) contents.replace(QRegExp("\r\n?"), "\n"); // Create a separate sub-interpreter for this script PyThreadState* interpreter = Py_NewInterpreter(); // Register the types if (!registerPythonTypes(tw->GetResult())) { Py_EndInterpreter(interpreter); return false; } pyQObject *TW; TW = (pyQObject*)QObjectToPython(tw); if (!TW) { tw->SetResult(tr("Could not create TW")); Py_EndInterpreter(interpreter); return false; } // Run the script PyObject * globals, * locals; globals = PyDict_New(); locals = PyDict_New(); // Create a dictionary of global variables // without the __builtins__ module, nothing would work! PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()); PyDict_SetItemString(globals, "TW", (PyObject*)TW); PyObject * ret = NULL; if (globals && locals) ret = PyRun_String(qPrintable(contents), Py_file_input, globals, locals); Py_XDECREF(globals); Py_XDECREF(locals); Py_XDECREF(ret); Py_XDECREF(TW); // Check for exceptions if (PyErr_Occurred()) { PyObject * errType, * errValue, * errTraceback; PyErr_Fetch(&errType, &errValue, &errTraceback); tmp = PyObject_Str(errValue); QString errString; if (!asQString(tmp, errString)) { Py_XDECREF(tmp); tw->SetResult(tr("Unknown error")); return false; } Py_XDECREF(tmp); tw->SetResult(errString); /////////////////////DEBUG // This prints the python error in the usual python way to stdout // Simply comment this block to prevent this behavior Py_XINCREF(errType); Py_XINCREF(errValue); Py_XINCREF(errTraceback); PyErr_Restore(errType, errValue, errTraceback); PyErr_Print(); /////////////////////DEBUG Py_XDECREF(errType); Py_XDECREF(errValue); Py_XDECREF(errTraceback); Py_EndInterpreter(interpreter); return false; } // Finish Py_EndInterpreter(interpreter); return true; }
/*-------------------------------------------------------------------------------*/ struct rest_response * start_intercloudGW( struct occi_category * optr, struct rest_client * cptr, struct rest_request * rptr, struct rest_response * aptr, void * vptr ) { struct intercloudGW * pptr; char sendstr[1024]=" "; char strtmp[1024]=" "; int status; char message[1024]; char srcdir[1024]; char * response = NULL; char * token; FILE * exp_file; listcc restResponse; PyObject *pName=NULL, *pModule=NULL, *pDict=NULL, *pFunc=NULL,*result=NULL; PyThreadState* pythr=NULL; if (!( pptr = vptr )) return( rest_html_response( aptr, 404, "Invalid Action" ) ); else{ if(!(pptr->name))strcpy(sendstr," "); else if(pptr->name[0]=='\0') strcpy(sendstr," "); else strcpy(sendstr,pptr->name); if(!(pptr->node)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->node[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->node,','); if(!(pptr->account)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->account[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->account,','); if(!(pptr->price)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->price[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->price,','); if(!(pptr->state)){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else if(pptr->state[0]=='\0'){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->state,','); // python interface sprintf(srcdir,"%s/pyaccords/pysrc",PYPATH); pythr = Py_NewInterpreter(); python_path(srcdir); pName = PyString_FromString("intercloudGWAct"); if(pName == NULL) printf("erro: in intercloudGWAct no such file name\n"); else pModule = PyImport_Import(pName); if(pModule == NULL) printf("error: failed to load intercloudGWAct module\n"); else pDict = PyModule_GetDict(pModule); if(pDict == NULL) printf("error: failed to load dict name in intercloudGW module\n"); else pFunc = PyDict_GetItemString(pDict,"start"); if(pFunc == NULL) printf("error: failed to load start function in intercloudGW module\n"); else result=PyObject_CallFunction(pFunc,"s",sendstr); if(result) response=allocate_string(PyString_AsString( result )); Py_DECREF(pModule); Py_DECREF(pName); Py_EndInterpreter(pythr); resetListe(&restResponse); token= strtok(response,","); for(; token != NULL ;) { addBacke(&restResponse,token); token=strtok(NULL, ","); } elemm *pelem = restResponse.first; if(pelem){ status = atoi(pelem->value); pelem = pelem->next; } if(pelem){ strcpy(message, pelem->value); pelem = pelem->next; } return( rest_html_response( aptr, status, message ) ); } }
static void run_interpreter(FILE *input, FILE *output) { PyThreadState *tstate; PyObject *new_stdin, *new_stdout; PyObject *mainmod, *globals; char buffer[1000]; char *p, *q; int n, end; PyEval_AcquireLock(); tstate = Py_NewInterpreter(); if (tstate == NULL) { fprintf(output, "Sorry -- can't create an interpreter\n"); return; } mainmod = PyImport_AddModule("__main__"); globals = PyModule_GetDict(mainmod); Py_INCREF(globals); new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL); new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL); PySys_SetObject("stdin", new_stdin); PySys_SetObject("stdout", new_stdout); PySys_SetObject("stderr", new_stdout); for (n = 1; !PyErr_Occurred(); n++) { Py_BEGIN_ALLOW_THREADS fprintf(output, "%d> ", n); p = fgets(buffer, sizeof buffer, input); Py_END_ALLOW_THREADS if (p == NULL) break; if (p[0] == '\377' && p[1] == '\354') break; q = strrchr(p, '\r'); if (q && q[1] == '\n' && q[2] == '\0') { *q++ = '\n'; *q++ = '\0'; } while (*p && isspace(*p)) p++; if (p[0] == '#' || p[0] == '\0') continue; end = run_command(buffer, globals); if (end < 0) PyErr_Print(); if (end) break; } Py_XDECREF(globals); Py_XDECREF(new_stdin); Py_XDECREF(new_stdout); Py_EndInterpreter(tstate); PyEval_ReleaseLock(); fprintf(output, "Goodbye!\n"); }
void IvrPython::run(){ FILE* fp; int retval; #ifndef IVR_PERL pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0); #endif //IVR_PERL // static PyThreadState* pyMainThreadState; fp = fopen((char*)fileName,"r"); if(fp != NULL){ #ifndef IVR_PERL PyMethodDef extIvrPython[] = { // media functions {"enqueueMediaFile", ivrEnqueueMediaFile, METH_VARARGS, "ivr: enqueue media file. " "filename: string, front : int = 1(default true) "}, {"emptyMediaQueue", ivrEmptyMediaQueue, METH_VARARGS, "ivr: empty the media queue."}, {"startRecording", ivrStartRecording, METH_VARARGS, "ivr: start recording to file. filename : string."}, {"stopRecording", ivrStopRecording, METH_VARARGS, "ivr: stop recording to file."}, #ifdef IVR_WITH_TTS {"say", ivrSay, METH_VARARGS, "ivr tts and enqueue. msg: string, front: boolean "}, #endif //IVR_WITH_TTS // DTMF functions {"enableDTMFDetection", ivrEnableDTMFDetection, METH_VARARGS, "enable DTMF detection. " "setCallback(onDTMF_FUNC, \"onDTMF\") first!"}, {"disableDTMFDetection", ivrDisableDTMFDetection, METH_VARARGS, "disable DTMF detection permanently"}, {"pauseDTMFDetection", ivrPauseDTMFDetection, METH_VARARGS, "pause DTMF detection temporarily, can be resumed"}, {"resumeDTMFDetection", ivrResumeDTMFDetection, METH_VARARGS, "resume DTMF detection"}, // informational {"getTime", ivrGetTime, METH_VARARGS, "Example Module"}, {"getFrom", ivrGetFrom, METH_VARARGS, "Example Module"}, {"getTo", ivrGetTo, METH_VARARGS, "Example Module"}, {"getFromURI", ivrGetFromURI, METH_VARARGS, "Example Module"}, {"getToURI", ivrGetToURI, METH_VARARGS, "Example Module"}, {"getDomain", ivrGetDomain, METH_VARARGS, "Example Module"}, // call transfer functions {"redirect", ivrRedirect, METH_VARARGS, "Example Module"}, {"dialout", ivrDialout, METH_VARARGS, "Example Module"}, // setting callbacks {"setCallback", setCallback, METH_VARARGS, "Example Module"}, {"sleep", ivrSleep, METH_VARARGS, "Sleep n seconds, or until wakeUp"}, {"usleep", ivrUSleep, METH_VARARGS, "Sleep n microseconds, or until wakeUp"}, {"msleep", ivrmSleep, METH_VARARGS, "Sleep n milliseconds, or until wakeUp"}, {"wakeUp", ivrWakeUp, METH_VARARGS, "wake Up from sleep"}, // legacy from old ivr: sequential functions {"play", ivrPlay, METH_VARARGS, "play and wait for the end of the file (queue empty)"}, {"record", ivrRecord, METH_VARARGS, "record maximum of time secs. Parameter: filename : string, timeout = 0 : int"}, {"playAndDetect", ivrPlayAndDetect, METH_VARARGS, "play and wait for the end of the file (queue empty) or keypress"}, {"detect", ivrDetect, METH_VARARGS, "detect until timeout Parameter: timeout = 0 : int"}, // for jitter/clock skew generation test only // DONT CALL THIS FUNCTION {"mediaThreadUSleep", ivrMediaThreadUSleep, METH_VARARGS, "let mthr sleep, dont call this function"}, {NULL, NULL, 0, NULL}, }; if(!Py_IsInitialized()){ DBG("Start" SCRIPT_TYPE "\n"); Py_Initialize(); PyEval_InitThreads(); pyMainThreadState = PyEval_SaveThread(); } DBG("Start new" SCRIPT_TYPE "interpreter\n"); PyEval_AcquireLock(); // PyThreadState* pyThreadState; if ( (mainInterpreterThreadState = Py_NewInterpreter()) != NULL){ PyObject* ivrPyInitModule = Py_InitModule(PY_MOD_NAME, extIvrPython); PyObject* ivrPythonPointer = PyCObject_FromVoidPtr((void*)this,NULL); if (ivrPythonPointer != NULL) PyModule_AddObject(ivrPyInitModule, "ivrPythonPointer", ivrPythonPointer); Py_tracefunc tmp_t = pythonTrace; PyEval_SetTrace(tmp_t, PyCObject_FromVoidPtr((void*)this,NULL)); if(!PyRun_SimpleFile(fp,(char*)fileName)){ fclose(fp); retval = 0;// true; } else{ PyErr_Print(); ERROR("IVR" SCRIPT_TYPE "Error: Failed to run \"%s\"\n", (char*)fileName); retval = -1;// false; } Py_EndInterpreter(mainInterpreterThreadState); } else{ ERROR("IVR" SCRIPT_TYPE "Error: Failed to start new interpreter.\n"); } PyEval_ReleaseLock(); #else //IVR_PERL DBG("Start" SCRIPT_TYPE ", about to alloc\n"); my_perl_interp = perl_alloc(); printf("interp is %ld\n", (long) my_perl_interp); printf("filename is %s\n", fileName); DBG("finished alloc Perl, about to construct Perl\n"); perl_construct(my_perl_interp); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; char *embedding[] = { "", (char*)fileName}; DBG("finished construct Perl, about to parse Perl\n"); perl_parse(my_perl_interp, xs_init, 2, embedding, (char **)NULL); DBG("finished parse Perl, about to run Perl\n"); SV *pivr = get_sv("Ivr::__ivrpointer__", TRUE); DBG("Ivr::__ivrpointer__ is %lx.\n", (unsigned long int) pivr); sv_setuv(pivr, (unsigned int) this); perl_run(my_perl_interp); DBG("finished run Perl, about to sleep 5 seconds to let callback event catch up\n"); sleep(5); DBG("after sleep, about to destruct\n"); perl_destruct(my_perl_interp); DBG("finished destruct Perl, about to free\n"); perl_free(my_perl_interp); #endif //IVR_PERL } else{ ERROR("IVR" SCRIPT_TYPE "Error: Can not open file \"%s\"\n",(char*) fileName); retval = -1;// false; } DBG("IVR: run finished. stopping rtp stream...\n"); pAmSession->rtp_str.pause(); }
void XBPyThread::Process() { CLog::Log(LOGDEBUG,"Python thread: start processing"); int m_Py_file_input = Py_file_input; // get the global lock PyEval_AcquireLock(); PyThreadState* state = Py_NewInterpreter(); if (!state) { PyEval_ReleaseLock(); CLog::Log(LOGERROR,"Python thread: FAILED to get thread state!"); return; } // swap in my thread state PyThreadState_Swap(state); XBMCAddon::AddonClass::Ref<XBMCAddon::Python::LanguageHook> languageHook(new XBMCAddon::Python::LanguageHook(state->interp)); languageHook->RegisterMe(); m_pExecuter->InitializeInterpreter(addon); CLog::Log(LOGDEBUG, "%s - The source file to load is %s", __FUNCTION__, m_source); // get path from script file name and add python path's // this is used for python so it will search modules from script path first CStdString scriptDir; URIUtils::GetDirectory(CSpecialProtocol::TranslatePath(m_source), scriptDir); URIUtils::RemoveSlashAtEnd(scriptDir); CStdString path = scriptDir; // add on any addon modules the user has installed ADDON::VECADDONS addons; ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons); for (unsigned int i = 0; i < addons.size(); ++i) #ifdef TARGET_WINDOWS { CStdString strTmp(CSpecialProtocol::TranslatePath(addons[i]->LibPath())); g_charsetConverter.utf8ToSystem(strTmp); path += PY_PATH_SEP + strTmp; } #else path += PY_PATH_SEP + CSpecialProtocol::TranslatePath(addons[i]->LibPath()); #endif // and add on whatever our default path is path += PY_PATH_SEP; // we want to use sys.path so it includes site-packages // if this fails, default to using Py_GetPath PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete if( pathObj && PyList_Check(pathObj) ) { for( int i = 0; i < PyList_Size(pathObj); i++ ) { PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete if( e && PyString_Check(e) ) { path += PyString_AsString(e); // returns internal data, don't delete or modify path += PY_PATH_SEP; } } } else { path += Py_GetPath(); } Py_DECREF(sysMod); // release ref to sysMod // set current directory and python's path. if (m_argv != NULL) PySys_SetArgv(m_argc, m_argv); CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str()); PySys_SetPath((char *)path.c_str()); CLog::Log(LOGDEBUG, "%s - Entering source directory %s", __FUNCTION__, scriptDir.c_str()); PyObject* module = PyImport_AddModule((char*)"__main__"); PyObject* moduleDict = PyModule_GetDict(module); // when we are done initing we store thread state so we can be aborted PyThreadState_Swap(NULL); PyEval_ReleaseLock(); // we need to check if we was asked to abort before we had inited bool stopping = false; { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = state; stopping = m_stopping; } PyEval_AcquireLock(); PyThreadState_Swap(state); if (!stopping) { try { if (m_type == 'F') { // run script from file // We need to have python open the file because on Windows the DLL that python // is linked against may not be the DLL that xbmc is linked against so // passing a FILE* to python from an fopen has the potential to crash. PyObject* file = PyFile_FromString((char *) CSpecialProtocol::TranslatePath(m_source).c_str(), (char*)"r"); FILE *fp = PyFile_AsFile(file); if (fp) { PyObject *f = PyString_FromString(CSpecialProtocol::TranslatePath(m_source).c_str()); PyDict_SetItemString(moduleDict, "__file__", f); if (addon.get() != NULL) { PyObject *pyaddonid = PyString_FromString(addon->ID().c_str()); PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid); CStdString version = ADDON::GetXbmcApiVersionDependency(addon); PyObject *pyxbmcapiversion = PyString_FromString(version.c_str()); PyDict_SetItemString(moduleDict, "__xbmcapiversion__", pyxbmcapiversion); CLog::Log(LOGDEBUG,"Instantiating addon using automatically obtained id of \"%s\" dependent on version %s of the xbmc.python api",addon->ID().c_str(),version.c_str()); } Py_DECREF(f); XBMCAddon::Python::PyContext pycontext; // this is a guard class that marks this callstack as being in a python context PyRun_FileExFlags(fp, CSpecialProtocol::TranslatePath(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL); } else CLog::Log(LOGERROR, "%s not found!", m_source); } else { //run script PyRun_String(m_source, m_Py_file_input, moduleDict, moduleDict); } } catch (const XbmcCommons::Exception& e) { e.LogThrowMessage(); } catch (...) { CLog::Log(LOGERROR, "failure in %s", m_source); } } if (!PyErr_Occurred()) CLog::Log(LOGINFO, "Scriptresult: Success"); else if (PyErr_ExceptionMatches(PyExc_SystemExit)) CLog::Log(LOGINFO, "Scriptresult: Aborted"); else { PythonBindings::PythonToCppException e; e.LogThrowMessage(); { CPyThreadState releaseGil; CSingleLock gc(g_graphicsContext); CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST); if (pDlgToast) { CStdString desc; CStdString path; CStdString script; URIUtils::Split(m_source, path, script); if (script.Equals("default.py")) { CStdString path2; URIUtils::RemoveSlashAtEnd(path); URIUtils::Split(path, path2, script); } desc.Format(g_localizeStrings.Get(2100), script); pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc); } } } PyObject *m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "Scriptresult: failed to set abortRequested"); // make sure all sub threads have finished for(PyThreadState* s = state->interp->tstate_head, *old = NULL; s;) { if(s == state) { s = s->next; continue; } if(old != s) { CLog::Log(LOGINFO, "Scriptresult: Waiting on thread %"PRIu64, (uint64_t)s->thread_id); old = s; } CPyThreadState pyState; Sleep(100); pyState.Restore(); s = state->interp->tstate_head; } // pending calls must be cleared out XBMCAddon::RetardedAsynchCallbackHandler::clearPendingCalls(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); //set stopped event - this allows ::stop to run and kill remaining threads //this event has to be fired without holding m_pExecuter->m_critSection //before //Also the GIL (PyEval_AcquireLock) must not be held //if not obeyed there is still no deadlock because ::stop waits with timeout (smart one!) stoppedEvent.Set(); { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = NULL; } PyEval_AcquireLock(); PyThreadState_Swap(state); m_pExecuter->DeInitializeInterpreter(); // run the gc before finishing if (!m_stopping && languageHook->HasRegisteredAddonClasses() && PyRun_SimpleString(GC_SCRIPT) == -1) CLog::Log(LOGERROR,"Failed to run the gc to clean up after running prior to shutting down the Interpreter %s",m_source); Py_EndInterpreter(state); // This is a total hack. Python doesn't necessarily release // all of the objects associated with the interpreter when // you end the interpreter. As a result there are objects // managed by the windowing system that still receive events // until python decides to clean them up. Python will eventually // clean them up on the creation or ending of a subsequent // interpreter. So we are going to keep creating and ending // interpreters until we have no more python objects hanging // around. if (languageHook->HasRegisteredAddonClasses()) { CLog::Log(LOGDEBUG, "The python script \"%s\" has left several " "classes in memory that we will be attempting to clean up. The classes include: %s", m_source, getListOfAddonClassesAsString(languageHook).c_str()); int countLimit; for (countLimit = 0; languageHook->HasRegisteredAddonClasses() && countLimit < 100; countLimit++) { PyThreadState* tmpstate = Py_NewInterpreter(); PyThreadState* oldstate = PyThreadState_Swap(tmpstate); if (PyRun_SimpleString(GC_SCRIPT) == -1) CLog::Log(LOGERROR,"Failed to run the gc to clean up after running %s",m_source); PyThreadState_Swap(oldstate); Py_EndInterpreter(tmpstate); } // If necessary and successfull, debug log the results. if (countLimit > 0 && !languageHook->HasRegisteredAddonClasses()) CLog::Log(LOGDEBUG,"It took %d Py_NewInterpreter/Py_EndInterpreter calls" " to clean up the classes leftover from running \"%s.\"", countLimit,m_source); // If not successful, produce an error message detailing what's been left behind if (languageHook->HasRegisteredAddonClasses()) CLog::Log(LOGERROR, "The python script \"%s\" has left several " "classes in memory that we couldn't clean up. The classes include: %s", m_source, getListOfAddonClassesAsString(languageHook).c_str()); } // unregister the language hook languageHook->UnregisterMe(); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); }
/* ----------------------------------------------------------------- */ int restart_ec2_instance( struct amazonEc2 * pptr ) { struct ec2_subscription * subptr = (struct ec2_subscription *) 0; int status = 0; char srcdir[1024]; char * response; char * token; char sendstr[1024]=" "; char strtmp[1024]=" "; listcc categoryAtr; PyObject *pName=NULL, *pModule=NULL, *pDict=NULL, *pFunc=NULL,*result=NULL; PyThreadState* pythr=NULL; /* ------------------------ */ /* prepare the subscription */ /* ------------------------ */ if (!( pptr )) return(118); else if (!(subptr = use_ec2_configuration( get_default_agent(),default_tls(),pptr->profile ))) return(118); if(!(strValid(pptr->id))) strcpy(sendstr," "); else strcpy(sendstr,pptr->id); if(!(strValid(pptr->name))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->name,','); if(!(strValid(pptr->flavor))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->flavor,','); if(!(strValid(pptr->image))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->image,','); if(!(strValid(pptr->original))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->original,','); if(!(strValid(pptr->profile))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->profile,','); if(!(strValid(pptr->node))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->node,','); if(!(strValid(pptr->price))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->price,','); if(!(strValid(pptr->account))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->account,','); if(!(strValid(pptr->number))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->number,','); if(!(strValid(pptr->rootpass))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->rootpass,','); if(!(strValid(pptr->reference))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->reference,','); if(!(strValid(pptr->network))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->network,','); if(!(strValid(pptr->access))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->access,','); if(!(strValid(pptr->accessip))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->accessip,','); if(!(strValid(pptr->keypair))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->keypair,','); if(!(strValid(pptr->placementgroup))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->placementgroup,','); if(!(strValid(pptr->publicaddr))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->publicaddr,','); if(!(strValid(pptr->privateaddr))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->privateaddr,','); if(!(strValid(pptr->firewall))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->firewall,','); if(!(strValid(pptr->group))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->group,','); if(!(strValid(pptr->zone))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->zone,','); if(!(strValid(pptr->hostname))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->hostname,','); if(!(strValid(pptr->workload))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->workload,','); if(!(strValid(pptr->agent))){ strcpy(strtmp," "); strConcat(sendstr,strtmp,','); } else strConcat(sendstr,pptr->agent,','); sprintf(strtmp,"%d",pptr->when); if(strValid(strcmp)) strConcat(sendstr,strtmp,','); else strConcat(sendstr," ",','); sprintf(strtmp,"%d",pptr->state); if(strValid(strtmp)) strConcat(sendstr,strtmp,','); else strConcat(sendstr," ",','); // python interface sprintf(srcdir,"%s/pyaccords/pysrc",PYPATH); pythr = Py_NewInterpreter(); python_path(srcdir); pName = PyString_FromString("amazonEc2Act"); if(pName == NULL) printf("erro: in amazonEc2Act.py no such file name\n"); else pModule = PyImport_Import(pName); if(pModule == NULL) printf("error: failed to load amazonEc2Act module\n"); else pDict = PyModule_GetDict(pModule); if(pDict == NULL) printf("error: failed to load dict name in amazonEc2Act module\n"); else pFunc = PyDict_GetItemString(pDict,"restart"); if(pFunc == NULL) printf("error: failed to load start function in amazonEc2Act module\n"); else result=PyObject_CallFunction(pFunc,"ssss",subptr->accesskey,subptr->secretkey,subptr->zone,sendstr); if (!result || PyErr_Occurred()) { PyErr_Print(); return (0); } response=allocate_string(PyString_AsString( result )); Py_DECREF(pModule); Py_DECREF(pName); Py_EndInterpreter(pythr); resetListe(&categoryAtr); token= strtok(response,","); for(; token != NULL ;) { addBacke(&categoryAtr,token); token=strtok(NULL, ","); } elemm *pelem = categoryAtr.first; if(pelem){ pptr->id = pelem->value; pelem = pelem->next; } if(pelem){ pptr->name = pelem->value; pelem = pelem->next; } if(pelem){ pptr->flavor = pelem->value; pelem = pelem->next; } if(pelem){ pptr->image = pelem->value; pelem = pelem->next; } if(pelem){ pptr->original = pelem->value; pelem = pelem->next; } if(pelem){ pptr->profile = pelem->value; pelem = pelem->next; } if(pelem){ pptr->node = pelem->value; pelem = pelem->next; } if(pelem){ pptr->price = pelem->value; pelem = pelem->next; } if(pelem){ pptr->account = pelem->value; pelem = pelem->next; } if(pelem){ pptr->number = pelem->value; pelem = pelem->next; } if(pelem){ pptr->rootpass = pelem->value; pelem = pelem->next; } if(pelem){ pptr->reference = pelem->value; pelem = pelem->next; } if(pelem){ pptr->network = pelem->value; pelem = pelem->next; } if(pelem){ pptr->access = pelem->value; pelem = pelem->next; } if(pelem){ pptr->accessip = pelem->value; pelem = pelem->next; } if(pelem){ pptr->keypair = pelem->value; pelem = pelem->next; } if(pelem){ pptr->placementgroup = pelem->value; pelem = pelem->next; } if(pelem){ pptr->publicaddr = pelem->value; pelem = pelem->next; } if(pelem){ pptr->privateaddr = pelem->value; pelem = pelem->next; } if(pelem){ pptr->firewall = pelem->value; pelem = pelem->next; } if(pelem){ pptr->group = pelem->value; pelem = pelem->next; } if(pelem){ pptr->zone = pelem->value; pelem = pelem->next; } if(pelem){ pptr->hostname = pelem->value; pelem = pelem->next; } if(pelem){ pptr->workload = pelem->value; pelem = pelem->next; } if(pelem){ pptr->agent = pelem->value; pelem = pelem->next; } if(pelem){ pptr->when = atoi(pelem->value); pelem = pelem->next; } if(pelem){ pptr->state = atoi(pelem->value); } return status; }
void XBPyThread::Process() { CLog::Log(LOGDEBUG,"Python thread: start processing"); int m_Py_file_input = Py_file_input; // get the global lock PyEval_AcquireLock(); PyThreadState* state = Py_NewInterpreter(); if (!state) { PyEval_ReleaseLock(); CLog::Log(LOGERROR,"Python thread: FAILED to get thread state!"); return; } // swap in my thread state PyThreadState_Swap(state); m_pExecuter->InitializeInterpreter(); CLog::Log(LOGDEBUG, "%s - The source file to load is %s", __FUNCTION__, m_source); // get path from script file name and add python path's // this is used for python so it will search modules from script path first CStdString scriptDir; URIUtils::GetDirectory(_P(m_source), scriptDir); URIUtils::RemoveSlashAtEnd(scriptDir); CStdString path = scriptDir; // add on any addon modules the user has installed ADDON::VECADDONS addons; ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons); for (unsigned int i = 0; i < addons.size(); ++i) path += PY_PATH_SEP + _P(addons[i]->LibPath()); // and add on whatever our default path is path += PY_PATH_SEP; { // we want to use sys.path so it includes site-packages // if this fails, default to using Py_GetPath PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete if( pathObj && PyList_Check(pathObj) ) { for( int i = 0; i < PyList_Size(pathObj); i++ ) { PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete if( e && PyString_Check(e) ) { path += PyString_AsString(e); // returns internal data, don't delete or modify path += PY_PATH_SEP; } } } else { path += Py_GetPath(); } Py_DECREF(sysMod); // release ref to sysMod } // set current directory and python's path. if (m_argv != NULL) PySys_SetArgv(m_argc, m_argv); CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str()); PySys_SetPath((char *)path.c_str()); CLog::Log(LOGDEBUG, "%s - Entering source directory %s", __FUNCTION__, scriptDir.c_str()); PyObject* module = PyImport_AddModule((char*)"__main__"); PyObject* moduleDict = PyModule_GetDict(module); // when we are done initing we store thread state so we can be aborted PyThreadState_Swap(NULL); PyEval_ReleaseLock(); // we need to check if we was asked to abort before we had inited bool stopping = false; { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = state; stopping = m_stopping; } PyEval_AcquireLock(); PyThreadState_Swap(state); if (!stopping) { if (m_type == 'F') { // run script from file // We need to have python open the file because on Windows the DLL that python // is linked against may not be the DLL that xbmc is linked against so // passing a FILE* to python from an fopen has the potential to crash. PyObject* file = PyFile_FromString((char *) _P(m_source).c_str(), (char*)"r"); FILE *fp = PyFile_AsFile(file); if (fp) { PyObject *f = PyString_FromString(_P(m_source).c_str()); PyDict_SetItemString(moduleDict, "__file__", f); Py_DECREF(f); PyRun_File(fp, _P(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict); // Get a reference to the main module // and global dictionary PyObject* main_module = PyImport_AddModule((char*)"__main__"); PyObject* global_dict = PyModule_GetDict(main_module); // Extract a reference to the function "func_name" // from the global dictionary PyObject* expression = PyDict_GetItemString(global_dict, "xbmcclosefilehack"); if (!PyObject_CallFunction(expression,(char*)"(O)",file)) CLog::Log(LOGERROR,"Failed to close the script file %s",_P(m_source).c_str()); } else CLog::Log(LOGERROR, "%s not found!", m_source); } else { //run script PyRun_String(m_source, m_Py_file_input, moduleDict, moduleDict); } } if (!PyErr_Occurred()) CLog::Log(LOGINFO, "Scriptresult: Success"); else if (PyErr_ExceptionMatches(PyExc_SystemExit)) CLog::Log(LOGINFO, "Scriptresult: Aborted"); else { PyObject* exc_type; PyObject* exc_value; PyObject* exc_traceback; PyObject* pystring; pystring = NULL; PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); if (exc_type == 0 && exc_value == 0 && exc_traceback == 0) { CLog::Log(LOGINFO, "Strange: No Python exception occured"); } else { if (exc_type != NULL && (pystring = PyObject_Str(exc_type)) != NULL && (PyString_Check(pystring))) { PyObject *tracebackModule; CLog::Log(LOGINFO, "-->Python script returned the following error<--"); CLog::Log(LOGERROR, "Error Type: %s", PyString_AsString(PyObject_Str(exc_type))); if (PyObject_Str(exc_value)) CLog::Log(LOGERROR, "Error Contents: %s", PyString_AsString(PyObject_Str(exc_value))); tracebackModule = PyImport_ImportModule((char*)"traceback"); if (tracebackModule != NULL) { PyObject *tbList, *emptyString, *strRetval; tbList = PyObject_CallMethod(tracebackModule, (char*)"format_exception", (char*)"OOO", exc_type, exc_value == NULL ? Py_None : exc_value, exc_traceback == NULL ? Py_None : exc_traceback); emptyString = PyString_FromString(""); strRetval = PyObject_CallMethod(emptyString, (char*)"join", (char*)"O", tbList); CLog::Log(LOGERROR, "%s", PyString_AsString(strRetval)); Py_DECREF(tbList); Py_DECREF(emptyString); Py_DECREF(strRetval); Py_DECREF(tracebackModule); } CLog::Log(LOGINFO, "-->End of Python script error report<--"); } else { pystring = NULL; CLog::Log(LOGINFO, "<unknown exception type>"); } CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST); if (pDlgToast) { CStdString desc; CStdString path; CStdString script; URIUtils::Split(m_source, path, script); if (script.Equals("default.py")) { CStdString path2; URIUtils::RemoveSlashAtEnd(path); URIUtils::Split(path, path2, script); } desc.Format(g_localizeStrings.Get(2100), script); pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc); } } Py_XDECREF(exc_type); Py_XDECREF(exc_value); // caller owns all 3 Py_XDECREF(exc_traceback); // already NULL'd out Py_XDECREF(pystring); } PyObject *m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "Scriptresult: failed to set abortRequested"); // make sure all sub threads have finished for(PyThreadState* s = state->interp->tstate_head, *old = NULL; s;) { if(s == state) { s = s->next; continue; } if(old != s) { CLog::Log(LOGINFO, "Scriptresult: Waiting on thread %"PRIu64, (uint64_t)s->thread_id); old = s; } CPyThreadState pyState; Sleep(100); pyState.Restore(); s = state->interp->tstate_head; } // pending calls must be cleared out PyXBMC_ClearPendingCalls(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = NULL; } PyEval_AcquireLock(); PyThreadState_Swap(state); m_pExecuter->DeInitializeInterpreter(); Py_EndInterpreter(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); }
/** * @brief * validate_job_formula - validate that the sorting forumla is in the * correct form. We do this by calling python and having * it catch exceptions. * */ int validate_job_formula(attribute *pattr, void *pobject, int actmode) { char *formula; char *errmsg = NULL; struct resource_def *pres; FILE *fp; char buf[1024]; char pathbuf[MAXPATHLEN]; char *globals1 = NULL; int globals_size1 = 1024; char *globals2 = NULL; int globals_size2 = 1024; char *script = NULL; int script_size = 2048; PyThreadState *ts_main = NULL; PyThreadState *ts_sub = NULL; int rc = 0; int err = 0; if (actmode == ATR_ACTION_FREE) return (0); #ifndef PYTHON return PBSE_INTERNAL; #else if (!Py_IsInitialized()) return PBSE_INTERNAL; formula = pattr->at_val.at_str; if (formula == NULL) return PBSE_INTERNAL; globals1 = malloc(globals_size1); if(globals1 == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } globals2 = malloc(globals_size2); if(globals2 == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } strcpy(globals1, "globals1={"); strcpy(globals2, "globals2={"); /* We need to create a python dictionary to pass to python as a list * of valid symbols. */ for (pres = svr_resc_def; pres; pres = pres->rs_next) { /* unknown resource is used as a delimiter between builtin and custom resources */ if (strcmp(pres->rs_name, RESOURCE_UNKNOWN) != 0) { snprintf(buf, sizeof(buf), "\'%s\':1,", pres->rs_name); if(pbs_strcat(&globals1, &globals_size1, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } if (pres->rs_type == ATR_TYPE_LONG || pres->rs_type == ATR_TYPE_SIZE || pres->rs_type == ATR_TYPE_LL || pres->rs_type == ATR_TYPE_SHORT || pres->rs_type == ATR_TYPE_FLOAT) { if(pbs_strcat(&globals2, &globals_size2, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } } } } snprintf(buf, sizeof(buf), "\'%s\':1, '%s':1, \'%s\':1,\'%s\':1, \'%s\':1, \'%s\':1, \'%s\':1, \'%s\': 1}\n", FORMULA_ELIGIBLE_TIME, FORMULA_QUEUE_PRIO, FORMULA_JOB_PRIO, FORMULA_FSPERC, FORMULA_FSPERC_DEP, FORMULA_TREE_USAGE, FORMULA_FSFACTOR, FORMULA_ACCRUE_TYPE); if (pbs_strcat(&globals1, &globals_size1, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } if (pbs_strcat(&globals2, &globals_size2, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } /* Allocate a buffer for the Python code */ script = malloc(script_size); if (script == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } *script = '\0'; /* import math and initialize variables */ sprintf(buf, "ans = 0\n" "errnum = 0\n" "errmsg = \'\'\n" "try:\n" " from math import *\n" "except ImportError, e:\n" " errnum=4\n" " errmsg=str(e)\n"); if (pbs_strcat(&script, &script_size, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } /* set up our globals dictionary */ if (pbs_strcat(&script, &script_size, globals1) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } if (pbs_strcat(&script, &script_size, globals2) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } /* Now for the real guts: The initial try/except block*/ sprintf(buf, "try:\n" " exec(\'ans="); if (pbs_strcat(&script, &script_size, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } if (pbs_strcat(&script, &script_size, formula) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } sprintf(buf, "\', globals1, locals())\n" "except SyntaxError, e:\n" " errnum=1\n" " errmsg=str(e)\n" "except NameError, e:\n" " errnum=2\n" " errmsg=str(e)\n" "except Exception, e:\n" " pass\n" "if errnum == 0:\n" " try:\n" " exec(\'ans="); if (pbs_strcat(&script, &script_size, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } if (pbs_strcat(&script, &script_size, formula) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } sprintf(buf, "\', globals2, locals())\n" " except NameError, e:\n" " errnum=3\n" " errmsg=str(e)\n" " except Exception, e:\n" " pass\n"); if (pbs_strcat(&script, &script_size, buf) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } /* run the script in a subinterpreter */ ts_main = PyThreadState_Get(); ts_sub = Py_NewInterpreter(); if (!ts_sub) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } err = PyRun_SimpleString(script); /* peek into the interpreter to get the values of err and errmsg */ if (err == 0) { PyObject *module; PyObject *dict; PyObject *val; err = -1; if ((module = PyImport_AddModule("__main__"))) { if ((dict = PyModule_GetDict(module))) { char *p; if ((val = PyDict_GetItemString(dict, "errnum"))) { p = pbs_python_object_str(val); if (*p != '\0') err = atoi(p); } if ((val = PyDict_GetItemString(dict, "errmsg"))) { p = pbs_python_object_str(val); if (*p != '\0') errmsg = strdup(p); } } } } switch(err) { case 0: /* Success */ rc = 0; break; case 1: /* Syntax error in formula */ rc = PBSE_BAD_FORMULA; break; case 2: /* unknown resource name */ rc = PBSE_BAD_FORMULA_KW; break; case 3: /* resource of non-numeric type */ rc = PBSE_BAD_FORMULA_TYPE; break; case 4: /* import error */ rc = PBSE_SYSTEM; break; default: /* unrecognized error */ rc = PBSE_INTERNAL; break; } if (err == 0) { snprintf(pathbuf, sizeof(pathbuf), "%s/%s", pbs_conf.pbs_home_path, FORMULA_ATTR_PATH_SCHED); if ((fp = fopen(pathbuf, "w")) == NULL) { rc = PBSE_SYSTEM; goto validate_job_formula_exit; } fprintf(fp, "### PBS INTERNAL FILE DO NOT MODIFY ###\n"); fprintf(fp, "%s\n", formula); fclose(fp); } else { snprintf(buf, sizeof(buf), "Validation Error: %s", errmsg?errmsg:"Internal error"); log_event(PBSEVENT_DEBUG2, PBS_EVENTCLASS_SERVER, LOG_DEBUG, __func__, buf); } validate_job_formula_exit: if (ts_main) { if (ts_sub) Py_EndInterpreter(ts_sub); PyThreadState_Swap(ts_main); } free(script); free(globals1); free(globals2); free(errmsg); return rc; #endif }
int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThreadState *interpreter, int app_type) { PyObject *app_list = NULL, *applications = NULL; if (uwsgi_apps_cnt >= uwsgi.max_apps) { uwsgi_log("ERROR: you cannot load more than %d apps in a worker\n", uwsgi.max_apps); return -1; } int id = uwsgi_apps_cnt; int multiapp = 0; int i; struct uwsgi_app *wi; time_t now = uwsgi_now(); if (uwsgi_get_app_id(NULL, wsgi_req->appid, wsgi_req->appid_len, -1) != -1) { uwsgi_log( "mountpoint %.*s already configured. skip.\n", wsgi_req->appid_len, wsgi_req->appid); return -1; } wi = &uwsgi_apps[id]; memset(wi, 0, sizeof(struct uwsgi_app)); wi->mountpoint_len = wsgi_req->appid_len < 0xff ? wsgi_req->appid_len : (0xff-1); strncpy(wi->mountpoint, wsgi_req->appid, wi->mountpoint_len); // dynamic chdir ? if (wsgi_req->chdir_len > 0) { strncpy(wi->chdir, wsgi_req->chdir, wsgi_req->chdir_len < 0xff ? wsgi_req->chdir_len : (0xff-1)); #ifdef UWSGI_DEBUG uwsgi_debug("chdir to %s\n", wi->chdir); #endif if (chdir(wi->chdir)) { uwsgi_error("chdir()"); } } // Initialize a new environment for the new interpreter // reload "os" environ to allow dynamic setenv() if (up.reload_os_env) { char **e, *p; PyObject *k, *env_value; PyObject *os_module = PyImport_ImportModule("os"); if (os_module) { PyObject *os_module_dict = PyModule_GetDict(os_module); PyObject *py_environ = PyDict_GetItemString(os_module_dict, "environ"); if (py_environ) { for (e = environ; *e != NULL; e++) { p = strchr(*e, '='); if (p == NULL) continue; k = PyString_FromStringAndSize(*e, (int)(p-*e)); if (k == NULL) { PyErr_Print(); continue; } env_value = PyString_FromString(p+1); if (env_value == NULL) { PyErr_Print(); Py_DECREF(k); continue; } #ifdef UWSGI_DEBUG uwsgi_log("%s = %s\n", PyString_AsString(k), PyString_AsString(env_value)); #endif if (PyObject_SetItem(py_environ, k, env_value)) { PyErr_Print(); } Py_DECREF(k); Py_DECREF(env_value); } } } } if (interpreter == NULL && id) { wi->interpreter = Py_NewInterpreter(); if (!wi->interpreter) { uwsgi_log( "unable to initialize the new python interpreter\n"); exit(1); } PyThreadState_Swap(wi->interpreter); init_pyargv(); // we need to inizialize an embedded module for every interpreter init_uwsgi_embedded_module(); init_uwsgi_vars(); } else if (interpreter) { wi->interpreter = interpreter; } else { wi->interpreter = up.main_thread; } if (wsgi_req->home_len) { set_dyn_pyhome(wsgi_req->home, wsgi_req->home_len); } if (wsgi_req->touch_reload_len > 0 && wsgi_req->touch_reload_len < 0xff) { struct stat trst; strncpy(wi->touch_reload, wsgi_req->touch_reload, wsgi_req->touch_reload_len); if (!stat(wi->touch_reload, &trst)) { wi->touch_reload_mtime = trst.st_mtime; } } wi->callable = up.loaders[loader](arg1); if (!wi->callable) { uwsgi_log("unable to load app %d (mountpoint='%s') (callable not found or import error)\n", id, wi->mountpoint); goto doh; } // the module contains multiple apps if (PyDict_Check((PyObject *)wi->callable)) { applications = wi->callable; uwsgi_log("found a multiapp module...\n"); app_list = PyDict_Keys(applications); multiapp = PyList_Size(app_list); if (multiapp < 1) { uwsgi_log("you have to define at least one app in the apllications dictionary\n"); goto doh; } PyObject *app_mnt = PyList_GetItem(app_list, 0); if (!PyString_Check(app_mnt)) { uwsgi_log("the app mountpoint must be a string\n"); goto doh; } char *tmp_mountpoint = PyString_AsString(app_mnt); wi->mountpoint_len = strlen(wi->mountpoint) < 0xff ? strlen(wi->mountpoint) : (0xff-1); strncpy(wi->mountpoint, tmp_mountpoint, wi->mountpoint_len); wsgi_req->appid = wi->mountpoint; wsgi_req->appid_len = wi->mountpoint_len; #ifdef UWSGI_DEBUG uwsgi_log("main mountpoint = %s\n", wi->mountpoint); #endif wi->callable = PyDict_GetItem(applications, app_mnt); if (PyString_Check((PyObject *) wi->callable)) { PyObject *callables_dict = get_uwsgi_pydict((char *)arg1); if (callables_dict) { wi->callable = PyDict_GetItem(callables_dict, (PyObject *)wi->callable); } } } Py_INCREF((PyObject *)wi->callable); wi->environ = malloc(sizeof(PyObject*)*uwsgi.cores); if (!wi->environ) { uwsgi_error("malloc()"); exit(1); } for(i=0;i<uwsgi.cores;i++) { wi->environ[i] = PyDict_New(); if (!wi->environ[i]) { uwsgi_log("unable to allocate new env dictionary for app\n"); exit(1); } } wi->argc = 1; if (app_type == PYTHON_APP_TYPE_WSGI) { #ifdef UWSGI_DEBUG uwsgi_log("-- WSGI callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_wsgi; wi->response_subhandler = uwsgi_response_subhandler_wsgi; wi->argc = 2; } else if (app_type == PYTHON_APP_TYPE_WEB3) { #ifdef UWSGI_DEBUG uwsgi_log("-- Web3 callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_web3; wi->response_subhandler = uwsgi_response_subhandler_web3; } else if (app_type == PYTHON_APP_TYPE_PUMP) { #ifdef UWSGI_DEBUG uwsgi_log("-- Pump callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_pump; wi->response_subhandler = uwsgi_response_subhandler_pump; } wi->args = malloc(sizeof(PyObject*)*uwsgi.cores); if (!wi->args) { uwsgi_error("malloc()"); exit(1); } for(i=0;i<uwsgi.cores;i++) { wi->args[i] = PyTuple_New(wi->argc); if (!wi->args[i]) { uwsgi_log("unable to allocate new tuple for app args\n"); exit(1); } // add start_response on WSGI app Py_INCREF((PyObject *)up.wsgi_spitout); if (app_type == PYTHON_APP_TYPE_WSGI) { if (PyTuple_SetItem(wi->args[i], 1, up.wsgi_spitout)) { uwsgi_log("unable to set start_response in args tuple\n"); exit(1); } } } if (app_type == PYTHON_APP_TYPE_WSGI) { // prepare sendfile() for WSGI app wi->sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL); wi->eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL); wi->eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL); } // cache most used values wi->error = PyFile_FromFile(stderr, "wsgi_errors", "w", NULL); Py_INCREF((PyObject *)wi->error); wi->gateway_version = PyTuple_New(2); PyTuple_SetItem(wi->gateway_version, 0, PyInt_FromLong(1)); PyTuple_SetItem(wi->gateway_version, 1, PyInt_FromLong(0)); Py_INCREF((PyObject *)wi->gateway_version); wi->uwsgi_version = PyString_FromString(UWSGI_VERSION); Py_INCREF((PyObject *)wi->uwsgi_version); wi->uwsgi_node = PyString_FromString(uwsgi.hostname); Py_INCREF((PyObject *)wi->uwsgi_node); if (uwsgi.threads > 1 && id) { // if we have multiple threads we need to initialize a PyThreadState for each one for(i=0;i<uwsgi.threads;i++) { //uwsgi_log("%p\n", uwsgi.core[i]->ts[id]); uwsgi.workers[uwsgi.mywid].cores[i].ts[id] = PyThreadState_New( ((PyThreadState *)wi->interpreter)->interp); if (!uwsgi.workers[uwsgi.mywid].cores[i].ts[id]) { uwsgi_log("unable to allocate new PyThreadState structure for app %s", wi->mountpoint); goto doh; } } PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key) ); } else if (interpreter == NULL && id) { PyThreadState_Swap(up.main_thread); } const char *default_app = ""; if ((wsgi_req->appid_len == 0 || (wsgi_req->appid_len = 1 && wsgi_req->appid[0] == '/')) && uwsgi.default_app == -1) { default_app = " (default app)" ; uwsgi.default_app = id; } wi->started_at = now; wi->startup_time = uwsgi_now() - now; if (app_type == PYTHON_APP_TYPE_WSGI) { uwsgi_log( "WSGI app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } else if (app_type == PYTHON_APP_TYPE_WEB3) { uwsgi_log( "Web3 app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } else if (app_type == PYTHON_APP_TYPE_PUMP) { uwsgi_log( "Pump app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } uwsgi_apps_cnt++; if (multiapp > 1) { for(i=1;i<multiapp;i++) { PyObject *app_mnt = PyList_GetItem(app_list, i); if (!PyString_Check(app_mnt)) { uwsgi_log("applications dictionary key must be a string, skipping.\n"); continue; } wsgi_req->appid = PyString_AsString(app_mnt); wsgi_req->appid_len = strlen(wsgi_req->appid); PyObject *a_callable = PyDict_GetItem(applications, app_mnt); if (PyString_Check(a_callable)) { PyObject *callables_dict = get_uwsgi_pydict((char *)arg1); if (callables_dict) { a_callable = PyDict_GetItem(callables_dict, a_callable); } } if (!a_callable) { uwsgi_log("skipping broken app %s\n", wsgi_req->appid); continue; } init_uwsgi_app(LOADER_CALLABLE, a_callable, wsgi_req, wi->interpreter, app_type); } } // emulate COW uwsgi_emulate_cow_for_apps(id); return id; doh: if (PyErr_Occurred()) PyErr_Print(); if (interpreter == NULL && id) { Py_EndInterpreter(wi->interpreter); if (uwsgi.threads > 1) { PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key)); } else { PyThreadState_Swap(up.main_thread); } } return -1; }