An earlier article demonstrated creating an extension for Python. This article focuses on embedding, which is a second way of integrating C and Python.
Embedding in Python is where C, or C++, or any program for that matter, is given direct access to the Python interpreter. This gives the program the power to load and execute Python scripts and services. In C, this enables you load in Python-specific modules, call Python functions, and access Python objects.
Embedding is powered by Python’s API, which can be used by including Python.h. This header contains all the functions, types, and macro definitions needed to use the API. It is fairly simple to initialize Python in C/C++ once the Python header file is included:
#include "Python.h" int main() { Py_Initialize(); PyRun_SimpleFile("<filename>"); Py_Finalize(); return(); }
Py_Initialize is the basic initialization function and allocates resources for the interpreter to start using the API. In particular, it initializes and creates Python’s sys, exceptions, _builtin_, and _main_modules. Py_Initialize searches for modules, assuming that the Python library is in a fixed location (details that may need to be altered, depending on the operating system, by setting Python’s path and home). After initialization and running the code, Py_Finalize releases the internal resources and shuts down the interpreter.
The Pyrun_SimpleFile is simply one of the very high-level API functions that reads the given file from a pointer (FILE *) and executes the commands stored there. Python’s high-level API functions are basically just for executing given Python source, not interacting with it in any significant way. Other high-level functions include the following:
Py_CompileString() Py_eval_input Py_file_input Py_Main() PyParser_SimpleParseString() PyParser_SimpleParseFile() PyRun_AnyFile() PyRun_SimpleString() PyRun_SimpleFile() Py_single_input PyRun_InteractiveOne() PyRun_InteractiveLoop() PyRun_String() PyRun_File()
The high-level tools really just scratch the surface, and Python’s API allows memory management, creating objects, threading, and exception handling, to name a few. Other commonly used commands you will find include PyImport_ImportModule() for importing and initializing entire Python modules, PyObject_GetAttrString() for accessing a given modules attributes, and PyObject_SetAttrString() for assigning values to variables within modules.
The API reference manual by Guido is of course very well organized and documented, and the place you should research before using this powerful Python feature.
References/Resources
- Guido van Rossum’s Python/C API Reference Manual:
http://www.python.org/doc/2.2.3/api/api.html - The Python Homepage:
http://www.python.org/ - Python.org: Extending and Embedding the Python Interpreter:
http://www.python.org/doc/current/ext/ext.html - Guido van Rossum’s tutorial on extending and embedding Python:
http://www.python.org/doc/current/ext/ext.html - Activestate’s Python Cookbook has a Python extension and embedding section:
http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Extending - Richard Baldwin wrote a series of Python tutorials for Earthweb. Lesson 1:
http://18.220.208.18/open/article.php/625901 - SWIG at Sourceforge:
http://swig.sourceforge.net/ - List of SWIG’s compatable platforms and languages:
http://www.swig.org/compat.html - SWIG’s license:
http://swig.sourceforge.net/copyright.html - A great reference is from swig.org’s documentation section, a paper called “Interfacing C/C++ and Python with SWIG,” which was presented at the 7th international Python Conference:
http://www.swig.org/papers/PyTutorial98/PyTutorial98.pdf - Boost.Python by David Abrahams:
http://www.python.org/cgi-bin/moinmoin/boost_2epython