1

我正在使用 Cython 来包装一个 C++ 库,在其中我使用(uintptr_t)(void *) 强制转换将指针传递给 python 调用者并作为句柄返回。在一个这样的场景中 - 我将一个转换指针作为 Python 整数传递给另一个 Cython 函数。在声明指针的原始函数中,反向转换为(Class *)(void *)成功生成原始指针值 [在 C++ 中验证]。而在另一个使用句柄的 Cython 函数中,反向转换给出了一些其他指针值,导致崩溃 [在 C++ 中验证]。对象大小的变化是否会影响从uintptr_t(Class *)(void *)的反向转换?或者对这种演员表和反向演员表有任何其他要求。

Class A:
    @property
    def cppobj(self):
        """
        - return pointer to C++ Object
        """
        cdef uintptr_t ptr = <uintptr_t><void *> self._obj
        # call to printptr C++ method
        # argument - <cpp.A *><void *> ptr
        # prints: 0x8805508
        return <uintptr_t><void *> self._obj

class B:
    def useA(self):
        # call to printptr C++ method
        # argument - <cpp.A *><void *> A.cppobj
        # prints: 0x880b718
4

0 回答 0