Beispiel #1
0
static pgint32
CPyStreamWrapper_Tell_Threaded (CPyStreamWrapper *wrapper)
{
    PyObject* result;
    pgint32 retval = 0;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->tell)
        return -1;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    result = PyObject_CallFunction (wrapper->tell, NULL);
    if (!result)
    {
        PyErr_Print ();
        retval = -1;
        goto end;
    }
    retval = PyInt_AsLong (result);
    Py_DECREF (result);

end:
#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
Beispiel #2
0
void MPyEmbed_CBPostFrame(void) {
	int i;
	if (!PythonAvailable) {
		return;
	}
	PyThread_acquire_lock(threaddatalock, WAIT_LOCK);
	for (i = 0; i < THREADS; i++) {
		if (VALID(threaddata[i])) {
			PyObject *cb = threaddata[i].postframe;
			PyThreadState *ts = threaddata[i].mainstate;
			PyObject *rv;

			PyThread_release_lock(threaddatalock);
			ts->thread_id = PyThread_get_thread_ident();
			PyEval_AcquireThread(ts);
			if (cb != NULL && PyCallable_Check(cb)) {
				rv = PyObject_CallObject(cb, NULL);
				Py_XDECREF(rv);
				if (rv == NULL) {
					PyErr_Print();
				}
			}
			PyEval_ReleaseThread(ts);
			PyThread_acquire_lock(threaddatalock, WAIT_LOCK);
		}
	}
	PyThread_release_lock(threaddatalock);
}
// Asuming we have a valid thread state, acquire the Python lock.
void PyXPCOM_InterpreterLock_Acquire()
{
	ThreadData *pData = (ThreadData *)PR_GetThreadPrivate(tlsIndex);
	NS_ABORT_IF_FALSE(pData, "Have no thread data for this thread!");
	PyThreadState *thisThreadState = pData->ts;
	PyEval_AcquireThread(thisThreadState);
}
Beispiel #4
0
static int
_pyobj_write_threaded (SDL_RWops *ops, const void* ptr, int size, int num)
{
    _RWWrapper *wrapper = (_RWWrapper *) ops->hidden.unknown.data1;
    PyObject *result;
    int retval;
    PyThreadState* oldstate;

    if (!wrapper->write)
        return -1;

    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);

#ifdef IS_PYTHON_3
    result = PyObject_CallFunction (wrapper->write, "y#", ptr, size * num);
#else
    result = PyObject_CallFunction (wrapper->write, "s#", ptr, size * num);
#endif
    if (!result)
    {
        PyErr_Print ();
        retval = -1;
        goto end;
    }
    Py_DECREF (result);
    retval = num;
    
