I have a simple goal, but struggle with it for quite a long time now. I want to parse C++ code, that’s it…
After searching for a while, I adopted Eli’s advice over here and tried to work with Python and libclang.
Now, I must give you head-start warning: I’m a complete n00b in Python, and even more than that about Clang.
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.
At the beggining it was pretty easy, making cosmetics fixes for print function and AttributeError calls, but then I hit this:
With Python 3.5.2 64 bit, linking against libclang.dll extracted from LLVM-3.8.1-win64.exe I keep getting the following error, trying to make an cindex.Index object:
Traceback (most recent call last): File "test.py", line 22, in <module> index = clang.cindex.Index.create() File "D:\Project\clang\cindex.py", line 2218, in create return Index(conf.lib.clang_createIndex(excludeDecls, 0)) File "D:\Project\clang\cindex.py", line 1971, in __init__ assert isinstance(obj, c_object_p) and obj AssertionError
With Python 2.7 I passed this error (but failed on node.get_childern())
I tried already on different computers, and got the same error over and over.
I debugged it with pdb, and found out that obj in clang\cindex.py, line 1971 is just an int, not clang.cindex.LP_c_void_p object, as it gets with Python2.7.
Another thing I’ve noticed with the debugger is, with Python 2.7 the call stack is:
> d:\project\clang\cindex.py(144)__get__()-><CDLL 'D... 21695c0> -> return value (Pdb) n --Call-- > d:\project\clang\cindex.py(1970)__init__() -> def __init__(self, obj):
but with Python 3.5.2 it is:
> d:\project\clang\cindex.py(144)__get__()-><CDLL '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):
Meaning that in Python 3.5.2, there’s always a call to the getattr() function in lib\ctypes_init_.py (which resides in class CDLL), while in Python 2.7 - this call doesn’t exist. Have no idea why.
Any help will be highly appriciated.