bool KviPythonInterpreter::init() { // get the global lock PyEval_AcquireLock(); // get a reference to the PyInterpreterState PyInterpreterState * mainInterpreterState = mainThreadState->interp; // create a thread state object for this thread m_pThreadState = PyThreadState_New(mainInterpreterState); // swap in the current thread state PyThreadState_Swap(m_pThreadState); // and hook in the kvirc error handling routines QString szPreCode = QString( \ "import kvirc\n" \ "import sys\n" \ "class kvirc_stderr_grabber:\n" \ "\tdef write(self,s):\n" \ "\t\tkvirc.error(s)\n" \ "sys.stderr=kvirc_stderr_grabber()\n" ); // evaluate that PyRun_SimpleString(szPreCode.toUtf8().data()); // swap out our thread state for now PyThreadState_Swap(NULL); // free the lock PyEval_ReleaseLock(); return true; }
static int python_init(void) { int i; static char name[] = "radiusd"; if (radiusd_module) return 0; Py_SetProgramName(name); Py_Initialize(); PyEval_InitThreads(); /* This also grabs a lock */ if ((radiusd_module = Py_InitModule3("radiusd", radiusd_methods, "FreeRADIUS Module.")) == NULL) goto failed; for (i = 0; radiusd_constants[i].name; i++) if ((PyModule_AddIntConstant(radiusd_module, radiusd_constants[i].name, radiusd_constants[i].value)) < 0) goto failed; PyEval_ReleaseLock(); /* Drop lock grabbed by InitThreads */ radlog(L_DBG, "python_init done"); return 0; failed: python_error(); Py_XDECREF(radiusd_module); radiusd_module = NULL; Py_Finalize(); return -1; }
void XBPython::Finalize() { if (m_bInitialized) { CLog::Log(LOGINFO, "Python, unloading python shared library because no scripts are running anymore"); PyEval_AcquireLock(); PyThreadState_Swap((PyThreadState*)m_mainThreadState); Py_Finalize(); PyEval_ReleaseLock(); #if !(defined(__APPLE__) || defined(_WIN32)) UnloadExtensionLibs(); #endif // first free all dlls loaded by python, after that python24.dll (this is done by UnloadPythonDlls #if !(defined(__APPLE__) || defined(_WIN32)) DllLoaderContainer::UnloadPythonDlls(); #endif #if defined(_LINUX) && !defined(__APPLE__) // we can't release it on windows, as this is done in UnloadPythonDlls() for win32 (see above). // The implementation for linux needs looking at - UnloadPythonDlls() currently only searches for "python24.dll" // The implementation for osx can never unload the python dylib. DllLoaderContainer::ReleaseModule(m_pDll); #endif m_hModule = NULL; m_mainThreadState = NULL; m_bInitialized = false; } }
bool KviPythonInterpreter::execute( const QString &szCode, QStringList &lArgs, //args QString &szRetVal, QString &szError, QStringList &) //lWarnings { if(!m_pThreadState) { szError = __tr2qs_ctx("Internal error: python interpreter not initialized","python"); return false; } int retVal; g_lError.clear(); // grab the global interpreter lock PyEval_AcquireLock(); // swap in my thread state PyThreadState_Swap(m_pThreadState); QString szVarCode = "aArgs = ["; bool bFirst = true; foreach(QString szArg,lArgs) { if(!bFirst) szVarCode += ","; else bFirst = false; szVarCode += QString::fromLatin1("\"%1\"").arg(szArg); } szVarCode += "]"; PyRun_SimpleString(szVarCode.toUtf8().data()); //clean "cr" from the python code (ticket #1028) QString szCleanCode = szCode; szCleanCode.replace(QRegExp("\r\n?"), "\n"); // execute some python code retVal = PyRun_SimpleString(szCleanCode.toUtf8().data()); szRetVal.setNum(retVal); if (PyErr_Occurred() || retVal) { szError = g_lError; } // clear the thread state PyThreadState_Swap(NULL); // release our hold on the global interpreter PyEval_ReleaseLock(); if(retVal) return false; return true; }
void XBPyThread::stop() { CSingleLock lock(m_pExecuter->m_critSection); if(m_stopping) return; m_stopping = true; if (m_threadState) { PyEval_AcquireLock(); PyThreadState* old = PyThreadState_Swap(m_threadState); PyObject *m; m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "XBPyThread::stop - failed to set abortRequested"); for(PyThreadState* state = m_threadState->interp->tstate_head; state; state = state->next) { Py_XDECREF(state->async_exc); state->async_exc = PyExc_SystemExit; Py_XINCREF(state->async_exc); } PyThreadState_Swap(old); PyEval_ReleaseLock(); } }
void XBPyThread::OnException() { done = true; m_threadState = NULL; CLog::Log(LOGERROR,"%s, abnormally terminating python thread", __FUNCTION__); PyEval_ReleaseLock(); m_pExecuter->setDone(m_id); }
void motor_env_init_v_multi_thread() { motor_env_init(); if(!PyEval_ThreadsInitialized()) PyEval_InitThreads(); mainThreadState = PyThreadState_Get(); PyEval_ReleaseLock(); }
void ModuleLoader::init() { Py_InitializeEx(0); PyEval_InitThreads(); //note, this implicitly acquires the lock! g_pymaintstate = PyThreadState_Get(); PyEval_ReleaseLock(); }
/** * Execute the current script * We are now in the thread. */ void PyApi::ExecuteInThread() const { assert(Py_IsInitialized() ); // get the lock so we can change things. PyEval_AcquireLock(); // make sure that the main thread is the active one. const auto mainInterpreterState = _mainThreadState->interp; PyThreadState_Swap(_mainThreadState); // create a new thread. const auto myThreadState = PyThreadState_New(mainInterpreterState); // make sure that the new thread has control // https://docs.python.org/3/c-api/init.html PyThreadState_Swap(myThreadState); // execute it... { const auto main_module = PyImport_AddModule("__main__"); const auto main_dict = PyModule_GetDict(main_module); Py_XINCREF(main_module); const auto local_dic = PyDict_New(); Py_XINCREF(local_dic); // we can now run our script const auto s = _script.c_str(); const auto pyRes = PyRun_String(s, Py_file_input, main_dict, local_dic); CheckForPythonErrors(); PyDict_Clear(local_dic); Py_XDECREF(local_dic); // pending calls must be cleared out Py_XDECREF(main_module); } // swap back to this thread. PyThreadState_Swap(myThreadState); // clear anything left behind. PyThreadState_Clear(myThreadState); PyThreadState_Swap(nullptr); // delete my thread. PyThreadState_Delete(myThreadState); // give control back to main thread PyThreadState_Swap(_mainThreadState); // release the lock one last time. PyEval_ReleaseLock(); }
void XBPyThread::OnException() { PyThreadState_Swap(NULL); PyEval_ReleaseLock(); CSingleLock lock(m_pExecuter->m_critSection); m_threadState = NULL; CLog::Log(LOGERROR,"%s, abnormally terminating python thread", __FUNCTION__); m_pExecuter->setDone(m_id); }
static int rw_seek_th(SDL_RWops* context, int offset, int whence) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; int retval; PyThreadState* oldstate; if(!helper->seek || !helper->tell) return -1; PyEval_AcquireLock(); oldstate = PyThreadState_Swap(helper->thread); if(!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/ { result = PyObject_CallFunction(helper->seek, "ii", offset, whence); if(!result) { PyErr_Clear(); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } Py_DECREF(result); } result = PyObject_CallFunction(helper->tell, NULL); if(!result) { PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } retval = PyInt_AsLong(result); Py_DECREF(result); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return retval; }
PythonThreadState::~PythonThreadState() { if(m_thisThreadState) { PyThreadState_Swap(m_mainThreadState); PyThreadState_Clear(m_thisThreadState); PyThreadState_Delete(m_thisThreadState); PyEval_ReleaseLock(); } }
void XBPyThread::stop() { CSingleLock lock(m_pExecuter->m_critSection); if(m_stopping) return; m_stopping = true; if (m_threadState) { PyEval_AcquireLock(); PyThreadState* old = PyThreadState_Swap((PyThreadState*)m_threadState); //tell xbmc.Monitor to call onAbortRequested() g_pythonParser.OnAbortRequested(); PyObject *m; m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "XBPyThread::stop - failed to set abortRequested"); PyThreadState_Swap(old); PyEval_ReleaseLock(); if(!stoppedEvent.WaitMSec(5000))//let the script 5 secs for shut stuff down { CLog::Log(LOGERROR, "XBPyThread::stop - script didn't stop in proper time - lets kill it"); } //everything which didn't exit by now gets killed PyEval_AcquireLock(); old = PyThreadState_Swap((PyThreadState*)m_threadState); for(PyThreadState* state = ((PyThreadState*)m_threadState)->interp->tstate_head; state; state = state->next) { Py_XDECREF(state->async_exc); state->async_exc = PyExc_SystemExit; Py_XINCREF(state->async_exc); } PyThreadState_Swap(old); PyEval_ReleaseLock(); } }
void gil_real_release() { //uwsgi_log("UNLOCK %d\n", uwsgi.mywid); #if !defined(PYTHREE) && !defined(UWSGI_PYPY) pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Swap(NULL)); PyEval_ReleaseLock(); #else pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get()); PyEval_SaveThread(); #endif }
/** Cleanup any thread local storage on pthread_exit() */ static void do_python_cleanup(void *arg) { PyThreadState *my_thread_state = arg; PyEval_AcquireLock(); PyThreadState_Swap(NULL); /* Not entirely sure this is needed */ PyThreadState_Clear(my_thread_state); PyThreadState_Delete(my_thread_state); PyEval_ReleaseLock(); }
PythonEnv* PythonEnv_Init() { PythonEnv* Self = (PythonEnv*) malloc( sizeof( PythonEnv ) ); Py_Initialize(); PyEval_InitThreads(); Self->MainThreadState = PyThreadState_Get(); PyEval_ReleaseLock(); return Self; }
/* analogue of PyEval_SaveThread() */ BPy_ThreadStatePtr BPY_thread_save(void) { PyThreadState *tstate = PyThreadState_Swap(NULL); /* note: tstate can be NULL when quitting Blender */ if (tstate && PyEval_ThreadsInitialized()) { PyEval_ReleaseLock(); } return (BPy_ThreadStatePtr)tstate; }
PythonInterpreter::~PythonInterpreter() { std::lock_guard<std::mutex> lock(globalMutex); // Acquire the global interpreter lock. PyEval_RestoreThread(state); // Destroy the interpreter. Py_EndInterpreter(state); // Release the global interpreter lock. PyEval_ReleaseLock(); }
void XBPyThread::stop() { if (m_threadState) { PyEval_AcquireLock(); m_threadState->c_tracefunc = xbTrace; m_threadState->use_tracing = 1; PyEval_ReleaseLock(); } stopping = true; }
void spyceClose() { if(spyInitialized) { PyEval_AcquireLock(); PyThreadState_Swap(NULL); PyThreadState_Clear(spyThreadState); PyThreadState_Delete(spyThreadState); PyEval_ReleaseLock(); g_pythonParser.Finalize(); } }
static int rw_read_th(SDL_RWops* context, void* ptr, int size, int maxnum) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; int retval; PyThreadState* oldstate; if(!helper->read) return -1; PyEval_AcquireLock(); oldstate = PyThreadState_Swap(helper->thread); result = PyObject_CallFunction(helper->read, "i", size * maxnum); if(!result) { PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } if(!PyString_Check(result)) { Py_DECREF(result); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } retval = PyString_GET_SIZE(result); memcpy(ptr, PyString_AsString(result), retval); retval /= size; Py_DECREF(result); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return retval; }
void PyThreadState_DeleteCurrent() { PyThreadState *tstate = _PyThreadState_Current; if (tstate == NULL) Py_FatalError( "PyThreadState_DeleteCurrent: no current tstate"); _PyThreadState_Current = NULL; tstate_delete_common(tstate); if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) PyThread_delete_key_value(autoTLSkey); PyEval_ReleaseLock(); }
void PyThreadState_DeleteCurrent() { PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( &_PyThreadState_Current); if (tstate == NULL) Py_FatalError( "PyThreadState_DeleteCurrent: no current tstate"); _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL); if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) PyThread_delete_key_value(autoTLSkey); tstate_delete_common(tstate); PyEval_ReleaseLock(); }
static void _py_init_interpreter(void) { if (!interpreter_initialized) { Py_Initialize(); PyEval_InitThreads(); python_log_message_init(); PyEval_ReleaseLock(); interpreter_initialized = TRUE; } }
void KviPythonInterpreter::done() { if(!m_pThreadState)return; // grab the lock PyEval_AcquireLock(); // swap my thread state out of the interpreter PyThreadState_Swap(NULL); // clear out any cruft from thread state object PyThreadState_Clear(m_pThreadState); // delete my thread state object PyThreadState_Delete(m_pThreadState); // release the lock PyEval_ReleaseLock(); m_pThreadState = 0; }
int PythonModule_Destroy( PythonModule* Self ) { if( Self != NULL ) { PyEval_AcquireLock(); PyThreadState_Swap(NULL); PyThreadState_Clear(Self->CurrentThreadState); PyThreadState_Delete(Self->CurrentThreadState); PyEval_ReleaseLock(); } free( Self ); }
void PyThreadState_DeleteCurrent() { PyThreadState *tstate = _PyThreadState_GET(); if (tstate == NULL) Py_FatalError( "PyThreadState_DeleteCurrent: no current tstate"); tstate_delete_common(tstate); if (_PyRuntime.gilstate.autoInterpreterState && PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate) { PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL); } _PyThreadState_SET(NULL); PyEval_ReleaseLock(); }
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(); }
gpointer pass_to_motor_v_multi_thread(gpointer args){ PyEval_AcquireLock(); PyInterpreterState * mainInterpreterState = mainThreadState->interp; PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(myThreadState); pass_to_motor(args); PyThreadState_Swap(NULL); PyThreadState_Clear(myThreadState); PyThreadState_Delete(myThreadState); PyEval_ReleaseLock(); return NULL; }
static int on_startup() { struct stat fi; if (!project_app || !*project_app) { nxweb_log_error("python wsgi app not specified; skipping python initialization"); return 0; } static const char* prog_name="python/nxwebpy.py"; if (stat(prog_name, &fi)==-1) { #ifdef NXWEB_LIBDIR prog_name=NXWEB_LIBDIR "/nxwebpy.py"; if (stat(prog_name, &fi)==-1) { #endif nxweb_log_error("%s is missing; skipping python initialization", prog_name); return 0; #ifdef NXWEB_LIBDIR } #endif } Py_SetProgramName((char*)prog_name); // initialize thread support PyEval_InitThreads(); Py_Initialize(); char *a[]={(char*)prog_name, (char*)project_root, (char*)project_app, (char*)virtualenv_path}; PySys_SetArgv(4, a); PyObject* py_module_name=PyString_FromString(MODULE_NAME); assert(py_module_name); // save a pointer to the main PyThreadState object py_main_thread_state=PyThreadState_Get(); py_module=PyImport_Import(py_module_name); if (!py_module || !PyModule_Check(py_module)) { fprintf(stderr, "can't load python module %s; check parse errors:\n", MODULE_NAME); PyErr_Print(); exit(0); } Py_DECREF(py_module_name); py_nxweb_on_request_func=PyObject_GetAttrString(py_module, FUNC_NAME); assert(py_nxweb_on_request_func && PyCallable_Check(py_nxweb_on_request_func)); // release the lock PyEval_ReleaseLock(); return 0; }