我是C++/Python混合語言編程的新手,對Python/C API沒有太多的想法。我剛開始使用Boost.Python來封裝Python的C++庫。我被困在包裝一個函數,該函數將指針指向一個數組作爲參數。 (2nd ctor)是C++中的原型。如何將指針傳遞給Python中的數組以用於包裝C++函數
class AAF{
AAF(AAF_TYPE t);
AAF(double v0, const double * t1, const unsigned * t2, unsigned T);
~AAF();
}
我是否正確地把它包裝在boost :: python中?
class_<AAF>("AAF", init<AAF_TYPE>())
.def(init<double, const double*, const unsigned*, unsigned>());
請注意,它編譯和鏈接成功,但我無法弄清楚如何在Python中調用它。我天真的嘗試像下面的失敗。
>>> z = AAF(10, [4, 5.5, 10], [1, 1, 2], 3);
Traceback (most recent call last):
File "./test_interval.py", line 40, in <module>
z = AAF(10, [4, 5.5, 10], [1, 1, 2], 3);
Boost.Python.ArgumentError: Python argument types in
AAF.__init__(AAF, int, list, list, int)
did not match C++ signature:
__init__(_object*, AAF_TYPE)
__init__(_object*, double, double const*, unsigned int const*, unsigned int)
>>> t1 = array.array('d', [4, 5.5, 10])
>>> t2 = array.array('I', [1, 1, 2])
>>> z = AAF(10, t1, t2, 3);
Traceback (most recent call last):
File "./test_interval.py", line 40, in <module>
z = AAF(10, t1, t2, 3);
Boost.Python.ArgumentError: Python argument types in
AAF.__init__(AAF, int, array.array, array.array, int)
did not match C++ signature:
__init__(_object*, AAF_TYPE)
__init__(_object*, double, double const*, unsigned int const*, unsigned int)
我的第二個問題是,我是否也需要包裝析構函數?請說明在某些情況下這可能是必要的,但並非總是如此。
感謝您的回覆,但正如我所說的,我對Python/C API沒有太多的想法。你能指點什麼地方,以便我可以找到一種方法將Python列表轉換爲C++數組嗎? – Aamir 2009-06-02 18:40:20