end:
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
    return retval;
}
SCPyModule::~SCPyModule() {
  //PYLOCK;
  PyEval_AcquireThread(tstate);
  FILE* f = fopen("refs.txt", "w");
  
  _Py_PrintReferences(f);
  
  /* Disable signal handling */
  PyOS_FiniInterrupts();

  PyInterpreterState_Clear(interp);
  
  
  /* Delete current thread */
  PyThreadState_Swap(NULL);
  PyInterpreterState_Delete(interp);
  
  /* Sundry finalizers */
  PyMethod_Fini();
  PyFrame_Fini();
  PyCFunction_Fini();
  PyTuple_Fini();
  PyList_Fini();
  PySet_Fini();
  PyString_Fini();
  PyInt_Fini();
  PyFloat_Fini();
  
  PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
  
  _Py_PrintReferenceAddresses(f);

  fclose(f);
}
static void clRunPython(char* cmd)
{ 
  #define BSZ 2048
  char pyBuf[BSZ];
  //clprintf (CL_LOG_SEV_INFO, "clRunPython called with [%s]", cmd);
  //clOsalMutexLock(&pyMutex);
  PyThreadState *tstate;
  //PyObject *result;
  
  snprintf(pyBuf,BSZ,"safplus.Callback(\"\"\"%s\"\"\")\n",cmd);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython requesting python lock [%s]", cmd);

  /* interp is your reference to an interpreter object. */
  tstate = PyThreadState_New(thePythonInterpreter);
  PyEval_AcquireThread(tstate);
  
  //clprintf(CL_LOG_SEV_INFO,"Stage 1.  Passing to Python layer: %s",pyBuf);
  int ret = PyRun_SimpleString(pyBuf);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython requesting release of python lock [%s]", cmd);

  PyEval_ReleaseThread(tstate);

  if (ret != 0)  clprintf(CL_LOG_SEV_ERROR,"Ran: %s.  There was an error.",pyBuf);

  PyThreadState_Delete(tstate);
}
Beispiel #7
0
static bRC freePlugin(bpContext *ctx)
{
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   if (!p_ctx) {
      return bRC_Error;
   }

   /*
    * Stop any sub interpreter started per plugin instance.
    */
   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python cleanup calls.
    */
   if (p_ctx->bpContext) {
      Py_DECREF(p_ctx->bpContext);
   }

   if (p_ctx->pModule) {
      Py_DECREF(p_ctx->pModule);
   }

   Py_EndInterpreter(p_ctx->interpreter);
   PyEval_ReleaseLock();

   free(p_ctx);
   ctx->pContext = NULL;

   return bRC_OK;
}
static void
t_bootstrap(void *boot_raw)
{
	struct bootstate *boot = (struct bootstate *) boot_raw;
	PyThreadState *tstate;
	PyObject *res;

	tstate = PyThreadState_New(boot->interp);
	PyEval_AcquireThread(tstate);
	res = PyEval_CallObjectWithKeywords(
		boot->func, boot->args, boot->keyw);
	Py_DECREF(boot->func);
	Py_DECREF(boot->args);
	Py_XDECREF(boot->keyw);
	PyMem_DEL(boot_raw);
	if (res == NULL) {
		if (PyErr_ExceptionMatches(PyExc_SystemExit))
			PyErr_Clear();
		else {
			PySys_WriteStderr("Unhandled exception in thread:\n");
			PyErr_PrintEx(0);
		}
	}
	else
		Py_DECREF(res);
	PyThreadState_Clear(tstate);
	PyThreadState_DeleteCurrent();
	PyThread_exit_thread();
}
Beispiel #9
0
static int
CPyStreamWrapper_Close_Threaded (CPyStreamWrapper *wrapper)
{
    PyObject *result;
    int retval = 1;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->close)
        return -1;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    if (wrapper->close)
    {
        result = PyObject_CallFunction (wrapper->close, NULL);
        if (!result)
        {
            PyErr_Print ();
            retval = 0;
        }
        Py_XDECREF (result);
    }

#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
Beispiel #10
0
/**
 * z_policy_thread_destroy:
 * @self: this
 *
 * Destructor of ZPolicyThread.
 * The embedded Python thread context (self->thread) will be cleared and 
 * deleted also, and if this thread was the last one running in the
 * interpreter instance, that will be stopped, too.
 */
void
z_policy_thread_destroy(ZPolicyThread *self)
{
  /* acquires the interpreter lock */

  if (self->policy->main_thread != self)
    {
      /* we are one of the secondary threads */
      z_python_lock();
      PyThreadState_Swap(self->thread);
      PyThreadState_Clear(self->thread);
      PyThreadState_Swap(NULL);
      PyThreadState_Delete(self->thread);
      z_python_unlock();
      z_policy_unref(self->policy);
    }
  else
    {
      /* we must be freed at last, when the policy is being destroyed */
      g_assert(self->policy->ref_cnt == 1);
      /* we are the main thread, destroy the interpreter */
      z_policy_purge(self->policy);
      PyEval_AcquireThread(self->thread);
      Py_EndInterpreter(self->thread);
      z_python_unlock();
    }
  g_mutex_free(self->startable_lock);
  g_cond_free(self->startable_signal);
  g_free(self);
}
Beispiel #11
0
static int
CPyStreamWrapper_Seek_Threaded (CPyStreamWrapper *wrapper, pgint32 offset,
    int whence)
{
    PyObject* result;
    int retval = 1;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->seek)
        return 0;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    result = PyObject_CallFunction (wrapper->seek, "li", offset, whence);
    if (!result)
    {
        PyErr_Print();
        retval = 0;
        goto end;
    }
    Py_DECREF (result);
