Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem with Python bindings generation (in case of inheritance)

I am currently creating a module for OpenCV 3.0 and trying to make it work in Python. I think I found what seems to be a problem in how OpenCV create Python bindings but I don't know what is the best way to overcome the problem.

My problem is the following : I have 2 classes in my module folder (headers in modules/xvideo/include/opencv2/xvideo) which are : parentClass.hpp and childClass.hpp. Of course ChildClass inheriting from ParentClass (names are just for the example). But the script gen2.py (located in modules/python/src2) is trying to create ChildClass before ParentClass and end up with the error :

Generator error: unable to resolve base xvideo_ParentClass for xvideo_ChildClass

This seems to be because classes are generated in the order given from the variable srcfiles in the gen function of the script :

def gen(self, srcfiles, output_path):
    self.clear()
    self.parser = hdr_parser.CppHeaderParser()

    # step 1: scan the headers and build more descriptive maps of classes, consts, functions
    #print(srcfiles) ##matthieu
    for hdr in srcfiles:
        decls = self.parser.parse(hdr)
        if len(decls) == 0:
            #print(hdr + '0') ##matthieu
            continue
        #else: ##matthieu
            #print(hdr) ##matthieu
        self.code_include.write( '#include "{0}"\n'.format(hdr[hdr.rindex('opencv2/'):]) )
        for decl in decls:
            name = decl[0]
            if name.startswith("struct") or name.startswith("class"):
                # class/struct
                p = name.find(" ")
                stype = name[:p]
                name = name[p+1:].strip()
                self.add_class(stype, name, decl)
    ...

So I went upstream to find how this list is created. It appears, that list is the concatenation of sublists of each module, where each sublist is created by the CMake file cmake/OpenCVModule.cmake at line 626 :

file(GLOB lib_hdrs
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
)

But one might know that file(GLOB) has an indeterministic behaviour. And that's why my child class is before my parent class in that list, and ultimately raises an error.

So what I am asking is if someone knows the best way to bypass that problem ? Is there a way to specify ourself the list ? Or did I simply forgot to write something ? please enlighten me ^^

Problem with Python bindings generation (in case of inheritance)

I am currently creating a module for OpenCV 3.0 and trying to make it work in Python. I think I found what seems to be a problem in how OpenCV create Python bindings but I don't know what is the best way to overcome the problem.

My problem is the following : I have 2 classes in my module folder (headers in modules/xvideo/include/opencv2/xvideo) which are : parentClass.hpp and childClass.hpp. Of course ChildClass inheriting from ParentClass (names are just for the example). But the script gen2.py (located in modules/python/src2) is trying to create ChildClass before ParentClass and end up with the error :

Generator error: unable to resolve base xvideo_ParentClass for xvideo_ChildClass

This seems to be because classes are generated in the order given from the variable srcfiles in the gen function of the script :

def gen(self, srcfiles, output_path):
    self.clear()
    self.parser = hdr_parser.CppHeaderParser()

    # step 1: scan the headers and build more descriptive maps of classes, consts, functions
    #print(srcfiles) ##matthieu
    for hdr in srcfiles:
        decls = self.parser.parse(hdr)
        if len(decls) == 0:
            #print(hdr + '0') ##matthieu
            continue
        #else: ##matthieu
            #print(hdr) ##matthieu
        self.code_include.write( '#include "{0}"\n'.format(hdr[hdr.rindex('opencv2/'):]) )
        for decl in decls:
            name = decl[0]
            if name.startswith("struct") or name.startswith("class"):
                # class/struct
                p = name.find(" ")
                stype = name[:p]
                name = name[p+1:].strip()
                self.add_class(stype, name, decl)
    ...

So I went upstream to find how this list is created. It appears, that list is the concatenation of sublists of each module, where each sublist is created by the CMake file cmake/OpenCVModule.cmake at line 626 :

file(GLOB lib_hdrs
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
    "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
)

But one might know that file(GLOB) has an indeterministic behaviour. And that's why my child class is before my parent class in that list, and ultimately raises an error.

So what I am asking is if someone knows the best way to bypass that problem ? Is there a way to specify ourself the list ? Or did I simply forgot to write something ? please enlighten me ^^

########## EDIT ##########

On your advice @StevenPuttemans I created an issue for it : http://code.opencv.org/issues/4384