无法访问使用Boost Python公开的C++类方法

2024-04-26 19:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手,目前尝试将一些C++ API暴露到Python中boost.python. 在

我从一个简单的类开始,它与我要实现的目标很接近:

class BaseImpl
{
public:
    BaseImpl() {}

    virtual int f()
    {
        return 10;
    }
};

class BaseImplWrap : public BaseImpl, public wrapper<BaseImpl>
{
public:
    int f()
    {
        return this->get_override("f")();
    }
};

暴露如下:

^{pr2}$

当我试图访问PythonWin上的exposed class方法时,出现以下错误:

PythonWin 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import test
>>> a=test.BaseImpl()
>>> a.fFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x000000000533ADF0> is not a Python function

然后,我从Exporting Class教程中尝试了一些更简单的方法,但仍然无法使用它,并出现以下错误:

namespace  {
    class hello
    {
        public:
            hello(const std::string& country) { this->country = country; }
            std::string greet() const { return "Hello from " + country; }

        private:
            std::string country;
    };
}

BOOST_PYTHON_MODULE(test)
{
    class_<hello>("hello", init<std::string>())
        .def("greet", &hello::greet)  // Add a regular member function.
        ;
}

>>> import test
>>> a=test.hello('world')
>>> a.greetFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x0000000002FEACA0> is not a Python function

知道为什么公开的类方法不被识别为python函数吗?这跟我的系统配置有关吗?在

我在windows7x64机器上运行,python2.7boost1.54.0和MSVC 2012 x64。在

提前谢谢。在


Tags: inpytestformathellolineargfunction