end:
#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
Beispiel #12
0
// Asuming we have a valid thread state, acquire the Python lock.
void PyWinInterpreterLock_Acquire()
{
	ThreadData *pData = (ThreadData *)TlsGetValue(dwTlsIndex);
	PyThreadState *thisThreadState = pData->ts;
	PyEval_AcquireThread(thisThreadState);
#ifdef TRACE_THREADSTATE
	InterlockedIncrement(&numAcquires);
#endif /* TRACE_THREADSTATE */
}
Beispiel #13
0
STDAPI DllUnregisterServer()
{
	int rc, pyrc;
	if (gPythoncom == 0)
		startUp();
	PyEval_AcquireThread(thisthread);
	rc = callSimpleEntryPoint("DllUnregisterServer", &pyrc);
	PyEval_ReleaseThread(thisthread);
	return rc==0 ? pyrc : SELFREG_E_CLASS;
}
Beispiel #14
0
int MPyEmbed_Repl(void) {
	if (!PythonAvailable) {
		return 0;
	}
	mainstate->thread_id = PyThread_get_thread_ident();
	PyEval_AcquireThread(mainstate);
	PyRun_AnyFile(stdin, "<stdin>");
	PyEval_ReleaseThread(mainstate);
	return 1;
}
Beispiel #15
0
/**
 * z_python_destroy:
 *
 * This function deinitializes the Python interpreter, it should be called
 * at program teardown.
 *
 * Returns TRUE if deinitialization was successful.
 **/
gboolean
z_python_destroy(void)
{
  if (initial_thread)
    {
      PyEval_AcquireThread(initial_thread);
      Py_Finalize();
    }
  return TRUE;
}
void clPyGlueTerminate()
{
    clRunPython("eoApp.Stop()");
    clOsalCondBroadcast (&event);
    clOsalMutexLock(&pyMutex);
    quit=1;
    PyEval_AcquireThread(thrdState);
    Py_Finalize();
    clOsalMutexUnlock(&pyMutex);
}
Beispiel #17
0
static bRC handlePluginEvent(bpContext *ctx, bDirEvent *event, void *value)
{
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
   bRC retval = bRC_Error;

   PyEval_AcquireThread(p_ctx->interpreter);
   retval = PyHandlePluginEvent(ctx, event, value);
   PyEval_ReleaseThread(p_ctx->interpreter);

   return retval;
}
Beispiel #18
0
static bRC setPluginValue(bpContext *ctx, pDirVariable var, void *value)
{
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
   bRC retval = bRC_Error;

   PyEval_AcquireThread(p_ctx->interpreter);
   retval = PySetPluginValue(ctx, var, value);
   PyEval_ReleaseThread(p_ctx->interpreter);

   return retval;
}
Beispiel #19
0
void IvrPython::onDTMFEvent(int detectedKey) {
  dtmfKey.set(detectedKey); // wake up waiting functions...

   if (onDTMFCallback == NULL) {
    DBG("IvrPython::onDTMFEvent, but script did not set onDTMF callback!\n");
    return;
  }
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback key is %d...\n", detectedKey);

#ifndef IVR_PERL
  PyThreadState *tstate;

  /* interp is your reference to an interpreter object. */
  tstate = PyThreadState_New(mainInterpreterThreadState->interp);
  PyEval_AcquireThread(tstate);

  /* Perform Python actions here.  */
  PyObject *arglist = Py_BuildValue("(i)", detectedKey);
  PyObject *result = PyEval_CallObject(onDTMFCallback, arglist);
  Py_DECREF(arglist);

  if (result == NULL) {
      DBG("Calling IVR" SCRIPT_TYPE "onDTMF failed.\n");
       PyErr_Print();
      //return ;
  } else {
      Py_DECREF(result);
  }

  /* Release the thread. No Python API allowed beyond this point. */
  PyEval_ReleaseThread(tstate);

  /* You can either delete the thread state, or save it
     until you need it the next time. */
  PyThreadState_Delete(tstate);
#else   //IVR_PERL
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback func is %s...\n", onDTMFCallback);

	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());
	dSP ;
	ENTER ;
	SAVETMPS ;
	PUSHMARK(SP) ;
	XPUSHs(sv_2mortal(newSViv(detectedKey)));
	PUTBACK ;
	call_pv(onDTMFCallback, G_DISCARD);
	FREETMPS ;
	LEAVE ;
