简体   繁体   English

如何将C ++指针传递给其他cython对象

[英]How to pass C++ pointers to to other cython objects

I still am having a very hard time figuring this out. 我仍然很难搞清楚这一点。 Take this simple scenario.. in a file named PyFoo.pyx 在名为PyFoo.pyx的文件中使用这个简单的场景..

cdef extern from "Foo.h" namespace "Foo":  
    cdef cppclass C_FOO:
        ...........

cdef class PythonFoo:
    C_FOO* pythonFoo
    def __cinit__(self):
        self.pythonFoo=new CFOO()

I do have a PyFoo.PXD also, that simply does this for a forward decl so other classes can see it 我也有一个PyFoo.PXD,它只是为前向decl做这个,所以其他类可以看到它

cdef class PythonFoo:
    C_FOO * pythonFoo

Now I have another class (in PyBar.pyx) that needs PythonFoo.pythonFoo as a member 现在我有另一个类(在PyBar.pyx中)需要PythonFoo.pythonFoo作为成员

cdef extern from "Bar.h" namespace "Bar":  
    cdef cppclass C_Bar:
         ThisFunctionNeeds(C_FOO*)
cimport PythonFoo        
cdef class PythonBar:
    C_Bar* pythonBar
    def __cinit__(self):
        self.pythonBar=new C_Bar()
    def SomeFunction(self,PythonFoo):
        ThisFunctionNeeds(PythonFoo.pythonFoo)   

No matter how I try to cimport it, I get "PythonFoo not defined" (Because there is no init in the pxd file I guess) or I have to manually include the Foo.pyx, making this now a member of Bar. 无论我如何尝试cimport它,我得到“PythonFoo未定义”(因为我猜pxd文件中没有init )或者我必须手动包含Foo.pyx,使它现在成为Bar的成员。

Can someone please show me (with an example please) how this is supposed to work? 有人可以告诉我(请举例说明)这应该如何工作?

Thank you. 谢谢。

EDIT: PythonFoo and PythonBar are in separate modules in the package. 编辑:PythonFoo和PythonBar在包中的单独模块中。

In a Python Tester, I try to use this 在Python测试器中,我尝试使用它

import PythonFoo
import PythonBar

foo=PythonFoo()
bar=PythonBar()
bar.SomeFunction(foo)

Here is where I get the "No module named 'PyFoo' 这是我得到“没有名为'PyFoo'的模块的地方

I was able to find why it would not work... The actual directory of the module was not placed, as a matter of fact, the file was placed at a higher directory from the build directory. 我能够找到为什么它不起作用...模块的实际目录没有放置,事实上,文件放在构建目录的更高目录中。

I fixed it with a simple 我用简单的方法修好了它

sys.path.append('package_directory') sys.path.append( 'package_directory')

EDIT: I also found out why this was... I had an __init__.py in the same directory as my setup.py file, so the compiler thought that directory was part of the package. 编辑:我也发现了为什么这是...我在我的setup.py文件所在的目录中有__init__.py ,所以编译器认为该目录是包的一部分。 I deleted the __init__.py , and I no longer needed to append the directory. 我删除了__init__.py ,我不再需要附加目录了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM