×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Python C++ Extension with Callback crashing

    Hi Guys,

    Has anybody of you successfully extended (using C++) the Python API using callbacks?

    Scenario:
    -Register a Python callback function (to a C++ extension to Python)
    -After the registration an C++ - RTimer Object is activated
    -When the timer calls RunL -> call Python callback.

    Problem:
    -The Problem is that the _g_PyThreadState_Current is set to 0 when the Timer-Event happens. This causes the Python Interpreter to crash during the call of "PyEval_CallObject" (because of the use of this NULL-Pointer).

    Questions:
    -Is it necessary to make a special call to SPy_get_globals(), SPy_get_thread_locals(), SPy_globals_initialize(), ...?

    Here are some code snippets:
    Registering the Callback
    Code:
    static PyObject*
    tcb_register(PyObject *self, PyObject *args)
    {
      PyObject *temp;
      
      if (PyArg_ParseTuple(args, "O:tcb_register", &temp))
        {
          if (!PyCallable_Check(temp))
    	{
    	  PyErr_SetString(PyExc_TypeError, "Callback must be a callable method");
    	  return NULL;
    	}
    
          Py_XINCREF(temp);
          Py_XDECREF(callback_function);
          callback_function = temp;
    
          startTimer();
          /* return result */
          Py_INCREF(Py_None);
          return Py_None;      
        }
      return NULL;
    }
    
    
    static const PyMethodDef myMethods[] = {
      {"register", tcb_register, METH_VARARGS, "Register the callback for 2s timer"},
      {"call", tcb_callback, METH_VARARGS, "Call the callback"},
      {NULL, NULL, 0, NULL} /* Sentinel */
    };
    
    
    DL_EXPORT(void) initmymod(void)
    {
      Py_InitModule("timercb", (PyMethodDef*)myMethods);
    }
    The Timer-Object:
    Code:
    CMyTimer::CMyTimer(TInt aPriority) : CActive(aPriority)
    {  
      CActiveScheduler::Add(this);
      iTimer = new (ELeave) RTimer;
      iTimer->CreateLocal();
    };
    
    CMyTimer::~CMyTimer()
    {  
      iTimer->Close();
      delete iTimer;
    }
    
    void CMyTimer::StartTimer()
    {
      iTimer->After(iStatus,3000000);
      SetActive();
    }
    
    void CMyTimer::DoCancel()
    {}
    
    void CMyTimer::RunL()
    { 
      test_callback();
    }
    and the function that calls the Python Callback:
    Code:
      static void test_callback()
      {
        PyObject *arglist;
        PyObject *result = NULL;
    
        /* call callback */
        arglist= Py_BuildValue("((i, i))", 3,7);
        result = PyEval_CallObject(callback_function, arglist);
        Py_DECREF(arglist);
    
        if (result != NULL)
          Py_DECREF(result);  
        
      }
    Thx.

    Cheers, Digger

  2. #2
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: Python C++ Extension with Callback crashing

    The interpreter has the invariant that the interpreter state is always saved prior to entering the active scheduler. If you want to invoke Python/C API functions inside a RunL method you need to restore the state, call the functions and save it again before returning from the method. See pages 70-74 of the API Reference. I admit that the explanation there could be better.

    Try this:
    Code:
    void CMyTimer::RunL()
    { 
      PyEval_RestoreThread(PYTHON_TLS->thread_state);
      test_callback();
      PyEval_SaveThread();
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Re: Python C++ Extension with Callback crashing

    Thanx. Works now

Similar Threads

  1. Python for Series 60 released on Forum Nokia!
    By eriksmartt in forum Symbian
    Replies: 5
    Last Post: 2009-07-14, 17:00
  2. Recording in Amr Format
    By losym in forum Symbian Media (Closed)
    Replies: 12
    Last Post: 2009-03-26, 06:41
  3. Use filebrowser.py to execute your python scripts.
    By zweiberg in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2006-11-01, 13:24
  4. Python from C++ app.
    By micmen in forum Symbian
    Replies: 4
    Last Post: 2005-06-09, 12:01

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×