PyLibClang is a comprehensive Python binding for libclang.
It distinguishes itself from the official clang python bindings in the following ways:
- It is a comprehensive binding, meaning it brings all libclang APIs into the Python environment. Conversely, the official binding only exposes a subset of the APIs.
- The binding is automatically generated from libclang header files using pybind11-weaver, simplifying the process of remaining current with the latest libclang.
- It is exported from C++, thereby facilitating faster performance than the official binding.
- It is directly accessible from PYPI.
At present, only Linux builds have been tested.
Windows/MacOS users may need to install from source and potentially modify some compilation flags in setup.py
to
enable successful compilation.
pip install pylibclang
# optional stubs
pip install pyblibclang-stubs
The version number adopts the format of {pylibclang_ver}{clang_ver}
, wherein pylibclang_ver
is an integer
and clang_ver
represents the version of the underlying libclang. For example, 9817.0.3
indicates that the version of
pylibclang is 98
, and the version of libclang is 17.0.3
.
There is a cindex.py
, ported from the official clang python
bindings, cindex.py
, which
serves as a wrapper around the raw C API.
Though not thoroughly tested, it should suffice for most use cases and is recommended as the initial entry point to the library.
Should you encounter any issues, please report them on Github, or attempt to rectify them yourself and submit a pull request. Typically, there are two kinds of problems you might face:
- Unresolved
cindex.py
code. In this case, updating thecindex.py
may be necessary. - Incompatible C-API call. For instance, a function might require an
int *
as both input and output, butcindex.py
only passes anint
as input. Owing to the constraints of Pybind11, this value will never be updated. In such cases, adding a new binding code inc_src/binding.cpp
, rebuilding the project, and then calling it fromcindex.py
may be required.
pylibclang._C
is the pybind11 binding for all the C APIs in libclang. If anything is missing in cindex.py
, the raw C
API can always be directly utilized. For instance, cindex.py
does not expose clang_getClangVersion
, but you can
still invoke it from pylibclang._C
:
import pylibclang._C as C
print(C.clang_getCString(C.clang_getClangVersion()))