2010-02-01 52 views
5

我有一个方法导出到Python使用boost python,它需要一个boost :: function作为参数。Python的方法来提升功能

从我读过的boost ::蟒蛇应该支持的boost ::功能,无需大惊小怪,但是当我尝试调用函数与蟒蛇方法它给了我这个错误

Boost.Python.ArgumentError: Python argument types in 
    Class.createTimer(Class, int, method, bool) 
did not match C++ signature: 
    createTimer(class Class {lvalue}, unsigned long interval, 
    class boost::function<bool _cdecl(void)> function, bool recurring=False) 

我打电话它从蟒与此代码

self.__class.createTimer(3, test.timerFunc, False) 

和在C++中它被定义为

boost::int32_t createTimer(boost::uint32_t interval, boost::function< bool() > function, bool recurring = false); 

这里的目标是一个定时器类,我可以做这样的事情

class->createTimer(3, boost::bind(&funcWithArgs, arg1, arg2)) 

创建执行funcWithArgs一个计时器。多亏了boost绑定,它可以和任何函数或方法一起工作。

那么我需要什么语法来使用boost :: python来接受我的python函数作为boost :: function?

回答

10

得到了蟒蛇邮件列表上的回答,有点改造和更多的研究,我正是我想要:)

我没有看到mithrandi之前的文章,但我并不想再的主意宣布这样的功能。随着一些花哨的包装和一点蟒蛇魔术这可以工作,并在同一时间看起来不错!

开始,包你的Python对象有这样的代码

当在类中定义,像这样

.def("createTimer", &createTimerWrapper, (bp::arg("interval"), bp::arg("function"), bp::arg("recurring") = false)) 

方法随着包装的那一点,你可以使用魔法一样这

import MyLib 
import time 

def callMePls(): 
    print("Hello world") 
    return True 

class = MyLib.Class() 

class.createTimer(3, callMePls) 

time.sleep(1) 

要完全模仿C++,我们还需要一个boost :: bind工具通货膨胀可以在这里找到:http://code.activestate.com/recipes/440557/

就这样,我们现在可以做这样的事情

import MyLib 
import time 

def callMePls(str): 
    print("Hello", str) 
    return True 

class = MyLib.Class() 

class.createTimer(3, bind(callMePls, "world")) 

time.sleep(1) 

编辑:

我想跟进我的问题时,我可以。我成功地使用了这段代码了一段时间,但是我发现当你想在对象构造函数中使用boost :: function的时候,这会崩溃。 有一种方法可以使它与此类似工作,但是您构造的新对象最终具有不同的签名,并且不会与其他对象一起使用。

这终于搞砸了我足以做些什么,因为我知道更多关于boost :: python现在我想出了一个很好的'适合所有'使用转换器的解决方案。 这里的代码将一个python callable转换为一个boost :: python < bool()>对象,它可以很容易地修改为转换为其他boost函数。

// Wrapper for timer function parameter 
struct timer_func_wrapper_t 
{ 
    timer_func_wrapper_t(bp::object callable) : _callable(callable) {} 

    bool operator()() 
    { 
     return _callable(); 
    } 

    bp::object _callable; 
}; 

struct BoostFunc_from_Python_Callable 
{ 
    BoostFunc_from_Python_Callable() 
    { 
     bp::converter::registry::push_back(&convertible, &construct, bp::type_id< boost::function< bool() > >()); 
    } 

    static void* convertible(PyObject* obj_ptr) 
    { 
     if(!PyCallable_Check(obj_ptr)) return 0; 
     return obj_ptr; 
    } 

    static void construct(PyObject* obj_ptr, bp::converter::rvalue_from_python_stage1_data* data) 
    { 
     bp::object callable(bp::handle<>(bp::borrowed(obj_ptr))); 
     void* storage = ((bp::converter::rvalue_from_python_storage< boost::function< bool() > >*) data)->storage.bytes; 
     new (storage)boost::function< bool() >(timer_func_wrapper_t(callable)); 
     data->convertible = storage; 
    } 
}; 

然后在你的初始化代码,即BOOST_PYTHON_MODULE(),只需通过创建结构

BOOST_PYTHON_MODULE(Foo) 
{ 
    // Register function converter 
    BoostFunc_from_Python_Callable(); 
注册类型