https://llvm.org/bugs/show_bug.cgi?id=26394
Bug ID: 26394 Summary: libclang python bindings discard index when creating a TranslationUnit Product: clang Version: 3.7 Hardware: PC OS: Linux Status: NEW Severity: normal Priority: P Component: -New Bugs Assignee: unassignedclangb...@nondot.org Reporter: andrej.lajo...@ad-vega.si CC: llvm-bugs@lists.llvm.org Classification: Unclassified I was testing the python bindings for libclang and I noticed that the following example crashes: test.py: ---------- import clang.cindex tu = clang.cindex.TranslationUnit.from_source('test.c') tu.reparse() print tu.cursor() ---------- The file 'test.c' must exist, but can be empty. The expected result is that clang would create a translation unit and then reparse it immediately. But the actual result is this: ---------- $ python2 test.py LIBCLANG FATAL ERROR: unknown module format libclang: crash detected during reparsing Segmentation fault ---------- I believe that I know what the problem is. When the classmethod clang.cindex.TranslationUnit.from_source() is called, it creates an index and a little while later passes it to the constructor: > if index is None: > index = Index.create() > [...] > return cls(ptr, index=index) However, the constructor only checks that the index is indeed a valid Index object, but does not store it in any way (I am omitting the documentation string and empty lines for brevity): > def __init__(self, ptr, index): > assert isinstance(index, Index) > ClangObject.__init__(self, ptr) As a consequence, after TranslationUnit.from_source() terminates, the index has a reference count of zero and it is destroyed by python's garbage collector. I believe that this is not something that should happen. Indeed, if I insert self.index = index just after the assert clause, my little snippet in test.py starts to work as expected. Can somebody involved in python bindings take a look at this, please? I have only started to dabble with clang about a day ago and I am not confident enough to propose a definite solution. -- You are receiving this mail because: You are on the CC list for the bug.
_______________________________________________ llvm-bugs mailing list llvm-bugs@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs