<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - libclang python bindings discard index when creating a TranslationUnit"
href="https://llvm.org/bugs/show_bug.cgi?id=26394">26394</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>libclang python bindings discard index when creating a TranslationUnit
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.7
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>andrej.lajovic@ad-vega.si
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>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:
<span class="quote">> if index is None:
> index = Index.create()
> [...]
> return cls(ptr, index=index)</span >
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):
<span class="quote">> def __init__(self, ptr, index):
> assert isinstance(index, Index)
> ClangObject.__init__(self, ptr)</span >
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.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>