April’s “API Spy” introduced Python’s C API and showed how a Python interpreter can be embedded in a C program. For many tasks where you need to run a Python script from within C code, last month’s technique is sufficient. However, as your C programs and Python scripts evolve, you may want or need more advanced interaction between the two languages.
April’s “API Spy” introduced Python’s C API and showed how a Python interpreter can be embedded in a C program. For many tasks where you need to run a Python script from within C code, last month’s technique is sufficient. However, as your C programs and Python scripts evolve, you may want or need more advanced interaction between the two languages.
For example, in addition to passing data to your Python scripts, you may also want to expose your C libraries to Python, so that Python can call back to your application, creating a true, two-way channel of communication between your C and Python code.
In fact, exposing C libraries to the Python interpreter is eminently practical:
* C code is often organized into one or more “libraries” of functions that are useful to an entire application. Exposing these libraries to embedded scripting languages allows an entire application to be scripted.
* Many existing, third-party C libraries accomplish certain tasks that no Python library can. So, instead of duplicating the implemention of those C functions in Python, it’s often faster (faster to develop and faster to execute) to “wrap” the existing C library.
* Existing Python code is often optimized by re-implementing it as a C library that’s then wrapped with a Python module. This provides the best of both worlds — rapid prototyping in Python and optimized performance in C — as needs dictate.
This month, the “API Spy” shows you…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: