我在调用python回调函数时试图“pythonize”方法参数:
%module (directors="1") py_test %feature("director") mgr; struct hdr_val { const char *hdr; const char *val; }; struct hdr_list { int count; struct hdr_val *elems; }; struct myinfo { int newcid; int oldcid; const char *uri; struct hdr_list hlist; }; %{ PyObject* make_hdrlist(const struct hdr_list *hl) { PyObject* result; result = PyList_New(hl->count); for(int i = 0; i count; i++) PyList_SetItem(result, i, Py_BuildValue("(ss)", hl->elems[i].hdr, hl->elems[i].val)); return result; } %} class mgr { public: mgr() { } virtual void doit(); virtual void done(const struct myinfo* i) // Will be redefined in python { } }; %typemap(out) struct myinfo* i { $result = Py_BuildValue("(iiso)", $1->newcid, $1->oldcid, $1->uri, make_hdrlist(&$1->hlist)); }
这样在python中我将能够执行以下操作:
import py_test class pymgr(py_test.mgr): def done(self, info): oldcid,newcid,uri,hlist = info
例如,我希望python中的info
参数是tuple("iiso")
而不是Swig包装器对象。
Unfortunatley SWIG由于某种原因忽略了我的typemap(out)
。
有什么想法吗?
答案 0 :(得分:1)
好的,我找到了答案 类型映射应放在BEFORE类mgr
之后%typemap(directorin) myinfo const * {
$input = Py_BuildValue("(iiso)", $1_name->newcid, $1_name->oldcid,
$1_name->uri, make_hdrlist(&$1_name->hlist));
}