type-conversionswigpython-bindings

From python how to pass frame object received in sys.settrace callback to c++ function which takes void *


from the call back function of sys.settrace how to pass frame object to a c++ which accepts void *

Limitations (in a given situation): Forced to use sys.settrace (can not use PyEval_SetTrace) Also c++ function can not accept PyObject* or PyFrameObject*

C++ code (using SWIG python bindings are generated for this):

class TEST_DECLS InterpPython{
static int TraceHook(void *frame, hwString what, void * arg);
}

Python code:

sys.settrace(_trace_hook)
def _trace_hook(frame, event, arg):
  InterpPython_TraceHook(frame,event, arg)

Results:

TypeError: in method 'InterpPython_TraceHook', argument 1 of type 'void *'


Solution

  • Try a typemap to convert the Python input object to a void* when it is a void* frame parameter. You could also add a check that the object is actually a PyFrameObject.

    %typemap(in) void* frame %{
        $1 = (void*)$input;
    %}