ppembed: A High-Level Embedding API
But don’t do that . As you can probably tell from the last example, embedded-mode integration code can very quickly become as complicated as extending code for nontrivial use. Today, no automation solution solves the embedding problem as well as SWIG addresses extending. Because embedding does not impose the kind of structure that extension modules and types provide, it’s much more of an open-ended problem; what automates one embedding strategy might be completely useless in another.
With a little up-front work, though, you can still automate common embedding tasks by wrapping up calls in a higher-level API. These APIs could handle things such as error detection, reference counts, data conversions, and so on. One such API, ppembed, is available on this book’s CD (see http://examples.oreilly.com/python2). It merely combines existing tools in Python’s standard C API to provide a set of easier-to-use calls for running Python programs from C.
Running Objects with ppembed
Example 20-15 demonstrates
how to recode objects-err-low.c
by linking
ppembed’s library files with your program.
Example 20-15. PP2E\Integrate\Embed\ApiClients\object-api.c
#include <stdio.h> #include "ppembed.h" main ( ) { /* with ppembed high-level api */ int failflag; PyObject *pinst; char *arg1="sir", *arg2="robin", *cstr; failflag = PP_Run_Function("module", "klass", "O", &pinst, "( )") || PP_Run_Method(pinst, "method", "s", &cstr, "(ss)", arg1, arg2); printf("%s\n", (!failflag) ...
Get Programming Python, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.