Dag Sverre Seljebotn <da...@...> writes:
> Robert Bradshaw wrote:
> > On Jun 23, 2009, at 1:40 AM, Jon Olav Vik wrote:
> >> Why isn't type CythonNVector recognized outside of cynvector.pyx? A  
> >> similar error results if I try "from cynvector cimport CythonNVector"; 
> >> then I get "Name 'CythonNVector' not declared in module 'cynvector'".
> > 
> > What does your cynvector.pxd look like? You probably need to declare  
> > CythonNVector there so other modules can cimport it.
> 
> Actually there probably is no -- it says import, not cimport.
> 
> So you need a cynvector.pxd as well, and cimport in in addition to the 
> normal import.
> 
> (There's a wishlist item for automatically deducing this from pyx files, 
> but it isn't there yet.)
> 
> http://docs.cython.org/docs/sharing_declarations.html

Thanks, I've got it working now. (I did use a pxd file for the Sundials 
declarations, but didn't realize that I needed one for my Cython module too.)

== test_cynvector.pyx ==
from cynvector cimport *
from sundials cimport *
cdef CythonNVector v = CythonNVector(2) # allocates empty N_Vector of length 2
cdef N_Vector p = v.nvector # alias to an attribute of the CythonNVector
print v.toarray() # uninitialized memory, but a success nevertheless...

As it turns out, I only needed the cimport for my example. To both import and 
cimport the module, however, it seems that I cannot use *:

from cynvector import *
from cynvector cimport * # TypeError: Cannot overwrite C type CythonNVector

Importing and then cimporting the module under the same name works fine, though:

import cynvector
cimport cynvector
cimport sundials
cdef cynvector.CythonNVector v = cynvector.CythonNVector(2)
cdef sundials.N_Vector p = v.nvector

== cynvector.pxd ==
"""Declarations for Cython bridge between Numpy array and Sundials N_Vector"""
from sundials cimport * # sundials.pxd defines N_Vector
cdef class CythonNVector:
    cdef N_Vector nvector
    cpdef toarray(self)

== cynvector.pyx ==
cimport numpy as np
from sundials cimport * # sundials.pxd declares N_Vector and friends

cdef extern from "pyarray_set_base.h":
    void PyArray_Set_BASE(np.ndarray arr, object obj)

cdef extern from "numpy/arrayobject.h":
    void import_array()
    object PyArray_SimpleNewFromData(int nd, np.npy_intp* dims, 
        int typenum, void* data)

import_array()

cdef class CythonNVector:
    cdef N_Vector nvector
    def __init__(self, length):
        self.nvector = N_VNew_Serial(length)

    def __dealloc__(self):
        N_VDestroy_Serial(self.nvector)

    cpdef toarray(self):
        cdef np.npy_intp* shape =  <np.npy_intp *>(
            &(<N_VectorContent_Serial>(self.nvector).content).length)
        assert sizeof(realtype) == sizeof(double)
        cdef np.ndarray result = PyArray_SimpleNewFromData(1, shape, 
            np.NPY_DOUBLE, 
            <void*>((<N_VectorContent_Serial>(self.nvector).content).data))
        PyArray_Set_BASE(result, self)
        return result

== setup_test_cynvector.py ==
"""Compile and test with
rm -rf build test_cynvector.so test_cynvector.c && \
python setup_test_cynvector.py build_ext --inplace && \
python -c "import test_cynvector"
"""

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [
        Extension("test_cynvector",
            ["test_cynvector.pyx"],
            include_dirs=['/site/VERSIONS/sundials-2.3.0/include', 
np.get_include ()],
            library_dirs=['/site/VERSIONS/sundials-2.3.0/lib'],
            libraries=['sundials_cvode', 'sundials_nvecserial']),
    ]
)


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to