Python callback from C++

随声附和 提交于 2021-02-08 07:25:21

问题


I have a C++ class

class EventHandler {
virtual long readFromDaemon(void *buf, size_t count) = 0;
};

and a Python program that uses it

class Handler(EventHandler):
    def readFromDaemon(self, buf, count):
    ...

C++ code calls EventHandler::readFromDaemon(). SWIG converts arguments and calls Python's readFromDaemon():

long SwigDirector_EventHandler::readFromDaemon(void *buf, size_t count) {
  ...
  swig::SwigVar_PyObject obj0;
  obj0 = SWIG_NewPointerObj(SWIG_as_voidptr(buf), SWIGTYPE_p_void,  0 );

  swig::SwigVar_PyObject obj1;
  obj1 = SWIG_From_size_t(static_cast< size_t >(count));
  ...
  swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar((char *)"readFromDaemon");
  swig::SwigVar_PyObject result = PyObject_CallMethodObjArgs(swig_get_self(), (PyObject *) swig_method_name ,(PyObject *)obj0,(PyObject *)obj1, NULL);

I want to convert (void *buf, size_t count) to PyBytesObject

def readFromDaemon(self, buf):
    # buf is bytes

But I didn't found a way how to achieve it.

%typemap(in) (void *buf, size_t count) { ... } does not help.

回答1:


Here's an example of reading and writing to C++ data buffers. the C++ functions in my example are:

void read(void* buf, size_t count);
void write(const void* buf, size_t count);

SWIG interface file:

%module x

%include <exception.i>

// Handle an input-only (const) buffer.
// A single Python input is translated into a buffer and a size.
%typemap(in) (const void *buf, size_t count) (Py_ssize_t tmp) %{
    if(PyBytes_AsStringAndSize($input,(char**)&$1,&tmp) == -1)
        return NULL;
    $2 = tmp;
%}

// Handle an output-only (non-const) buffer.
// The single integer Python input is allocated in a temporary
// buffer and the pointer and its size are created as C++ parameters.
%typemap(in,numinputs=1) (void *buf, size_t count) (long tmp) %{
    if(!PyLong_Check($input))
        SWIG_exception(SWIG_TypeError,"expected integer");
    tmp = PyLong_AsLong($input);
    if(tmp < 1 || tmp > 65535)
        SWIG_exception(SWIG_ValueError,"expected value 1-65535");
    $2 = tmp;
    $1 = new char[$2];
%}

// The pair of output arguments are translated into a single
// Python bytes object and appended to any existing return value.
%typemap(argout) (void *buf, size_t count) (PyObject* po) %{
    po = PyBytes_FromStringAndSize((char*)$1,$2);
    $result = SWIG_Python_AppendOutput($result,po);
%}

// Free the temporary buffer created in the "in" typemap.
%typemap(freearg) (void* buf, size_t count) %{
    delete $1;
%}

// SWIG will wrap these two functions prototypes.
void read(void* buf, size_t count);
void write(const void* buf, size_t count);

// Implementation
%{
#include <iostream>
void write(const void* buf, size_t count)
{
    char* tmp = (char*)buf;
    for(size_t i = 0; i < count; i++)
        std::cout << i << ": " << (int)tmp[i] << std::endl;
}

void read(const void* buf, size_t count)
{
    char* tmp = (char*)buf;
    for(size_t i = 0; i < count; i++)
        tmp[i] = (char)i;
}
%}

Demo:

>>> import x
>>> x.read(10)
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'
>>> x.read(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: expected value 1-65535
>>> x.read(1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: expected value 1-65535
>>> x.write(b'abcdefg')
0: 97
1: 98
2: 99
3: 100
4: 101
5: 102
6: 103
>>> x.write(12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected bytes, int found



回答2:


I found a decision in another topic here.

%typemap(directorin) (void *buf, size_t count) {
    $input = PyBytes_FromStringAndSize(static_cast<char*>($1), $2);
}

But there is another problem. The memory buffer is copied from C++ to Python object. How to copy bytes back from python object to C++? There is memoryview since python 2.7, but what to do with 2.6 and olders versions?

This is C++ code that calls EventHandler::readFromDaemon, for clarity

std::vector<char> buf(maxlen);
long cnt = m_evt_handler->readFromDaemon(&buf[0], buf.size());

Here is wrapper code now

long SwigDirector_EventHandler::readFromDaemon(void *buf, size_t count) {
  long c_result;
  swig::SwigVar_PyObject obj0;
  {
    obj0 = PyBytes_FromStringAndSize(static_cast<char*>(buf), count);
  }
  ...


来源:https://stackoverflow.com/questions/22407577/python-callback-from-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!