Yes, you can create built-in modules containing functions, variables, exceptions
and even new types in C. This is explained in the document
Extending and Embedding the Python Interpreter.
Most intermediate or advanced Python books will also cover this topic.
Yes, using the C compatibility features found in C++. Place extern "C" {
... } around the Python include files and put extern "C" before each
function that is going to be called by the Python interpreter. Global or static
C++ objects with constructors are probably not a good idea.
There are a number of alternatives to writing your own C extensions, depending
on what you’re trying to do.
Cython and its relative Pyrex are compilers
that accept a slightly modified form of Python and generate the corresponding
C code. Cython and Pyrex make it possible to write an extension without having
to learn Python’s C API.
If you need to interface to some C or C++ library for which no Python extension
currently exists, you can try wrapping the library’s data types and functions
with a tool such as SWIG. SIP, CXX Boost, or Weave are also
alternatives for wrapping C++ libraries.
The highest-level function to do this is PyRun_SimpleString() which takes
a single string argument to be executed in the context of the module
__main__ and returns 0 for success and -1 when an exception occurred
(including SyntaxError). If you want more control, use
PyRun_String(); see the source for PyRun_SimpleString() in
Python/pythonrun.c.
Call the function PyRun_String() from the previous question with the
start symbol Py_eval_input; it parses an expression, evaluates it and
returns its value.
The PyObject_CallMethod() function can be used to call an arbitrary
method of an object. The parameters are the object, the name of the method to
call, a format string like that used with Py_BuildValue(), and the
argument values:
PyObject *
PyObject_CallMethod(PyObject *object, const char *method_name,
const char *arg_format, ...);
This works for any object that has methods – whether built-in or user-defined.
You are responsible for eventually Py_DECREF()’ing the return value.
To call, e.g., a file object’s “seek” method with arguments 10, 0 (assuming the
file object pointer is “f”):
res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0);
if (res == NULL) {
... an exception occurred ...
}
else {
Py_DECREF(res);
}
Note that since PyObject_CallObject() always wants a tuple for the
argument list, to call a function without arguments, pass “()” for the format,
and to call a function with one argument, surround the argument in parentheses,
e.g. “(i)”.
In Python code, define an object that supports the write() method. Assign
this object to sys.stdout and sys.stderr. Call print_error, or
just allow the standard traceback mechanism to work. Then, the output will go
wherever your write() method sends it.
The easiest way to do this is to use the io.StringIO class:
>>> import io, sys
>>> sys.stdout = io.StringIO()
>>> print('foo')
>>> print('hello world!')
>>> sys.stderr.write(sys.stdout.getvalue())
foo
hello world!
A custom object to do the same would look like this:
>>> import io, sys
>>> class StdoutCatcher(io.TextIOBase):
... def __init__(self):
... self.data = []
... def write(self, stuff):
... self.data.append(stuff)
...
>>> import sys
>>> sys.stdout = StdoutCatcher()
>>> print('foo')
>>> print('hello world!')
>>> sys.stderr.write(''.join(sys.stdout.data))
foo
hello world!
You can get a pointer to the module object as follows:
module = PyImport_ImportModule("<modulename>");
If the module hasn’t been imported yet (i.e. it is not yet present in
sys.modules), this initializes the module; otherwise it simply returns
the value of sys.modules["<modulename>"]. Note that it doesn’t enter the
module into any namespace – it only ensures it has been initialized and is
stored in sys.modules.
You can then access the module’s attributes (i.e. any name defined in the
module) as follows:
attr = PyObject_GetAttrString(module, "<attrname>");
Calling PyObject_SetAttrString() to assign to variables in the module
also works.
Depending on your requirements, there are many approaches. To do this manually,
begin by reading the “Extending and Embedding” document. Realize that for the Python run-time system, there isn’t a
whole lot of difference between C and C++ – so the strategy of building a new
Python type around a C structure (pointer) type will also work for C++ objects.
For C++ libraries, see Writing C is hard; are there any alternatives?.
Setup must end in a newline, if there is no newline there, the build process
fails. (Fixing this requires some ugly shell script hackery, and this bug is so
minor that it doesn’t seem worth the effort.)
When using GDB with dynamically loaded extensions, you can’t set a breakpoint in
your extension until your extension is loaded.
In your .gdbinit file (or interactively), add the command:
br _PyImport_LoadDynamicModule
Then, when you run GDB:
$ gdb /local/bin/python
gdb) run myscript.py
gdb) continue # repeat until your extension is loaded
gdb) finish # so that your extension is loaded
gdb) br myfunction.c:50
gdb) continue
Most packaged versions of Python don’t include the
/usr/lib/python2.x/config/ directory, which contains various files
required for compiling Python extensions.
For Red Hat, install the python-devel RPM to get the necessary files.
For Debian, run apt-get install python-dev.
To dynamically load g++ extension modules, you must recompile Python, relink it
using g++ (change LINKCC in the Python Modules Makefile), and link your
extension module using g++ (e.g., g++ -shared -o mymodule.so mymodule.o).