<div dir="rtl"><div style="direction:ltr"><div style="font-size:12.8px">Hey.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I have a simple goal, but struggle with it for quite a long time now. I want to parse C++ code, that's it...</div><div style="font-size:12.8px">After searching for a while, I adopted Eli's advice <a href="http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/">over here</a> and tried to work with Python and libclang.</div><div style="font-size:12.8px">Now, I must give you head-start warning: I'm a complete n00b in Python, and even more than that about Clang.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I understand that the existing code in binding\python\clang\cindex.py is not completely lined up with the last version of Python, so I started try fixing it.</div><div style="font-size:12.8px">At the beggining it was pretty easy, making cosmetics fixes for print function and AttributeError calls, but then I hit this:</div><div style="font-size:12.8px"><br></div><div dir="ltr" style="font-size:12.8px">With Python 3.5.2 64 bit, linking against libclang.dll extracted from <span style="color:rgb(36,39,41);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-size:13px;line-height:16.9px;white-space:pre-wrap;background-color:rgb(239,240,241)">LLVM-3.8.1-win64.exe </span>I keep getting the following error, trying to make an cindex.Index object:</div><div dir="ltr" style="font-size:12.8px"><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(239,240,241)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Traceback</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">most recent call </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">last</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">):</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">File</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">"test.py"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> line </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">22</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">in</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)"><module></span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
index </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> clang</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">cindex</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Index</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">create</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">()</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">File</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">"D:\Project\clang\cindex.py"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> line </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">2218</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">in</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> create
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">return</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Index</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">conf</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">lib</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">clang_<wbr>createIndex</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">excludeDecls</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">0</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">))</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">File</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">"D:\Project\clang\cindex.py"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> line </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">1971</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">in</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> __init__
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">assert</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> isinstance</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">obj</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> c_object_p</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">and</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> obj
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">AssertionError</span></code></pre>With Python 2.7 I passed this error (but failed on node.get_childern())</div><div dir="ltr" style="font-size:12.8px">I tried already on different computers, and got the same error over and over.</div>I debugged it with pdb, and found out that obj in clang\cindex.py, line 1971 is just an int, not <span style="color:rgb(48,51,54);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit;font-size:13px;background-color:rgb(239,240,241)">clang.cindex.LP_c_void_p</span> object, as it gets with Python2.7.</div><div dir="ltr">Another thing I've noticed with the debugger is, with Python 2.7 the call stack is:</div><div dir="ltr"><pre class="" style="margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(239,240,241)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit"><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">></span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> d</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">:</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">\project\clang\cindex</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">py</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">144</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">__get__</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">()-><</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">CDLL </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">'D... 21695c0>
-> return value
(Pdb) n
--Call--
> d:\project\clang\cindex.py(1970)__init__()
-> def __init__(self, obj):</span></code></pre>but with Python 3.5.2 it is:</div><div dir="ltr"><pre class="" style="margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(239,240,241)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit"><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">></span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> d</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">:</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">\project\clang\cindex</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">py</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">144</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">__get__</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">()-><</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">CDLL </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">'D:\Pro... at 0x27a2cf8>
-> return value
(Pdb) n
--Call--
> c:\program files\python35\lib\ctypes\__init__.py(357)__getattr__()
-> def __getattr__(self, name):
(Pdb) r
--Return--
> c:\program files\python35\lib\ctypes\__init__.py(362)__getattr__()-><_FuncPtr obj...000000296B458>
-> return func
(Pdb) n
--Call--
> d:\project\clang\cindex.py(1970)__init__()
-> def __init__(self, obj):</span></code></pre></div><div dir="ltr"><span style="font-size:12.8px">Meaning that in Python 3.5.2, there's always a call to the </span><span style="color:rgb(125,39,39);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-size:13px;background-color:rgb(239,240,241)">__getattr__() </span>function in <span style="font-size:13px;color:rgb(125,39,39);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit;background-color:rgb(239,240,241)">lib\ctypes\__init__.py </span>(which resides in class CDLL), while in Python 2.7 - this call doesn't exist. Have no idea why.</div><div dir="ltr"><span style="font-size:12.8px">Any help will be highly appriciated.</span></div><div style="direction:ltr"><div dir="ltr" style="font-size:12.8px"><br></div><div dir="ltr" style="font-size:12.8px">Thanks,</div><div dir="ltr" style="font-size:12.8px">Itzik</div></div></div>