Problem with Python bindings generation (in case of inheritance)

asked 2015-06-05 02:47:07 -0600

Mattpiz gravatar image

updated 2015-06-05 03:38:48 -0600

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

edit retag flag offensive close merge delete

Comments

1

Hmm nice find! I would suggest you to open up a bug issue about this. I know that similar behaviour exists when compiling C++ modules in the incorrect order and thus module building sequences are defined there manually in each CMAKE file. Could you not solve it by adding your two libs first manually in that CMAKE file?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-05 02:53:12 -0600 )edit
1

I don't understand, you suggest using something like an if statement and in case ${name} correspond to my module, I manually give the list of headers ?

Mattpiz gravatar imageMattpiz ( 2015-06-05 03:46:29 -0600 )edit

Yeah, however not really sure how to do that myself :D

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-05 04:53:50 -0600 )edit