python - 返回对象指针 : 'Cannot convert ' Coordinate *' to Python object'

标签 python c++ cython cythonize

我目前正在尝试对我的 C++ 类进行 cython 化,以便在 Python 中使用,我从我的一个简单类开始,但我一直坚持返回一个指针。我试图解决这个问题是添加一个复制构造函数并返回 C++ 类的 Python 版本,但没有成功。

我收到以下错误:

Error compiling Cython file:
------------------------------------------------------------
...

    def bearing(self, Coordinate *coordinate):
        return self.thisptr.bearing(coordinate)

    def destination(self, double bearing, double distance):
        return PyCCoordinate(self.thisptr.destination(bearing, distance))
                                                    ^
------------------------------------------------------------

./coordinate.pyx:32:53: Cannot convert 'Coordinate *' to Python object

这是我的文件

坐标.h

class Coordinate {
private:
    double m_x = 0;
    double m_y = 0;
public:
    Coordinate(const double x, const double y);

    Coordinate(const Coordinate *coord);

    void setX(const double value);

    void setY(const double value);

    double getX();

    double getY();

    double distance(Coordinate *coord);

    double bearing(Coordinate *coord);

    Coordinate *destination(const double bearing, const double distance);
};

坐标.cpp

#include <cmath>
#include "coordinate.h"

Coordinate::Coordinate(const double x, const double y) {
    m_x = x;
    m_y = y;
}

Coordinate::Coordinate(const Coordinate *coord) {
    m_x = coord->x;
    m_y = coord->y;
}


void Coordinate::setX(const double value) {
    m_x = value;
}

void Coordinate::setY(const double value) {
    m_y = value;
}

double Coordinate::getX() {
    return m_x;
}

double Coordinate::getY() {
    return m_y;
}

double Coordinate::distance(Coordinate *coord) {
    return sqrt(pow(m_x - coord->getX(), 2.0) + pow(m_y - coord->getY(), 2.0));
}

double Coordinate::bearing(Coordinate *coord) {
    const double len_x = coord->getX() - m_x;
    const double len_y = coord->getY() - m_y;
    double res = atan2(len_y, len_x);
    if (res < 0.0) {
        res += 2.0 * M_PI;
    }
    return res;
}

Coordinate *Coordinate::destination(const double bearing, const double distance) {
    double new_x = m_x + cos(bearing) * distance;
    double new_y = m_y + sin(bearing) * distance;
    return new Coordinate(new_x, new_y);
}

坐标.pxy

cdef extern from "coordinate.h":
    cdef cppclass Coordinate:
        Coordinate(const double x, const double y) except +
        Coordinate(const Coordinate *coord) except +

        void setX(const double value)
        void setY(const double value)
        double getX()
        double getY()
        double distance(Coordinate *coord)
        double bearing(Coordinate *coord)
        Coordinate *destination(const double bearing, const double distance)

cdef class PyCCoordinate:
    cdef Coordinate *thisptr
    def __cinit__(self, double x, double y):
        self.thisptr = new Coordinate(x,y)
    def __cinit__(self, Coordinate* coord):
        self.thisptr = new Coordinate(coord)
    def __dealloc__(self):
        del self.thisptr        

    def distance(self, Coordinate *coordinate):
        return self.thisptr.distance(coordinate)

    def bearing(self, Coordinate *coordinate):
        return self.thisptr.bearing(coordinate)

    def destination(self, double bearing, double distance):
        return PyCCoordinate(self.thisptr.destination(bearing, distance))

最佳答案

一个问题是 cython 语法有点误导:如果 def-函数被定义为例如(因为你的例子不是最小的我做了一个不相关的):

def twice(double d):
   return 2.0*d

然后参数不是作为 C-double 而是作为通常的 Python 对象传递给这个 (python) 函数。然而,早期绑定(bind)将导致 cython 尝试在运行时通过 __pyx_PyFloat_AsDouble 将此 Python 对象转换为 C-double,这是调用函数时的第一件事 - 这一切都发生在幕后,所以作为编码员,你有这个欺骗性的填充,你会传递一个真正的 double 函数。

但是,这种自动转换仅适用于某些类型 - 最突出的是 doubleintcdef 类等等。对于其他类型,这是不可能的,例如对于原始指针(这也意味着指向自定义 cpp 类的指针)——没有什么像 __pyx_PyFloat_AsDouble

例如

def twice(double *d):
   pass

不能被 cython 化,因为原始指针不能自动从 python 对象转换为 python 对象,这是 python 函数所必需的。

用原始指针定义一个 cdef 函数是可能的,因为它们不仅仅是简单的 C 函数,因此

cdef twice(double *d):
   pass

会编译。然而,这对 __cinit__ 没有帮助,因为它必须是一个 def 函数。

不幸的是,Cython 不会显示代码中的所有错误,只会显示它发现的第一个错误 - 否则它会显示所有带有 Coordinate *coordinatedef无效。

如何求解?基本上,您应该在签名中使用您的 cdef-wrapper-class PyCCoordinate,然后使用 thisptr-进行计算,例如:

def distance(self, PyCCoordinate coordinate):
    return self.thisptr.distance(coordinate.thisptr)

这个解决方案显然不适用于对象的构造,例如方法 destination - 您必须先构造一个 PyCCoordinate 对象,然后才能使用它!一个可能的解决方案是有一个构造函数,它将构造一个包装对象,其中 thisptrNULL,调用它并手动设置 this 指针,例如

cdef class PyCCoordinate:
    cdef Coordinate *thisptr
    def __cinit__(self):
        pass

    def __dealloc__(self):
        del self.thisptr        

    def destination(self, double bearing, double distance):
        cdef PyCCoordinate res=PyCCoordinate()
        res.thisptr=self.thisptr.destination(bearing, distance)
        return res

另一个问题:cython(还有python),不像c++,不知道重载,所以你不能定义两个不同的构造函数(也不能定义其中一个为private),所以dispatch必须手动完成,例如:

cdef class PyCCoordinate:
    cdef Coordinate *thisptr

    def __cinit__(self, x=None, y=None):
        if x is None or y is None:#default, "private" constructor
           pass     #thisptr is initialized to NULL
        else:
           self.thisptr = new Coordinate(x,y) #

    def __dealloc__(self):
        del self.thisptr        

    def destination(self, double bearing, double distance):
        cdef PyCCoordinate res=PyCCoordinate()
        res.thisptr=self.thisptr.destination(bearing, distance)
        return res

关于python - 返回对象指针 : 'Cannot convert ' Coordinate *' to Python object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49793671/

相关文章:

python - 如何制作自定义打印格式?

python - 从命令行运行 python 程序,在另一个窗口中查看结果

c++ - 为什么这个模板函数可以成功编译?

c++ - 以c++行方式读取文本文件的内容

python - Cython:编译并找不到库 Mac OSX 10.12

python - Django 模板标签循环字典变量

python - 使用 selenium 和 python 发布到 facebook 墙

c++ - 如何摆脱 C++ 中的段错误错误?

python - Cython 和 regex.h

python - 在 Cython 中优化代码的技巧