Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code to parse and inspect c++

Tags:

c++

python

Is there a library for Python that will allow me to parse c++ code?

For example, let's say I want to parse some c++ code and find the names of all classes and their member functions/variables.

I can think of a few ways to hack it together using regular expressions, but if there is an existing library it would be more helpful.

like image 389
Mike Avatar asked Mar 06 '10 18:03

Mike


People also ask

Can Python interact with C?

In general, already-written C code will require no modifications to be used by Python. The only work we need to do to integrate C code in Python is on Python's side. The steps for interfacing Python with C using Ctypes.

How do you inspect code in Python?

We use the getsource() method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.

How do you inspect a class in Python?

isclass(): The isclass() method returns True if that object is a class or false otherwise. When it is combined with the getmembers() functions it shows the class and its type. It is used to inspect live classes.

How do I connect to Python and C?

There a number of ways to do this. The rawest, simplest way is to use the Python C API and write a wrapper for your C library which can be called from Python. This ties your module to CPython. The second way is to use ctypes which is an FFI for Python that allows you to load and call functions in C libraries directly.


1 Answers

In the past I've used for such purposes gccxml (a C++ parser that emits easily-parseable XML) -- I hacked up my own Python interfaces to it, but now there's a pygccxml which should package that up nicely for you.

like image 198
Alex Martelli Avatar answered Sep 22 '22 07:09

Alex Martelli