[Tutor] How to create a python extension module from a shared library?

Etienne Robillard tkadm30 at yandex.com
Wed Dec 27 18:01:06 EST 2017

Hi all,

I want to build a CPython extension module for the libuwsgi.so shared 
library included in uWSGI. My objective is to allow unconditional access 
to this shared library using the standard Python interpreter, for 
introspection purpose and extending uWSGI.

I have investigated several ways to do this:

1. ctypes

Probably the most straightforward to load the shared library

 >>> from ctypes import CDLL

 >>> lib = CDLL('./libuwsgi.so')

However, this method does not properly reflect C functions and variables 
to their respective Python attributes...

2. CFFI/pycparser

Next I tried to parse the uwsgi.h file with CFFI and pycparser:

 >>> from cffi import FFI

 >>> ffi = FFI()

 >>> lib = ffi.cdef(open('./uwsgi.h').read())

However, since directives are not supported in CFFI, it's not possible 
to parse the header file.

3. CFFI/clang

For this experiment, I wanted to parse the C header file to generate a 
Abstract Syntax Tree (AST) using clang:

 >>> from pycparser import parse_file

 >>> ast = parse_file('./uwsgi.h', use_cpp=True, cpp_path='clang')

 >>> ast.show()

FileAST: (at None)

Ideally, it would be really cool if CFFI could parse a C header and 
generate a AST with libclang. Another possibility I haven't explored yet 
is to use cppyy.

So, could you please advise on the most robust approach to reflect a 
shared library into a Python extension module?



Etienne Robillard
tkadm30 at yandex.com

More information about the Tutor mailing list