#endif	//IVR_PERL
  DBG("IvrPython::onDTMFEvent done...\n");
}
Beispiel #20
0
/*
 * Do actual I/O.  Bareos calls this after startBackupFile
 *   or after startRestoreFile to do the actual file
 *   input or output.
 */
static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #21
0
/*
 * Handle an event that was generated in Bareos
 */
static bRC handlePluginEvent(bpContext *ctx, bsdEvent *event, void *value)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #22
0
/*
 * Set a plugin value (none defined)
 */
static bRC setPluginValue(bpContext *ctx, psdVariable var, void *value)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #23
0
/*
 * Called when starting to backup a file.  Here the plugin must
 *  return the "stat" packet for the directory/file and provide
 *  certain information so that Bareos knows what the file is.
 *  The plugin can create "Virtual" files by giving them a
 *  name that is not normally found on the file system.
 */
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #24
0
/*
 * Called after the file has been restored. This can be used to
 *  set directory permissions, ...
 */
static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #25
0
/* When using Incremental dump, all previous dumps are necessary */
static bRC checkFile(bpContext *ctx, char *fname)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Beispiel #26
0
int MPyEmbed_Run(char *file) {
	FILE *fp;
	if (!PythonAvailable) {
		return 0;
	}
	fp = fopen(file, "r");
	if (!fp) {
		perror("fopen");
		return 0;
	}
	mainstate->thread_id = PyThread_get_thread_ident();
	PyEval_AcquireThread(mainstate);
	PyRun_SimpleFile(fp, file);
	PyEval_ReleaseThread(mainstate);
	fclose(fp);
	return 1;
}
/*
 * Class:     jep_InvocationHandler
 * Method:    invoke
 * Signature: (Ljava/lang/String;JJ[Ljava/lang/Object;[Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
 */
