저는 C++/Python 혼합 언어 프로그래밍에 익숙하지 않으며 Python/C API에 대해별로 생각하지 않습니다. 방금 Boost.Python을 사용하여 파이썬 용 C++ 라이브러리를 포장하기 시작했습니다. 인수로 배열에 대한 포인터를 사용하는 함수를 래핑하는 데 막혔습니다. 다음 (두 번째 ctor)은 C++의 프로토 타입입니다.파이썬에서 래핑 된 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>());
성공적으로 컴파일되고 링크되었지만 파이썬에서 호출하는 방법을 알아 내지 못했습니다. 다음과 같은 나의 순진한 시도는 실패했다.
>>> 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에 대해서는별로 생각하지 못했습니다. C++ 배열로 파이썬리스트를 변환하는 방법을 찾을 수 있도록 어딘가에서 가리킬 수 있습니까? – Aamir
이 경우 "to C array"라고 말하지만 그럼에도 몇 가지 예제 코드를 추가했습니다. – andreabedini