8

I have some C++ code that defines two classes, A and B. B takes an instance of A during construction. I have wrapped A with Boost.Python so that Python can create instances of A, as well as subclasses. I want to do the same with B.

class A {
    public:
        A(long n, long x, long y) : _n(n), _x(x), _y(y) {};
        long get_n() { return _n; }
        long get_x() { return _x; }
        long get_y() { return _y; }
    private:
        long _n, _x, _y;
};

class B {
    public:
        B(A a) : _a(a) {};
        doSomething() { ... };
    private:
        A _a;
};

While wrapping B, I needed to work out how to pass an instance of A to B's constructor. I did some digging and the solution I found was to write a "converter" class:

struct A_from_python_A {
    static void * convertible(PyObject* obj_ptr) {
        // assume it is, for now...
        return obj_ptr;
    }

    // Convert obj_ptr into an A instance
    static void construct(PyObject* obj_ptr,
                      boost::python::converter::rvalue_from_python_stage1_data* data) {
        // extract 'n':
        PyObject * n_ptr = PyObject_CallMethod(obj_ptr, (char*)"get_n", (char*)"()");
        long n_val = 0;
        if (n_ptr == NULL) {
            cout << "... an exception occurred (get_n) ..." << endl;
        } else {
            n_val = PyInt_AsLong(n_ptr);
            Py_DECREF(n_ptr);
        }

        // [snip] - also do the same for x, y

        // Grab pointer to memory into which to construct the new A
        void* storage = (
            (boost::python::converter::rvalue_from_python_storage<A>*)
            data)->storage.bytes;

        // in-place construct the new A using the data
        // extracted from the python object
        new (storage) A(n_val, x_val, y_val);

        // Stash the memory chunk pointer for later use by boost.python
        data->convertible = storage;
    }

    // register converter functions
    A_from_python_A() {
        boost::python::converter::registry::push_back(
            &convertible,
            &construct,
            boost::python::type_id<A>());
    }
};

Then I register this with:

BOOST_PYTHON_MODULE(interpolation_ext)
{
    // register the from-python converter for A
    A_from_python_A();

    class_<A>("A", init<long, long, long>())
        ;

    class_<B>("B", init<object>())
        ;
}