JNIEXPORT jobject JNICALL Java_jep_InvocationHandler_invoke
(JNIEnv *env,
 jclass clazz,
 jstring jname,
 jlong _jepThread,
 jlong _target,
 jobjectArray args,
 jintArray types,
 jint returnType)
{

    JepThread     *jepThread;
    jobject        ret;
    const char    *cname;
    PyObject      *target;
    PyObject      *callable;

    target   = (PyObject *) (intptr_t) _target;
    ret      = NULL;
    callable = NULL;

    jepThread = (JepThread *) (intptr_t) _jepThread;
    if (!jepThread) {
        THROW_JEP(env, "Couldn't get thread objects.");
        return NULL;
    }

    PyEval_AcquireThread(jepThread->tstate);

    // now get the callable object
    cname = jstring2char(env, jname);
    // python docs say this returns a new ref. they lie like dogs.
    callable = PyObject_GetAttrString(target, (char *) cname);
    release_utf_char(env, jname, cname);

    if (process_py_exception(env, 0) || !callable) {
        goto EXIT;
    }

    ret = pyembed_invoke(env, callable, args, types);

EXIT:
    PyEval_ReleaseThread(jepThread->tstate);

    return ret;
}
Beispiel #28
0
void IvrPython::onMediaQueueEmpty() {
  isMediaQueueEmpty.set(true);

    DBG("executiong MQE callback...\n");
    if (onMediaQueueEmptyCallback == NULL) {
	DBG("IvrPython::onMediaQueueEmpty, but script did not set onMediaQueueEmpty callback.\n");
	return;
    }

#ifndef IVR_PERL
    PyThreadState *tstate;

    /* interp is your reference to an interpreter object. */
    tstate = PyThreadState_New(mainInterpreterThreadState->interp);
    PyEval_AcquireThread(tstate);

    /* Perform Python actions here.  */
    PyObject *arglist = Py_BuildValue("()");
    PyObject *result = PyEval_CallObject(onMediaQueueEmptyCallback, arglist);
    Py_DECREF(arglist);

    if (result == NULL) {
	DBG("Calling IVR" SCRIPT_TYPE "onMediaQueueEmpty failed.\n");
	    // PyErr_Print();
	//return ;
    } else {
	Py_DECREF(result);
    }

    /* Release the thread. No Python API allowed beyond this point. */
    PyEval_ReleaseThread(tstate);

    /* You can either delete the thread state, or save it
       until you need it the next time. */
    PyThreadState_Delete(tstate);
#else   //IVR_PERL
	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());

	dSP ;
	PUSHMARK(SP) ;
	call_pv(onMediaQueueEmptyCallback, G_DISCARD|G_NOARGS) ;
#endif	//IVR_PERL
    DBG("IvrPython::onMediaQueueEmpty done.\n");
}
Beispiel #29
0
/*
 * Class:     jep_python_PyPointer
 * Method:    decref
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL Java_jep_python_PyPointer_decref
(JNIEnv *env, jobject obj, jlong tstate, jlong pyobj)
{
    JepThread  *jepThread;
    PyObject   *pyObject;

    jepThread = (JepThread *) tstate;
    if (!jepThread) {
        THROW_JEP(env, "Couldn't get thread objects.");
        return;
    }

    pyObject = (PyObject*) pyobj;

    PyEval_AcquireThread(jepThread->tstate);
    Py_DECREF(pyObject);
    PyEval_ReleaseThread(jepThread->tstate);
}
static void clRunPython(char* cmd)
{ 
  #define BSZ 2048
  char pyBuf[BSZ];
  clprintf (CL_LOG_SEV_INFO, "clRunPython called with [%s]", cmd);
  //clOsalMutexLock(&pyMutex);
  thrdState = PyThreadState_Get();
  
  #if 0
  if (quit)
    {
      clprintf(CL_LOG_SEV_INFO,"Python has quit, so not running: %s",cmd);
      //clOsalMutexUnlock(&pyMutex);
      return;
    }
  #endif

#if 0
  PyEval_AcquireLock();
  PyThreadState_Swap(thrdState);
#endif

  snprintf(pyBuf,BSZ,"clCmdFromAsp(\"\"\"%s\"\"\")\n",cmd);

  clprintf (CL_LOG_SEV_INFO, "clRunPython requesting python lock [%s]", cmd);
  PyEval_AcquireThread(thrdState);
  clprintf(CL_LOG_SEV_INFO,"Stage 1.  Passing to Python layer: %s",pyBuf);
  int ret = PyRun_SimpleString(pyBuf);
  clprintf (CL_LOG_SEV_INFO, "clRunPython requesting release of python lock [%s]", cmd);
  PyEval_ReleaseThread(thrdState);

  if (ret != 0)  clprintf(CL_LOG_SEV_ERROR,"Ran: %s.  There was an error.",pyBuf);

#if 0
  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();
#endif

  //clprintf (CL_LOG_SEV_INFO, "clRunPython unlocking mutex [%s]", cmd);
  //clOsalMutexUnlock(&pyMutex);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython unlocked mutex [%s]", cmd);
}