is 'handle()' a method that is called internally when passing extension
types as parameters?

I'm exposing a C function to Python as follows:

def cvSomeFunction(CvArr image):  #image should be able to be CvArr IplImage
or CvMat
     c_lib.cvSomeFunction(image.thisptr)    # C func prototype = void
cvSomeFunction(CvArr* image) and image can be CvArr* IplImage* or CvMat*

I'm not sure I understand if the classes you show will do that.

For example, my IplImage class is like this:

cdef class IplImage:
      cdef c_lib.IplImage* thisptr

      <----snip---->

and i work with it like this:

cdef c_lib.IplImage* img = cvFunctionThatReturnsPtr()
cdef py_lib.IplImage pyimg = py_lib.IplImage()
pyimg.thisptr = img

etc....

now, if I rewrite my python function wrapper above like this, it works, but
only when i pass the python type IplImage
 def cvSomeFunction(py_lib.IplImage image):
     c_lib.cvSomeFunction(image.thisptr)  # C prototype is still --- void
cvSomeFunction(CvArr* image)


I understand why its not working like that, I'm just hoping there is some
way around it that doesnt require a bunch of type checking in Python. I'm
trying to keep the wrapper as thin as possible.

I appreciate all your help Lisandro!

Chris




On Thu, May 21, 2009 at 9:18 PM, Lisandro Dalcin <[email protected]> wrote:

> cdef class CvArr:
>   def c_CvArr* handle(self):
>       raise NotImplementedError
>
> cdef class IplImage(CvArr):
>    cdef c_IplImage* image
>    def c_CvArr* handle(self):
>       return <c_CvArr*>self.image
>
> cdef class CvMat(CvArr):
>    cdef c_CvMat *cvmat
>    def c_CvArr* handle(self):
>       return <c_CvArr*>self.cvmat
>
>
> Mmm... just a use case for "cdef properties" managed by calling
> get/setters in the vtable?
>
>
> On Thu, May 21, 2009 at 9:16 PM, Chris Colbert <[email protected]>
> wrote:
> > the problem is that the C library has several functions that will accept
> one
> > of many types of arrays.
> >
> > The function parameter type is CvArr whose definition is (in C):
> >
> > typedef void CvArr;
> >
> > and the functions are declared as accepting CvArr* types as arguments.
> >
> > two of the types I am wrapping are IplImage and CvMat both of which can
> be
> > passed as CvArr*.
> >
> > I would like to make python callable functions that accept either
> instance
> > of IplImage or CvMat (which I have created as extension types).
> >
> > Unfortunately, i haven't yet been able to figure it out through various
> > tinkering with subclassing, casting, or void pointers....
> >
> >
> > I would greatly appreciate any insight one could give..
> >
> > Thanks and Cheers!
> >
> > Chris
> >
> > On Thu, May 21, 2009 at 7:05 PM, Chris Colbert <[email protected]>
> wrote:
> >>
> >> I have certain functions that should accept types of IplImage or CvMat
> >>
> >> I have the two types declared as inheriting from a generic CvArr as such
> >>
> >> cdef class CvArr:
> >>        cdef void* thisptr
> >>
> >> cdef class IplImage(CvArr):
> >>       .....
> >>
> >> cdef class CvMat(CvArr):
> >>       ....
> >>
> >>
> >> and a function declared like this:
> >>
> >> def cvSmooth(CvArr src, .....):
> >>       c_lib.cvSmooth(src.thisptr, ....)
> >>
> >> when compiling and calling that function with instances of IplImage, the
> >> Cython compiler complains that src is not a CvArr.
> >>
> >> Is there a way around this?
> >>
> >> Chris
> >
> >
> > _______________________________________________
> > Cython-dev mailing list
> > [email protected]
> > http://codespeak.net/mailman/listinfo/cython-dev
> >
> >
>
>
>
> --
> Lisandro Dalcín
> ---------------
> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
> Tel/Fax: +54-(0)342-451.1594
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to