Convertible and construct are methods that answer the "is this convertible?" and "how to convert?" questions respectively. I have observed that the construct() method is non-trivial - it has to reach into A's PyObject*, extract all relevant fields, then rebuild a C++ instance that it then passes to B's constructor. Because A contains some private fields, it has to do this via public access mechanisms (whereas with a pure Python object it wouldn't have to, right?). This seems to work.

However, is the field extraction in the 'construct' function really necessary? It seems laborious. If A is a compound object, it could get very complicated, and possibly require one converter to invoke another. I perhaps understand the requirement if A is a Python class, but if the A instance originated from the C++ side, is there a way to determine that this is the case, and then simply get a handle (e.g. pointer) to this 'native' object, as a shortcut?

Here's the associated python code:

from my_ext import A, B
a = A(1,2,3)
b = B(a)
b.doSomething()
4

1 回答 1

10

简而言之,将B's 包装器定义为:

class_<B>( "B", init< A >() )

代替

class_<B>( "B", init< object >() )

在 Boost.Python(至少在 1.50 中)为类定义包装器时,class_模板会生成转换和构造函数。这允许A转换为 aA的包装器并从其构造。这些PyObject转换具有严格的类型检查,并要求在 python 中满足以下条件:isinstance( obj, A ).

自定义转换器通常用于支持:

  • 与现有 Python 类型的自动转换。例如,转换std::pair< long, long >为和从PyTupleObject.
  • 鸭打字。例如,B接受D不是从 派生的类,A只要D提供兼容的接口即可。

B从一个实例构造A

由于既不是现有的 Python 类型AB也不需要鸭子类型,因此不需要自定义转换器。要B获取 的实例A,可以像指定init获取A.

这是 and 的简化示例AB其中B可以从A.

class A
{
public:
  A( long n ) : n_( n ) {};
  long n() { return n_; }
private:
  long n_;
};

class B
{
public:
  B( A a ) : a_( a ) {};
  long doSomething() { return a_.n() * 2; }
private:
  A a_;
};

包装器将被定义为:

using namespace boost::python;
BOOST_PYTHON_MODULE(example)
{
  class_< A >( "A", init< long >() )
    ;

  class_<B>( "B", init< A >() )
    .def( "doSomething", &B::doSomething )
    ;
}

B的包装器明确表明它将A通过init< A >(). 此外,A的接口没有完全暴露给 Python 对象,因为没有为A::n()函数定义包装器。

>>> from example import A, B
>>> a = A( 1 )
>>> b = B( a )
>>> b.doSomething()
2

这也适用于派生自A. 例如:

>>> from example import A, B
>>> class C( A ):
...     def __init__( self, n ):
...         A.__init__( self, n )
... 
>>> c = C( 2 )
>>> b = B( c )
>>> b.doSomething()
4

但是,未启用鸭子类型。

>>> from example import A, B
>>> class E: pass
... 
>>> e = E()
>>> b = B( e )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    B.__init__(B, instance)
did not match C++ signature:
    __init__(_object*, A)

B从可转换为 的对象构造A

B为了支持可以从提供兼容接口的对象构造的情况,需要自定义转换器。A::n()尽管之前没有A为.get_num()int

首先,编写一个A_from_python提供转换器和构造函数的结构。

struct A_from_python
{
  static void* convertible( PyObject* obj_ptr )
  {
    // assume it is, for now...
    return obj_ptr;
  }

  // Convert obj_ptr into an A instance
  static void construct(
    PyObject* obj_ptr,
    boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    std::cout << "constructing A from ";
    PyObject_Print( obj_ptr, stdout, 0 );
    std::cout << std::endl;

    // Obtain a handle to the 'get_num' method on the python object.
    // If it does not exists, then throw.
    PyObject* n_ptr = 
      boost::python::expect_non_null( 
        PyObject_CallMethod( obj_ptr,
                             (char*)"get_num",
                             (char*)"()"  ));

    long n_val = 0;
    n_val = PyInt_AsLong( n_ptr );
    Py_DECREF( n_ptr );

    // Grab pointer to memory into which to construct the new A
    void* storage = (
      (boost::python::converter::rvalue_from_python_storage< A >*)
       data)->storage.bytes;

    // in-place construct the new A using the data
    // extracted from the python object
    new ( storage ) A( n_val );

    // Stash the memory chunk pointer for later use by boost.python
    data->convertible = storage;
  }

  A_from_python()
  {
    boost::python::converter::registry::push_back(
      &convertible,
      &construct,
      boost::python::type_id< A >() );
  }
};

boost::python::expect_non_null如果返回则用于抛出异常NULL。这有助于提供 python 对象必须提供方法的鸭式保证get_num。如果PyObject已知 是给定类型的实例,则可以使用boost::python::api::handleboost::python::api::object直接提取该类型,而不必通过PyObject接口进行一般性调用。

接下来,在模块中注册转换器。

using namespace boost::python;
BOOST_PYTHON_MODULE(example)
{
  // register the from-python converter for A
  A_from_python();

  class_< A >( "A", init< long >() )
    ;

  class_<B>( "B", init< A >() )
    .def( "doSomething", &B::doSomething )
    ;
}

AB或其关联的包装器定义未发生任何更改。创建了自动转换函数,然后在模块中定义/注册。

>>> from example import A, B
>>> a = A( 4 )
>>> b = B( a )
>>> b.doSomething()
8
>>> class D:
...     def __init__( self, n ):
...         self.n = n
...     def get_num( self ):
...         return self.n
... 
>>> d = D( 5 )
>>> b = B( d )
constructing A from <__main__.D instance at 0xb7f7340c>
>>> b.doSomething()
10
>>> class E: pass
...
>>> e = E()
>>> b = B( e )
constructing A from <__main__.E instance at 0xb7f7520c>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: get_num

D::get_num()存在,因此A是从DwhenD传递给构造函数的实例B构造的。但是,E::get_num()不存在,并且在尝试A从 的实例构造时引发异常E


另一种转换解决方案。

对于较大的类型,通过 C-API 实现鸭子类型可能会变得非常复杂。另一种解决方案是在 python 中执行鸭子类型,并将 python 文件与库一起分发。

example_ext.py将导入AandB类型,以及 monkey patchB的构造函数:

from example import A, B

def monkey_patch_B():
    # Store handle to original init provided by Boost.
    original_init = B.__init__

    # Construct an A object via duck-typing.
    def construct_A( obj ):
        return A( obj.get_num() )

    # Create a new init that will delegate to the original init.
    def new_init( self, obj ):
        # If obj is an instance of A, use it.  Otherwise, construct
        # an instance of A from object.
        a = obj if isinstance( obj, A ) else construct_A ( obj )

        # Delegate to the original init.
        return original_init( self, a )

    # Rebind the new_init.
    B.__init__ = new_init

monkey_patch_B()

最终用户所需的唯一更改是导入example_ext而不是example

>>> from example_ext import A, B
>>> a = A( 6 )
>>> b = B( a )
>>> b.doSomething()
12
>>> class D:
...     def __init__( self, n ):
...         self.n = n
...     def get_num( self ):
...         return self.n
... 
>>> d = D( 7 )
>>> b = B( d )
>>> b.doSomething()
14
>>> class E: pass
... 
>>> e = E()
>>> b = B( e )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example_ext.py", line 15, in new_init
    a = obj if isinstance( obj, A ) else construct_A ( obj )
  File "example_ext.py", line 9, in construct_A
    return A( obj.get_num() )
AttributeError: E instance has no attribute 'get_num'

由于修补的构造函数保证A将传递给的实例B,因此A_from_python::construct不会调用 。因此,输出中缺少打印语句。

虽然这种方法避免了 C-API,从而更容易执行鸭式打字,但它确实有一个主要的权衡,因为它需要专门为转换的部分 API 打补丁。另一方面,当自动类型转换功能可用时,不需要打补丁。


此外,无论如何,C++ 和 Python 中的访问控制都是为了防止意外误用。两者都不能防止故意获取对具有私人可见性的成员的访问权限。在 Python 中执行起来要容易得多,但在 C++ 标准中通过显式模板实例化特别允许这样做。

于 2012-08-15T20:43:28.920 回答