开发者

SWIG and triggering a Python callback from C code

开发者 https://www.devze.com 2023-03-24 03:34 出处:网络
Apologies for not being familiar with formatting on here...I\'ve made some progress thanks to helpful replies and edited and removed my original

Apologies for not being familiar with formatting on here...I've made some progress thanks to helpful replies and edited and removed my original question to be replaced by the current one.

My problem lies with converting a C struct or struct pointer to PyObject. There is no alternative to this because I am wrapping an existing C library whose callback requires a C struct pointer.

Following works but with limitations:

%module cain1
%{

  typedef struct {
    double price;
    int volume;
  } book_entry_t;

  typedef struct {

    char symbol[10];
    book_entry_t *book;

  } trade_t;

  typedef void (*CALLBACK)(trade_t trade);

  CALLBACK my_callback = 0;
  st开发者_C百科atic PyObject *my_pycallback = NULL;

  static void bigSnake(trade_t trade)
  {
    PyObject *result;

    PyObject *d1;

    result =  PyEval_CallObject(my_pycallback,

                Py_BuildValue("(y#)",
                          (char*)&trade,
                          sizeof(trade_t)
                          )

                );

    Py_XDECREF(result);
    return /*void*/;

  }

  void test_cb (PyObject *callMe1) {
    trade_t d1;
    book_entry_t b1;
    b1.price = 123.45;
    b1.volume = 99;


    Py_XINCREF(callMe1);         /* Add a reference to new callback */
    my_pycallback = callMe1;     /* Remember new callback */

    strcpy (d1.symbol,"Gupta Ltd");
    d1.book = &b1;


    bigSnake(d1);

  }


%}


// Expose in python module..
typedef struct {
  double price;
  int volume;
} book_entry_t;

typedef struct {

  char symbol[10];
  book_entry_t *book;

} trade_t;


void test_cb(PyObject *callMe1);

and then triggering the callback from Python:

import cain1
import struct 

def dave(d1):
    N1,N2 = struct.unpack('10sP', d1)
    print ('\n   %s: %x' % (N1.decode() ,N2))

    price,volume = struct.unpack('di',N2)

    print (price,volume)

def main():
    cain1.test_cb(dave) 

main()

but I am unable to recover the book_entry_t strcut contents pointed to by trade_t....

I just feel this is all too convoluted since I have the pointer to structs and there must be a straightforward way for Python to use that without any fuss.


Py_BuildValue("(N)",details) expects a PyObject* (your "N" says so), and you pass it something very different. Try Py_BuildValue("(i)", details.index) instead, and change it to accomodate any changes in details_t.


You're attempting to build a PyObject from a details_t struct. This isn't valid. Either pass the callback an integer (seems easier since details_t only has the one field) OR create a proper PyObject type. You can't blindly cast one type to another and expect it to work (a PyObject is more than just a pointer).

0

精彩评论

暂无评论...
验证码 换一张
取 消