00001 /************************************************************************* 00002 * PPEMBED, VERSION 2.0 00003 * AN ENHANCED PYTHON EMBEDDED-CALL INTERFACE 00004 * 00005 * Wraps Python's run-time embedding API functions for easy use. 00006 * Most utilities assume the call is qualified by an enclosing module 00007 * (namespace). The module can be a file-name reference or a dummy module 00008 * created to provide a namespace for file-less strings. These routines 00009 * automate debugging, module (re)loading, input/output conversions, etc. 00010 * 00011 * Python is automatically initialized when the first API call occurs. 00012 * Input/output conversions use the standard Python conversion format 00013 * codes (described in the C API manual). Errors are flagged as either 00014 * a -1 int, or a NULL pointer result. Exported names use a PP_ prefix 00015 * to minimize clashes; names in the built-in Python API use Py prefixes 00016 * instead (alas, there is no "import" equivalent in C, just "from*"). 00017 * Also note that the varargs code here may not be portable to certain 00018 * C compilers; to do it portably, see the text or file 'vararg.txt' 00019 * here, or search for string STDARG in Python's source code files. 00020 * 00021 * New in this version/edition: names now have a PP_ prefix, files 00022 * renamed, compiles to a single .a file, fixed pdb retval bug for 00023 * strings, and char* results returned by the "s" convert code now 00024 * point to new char arrays which the caller should free() when no 00025 * longer needed (this was a potential bug in prior version). Also 00026 * added new API interfaces for fetching exception info after errors, 00027 * precompiling code strings to byte code, and calling simple objects. 00028 * 00029 * Also fully supports Python 1.5 module package imports: module names 00030 * in this API can take the form "package.package.[...].module", where 00031 * Python maps the package names to a nested directories path in your 00032 * file system hierarchy; package dirs all contain __init__.py files, 00033 * and the leftmost one is in a directory found on PYTHONPATH. This 00034 * API's dynamic reload feature also works for modules in packages; 00035 * Python stores the full path name in the sys.modules dictionary. 00036 * 00037 * Caveats: there is no support for advanced things like threading or 00038 * restricted execution mode here, but such things may be added with 00039 * extra Python API calls external to this API (see the Python/C API 00040 * manual for C-level threading calls; see modules rexec and bastion 00041 * in the library manual for restricted mode details). For threading, 00042 * you may also be able to get by with C threads and distinct Python 00043 * namespaces per Python code segments, or Python language threads 00044 * started by Python code run from C (see the Python thread module). 00045 * 00046 * Note that Python can only reload Python modules, not C extensions, 00047 * but it's okay to leave the dynamic reload flag on even if you might 00048 * access dynamically-loaded C extension modules--in 1.5.2, Python 00049 * simply resets C extension modules to their initial attribute state 00050 * when reloaded, but doesn't actually reload the C extension file. 00051 *************************************************************************/ 00052 00053 /* 00054 PPEMBED, VERSION 2.0 00055 AN ENHANCED PYTHON EMBEDDED-CALL INTERFACE 00056 00057 Copyright 1996-2000, by Mark Lutz, and O'Reilly and Associates. 00058 Permission to use, copy, modify, and distribute this software 00059 for any purpose and without fee is hereby granted. This software 00060 is provided on an as is basis, without warranties of any kind. 00061 */ 00062 00063 #ifndef PPEMBED_H 00064 #define PPEMBED_H 00065 00066 #ifdef __cplusplus 00067 extern "C" { /* a C library, but callable from C++ */ 00068 #endif 00069 00070 #include <stdio.h> 00071 // Python 00072 #if defined (_POSIX_C_SOURCE) 00073 # undef _POSIX_C_SOURCE 00074 #endif // (re-)defined in pyconfig.h 00075 #include <Python.h> 00076 00077 extern int PP_RELOAD; /* 1=reload py modules when attributes referenced */ 00078 extern int PP_DEBUG; /* 1=start debugger when string/function/member run */ 00079 00080 typedef enum { 00081 PP_EXPRESSION, /* which kind of code-string */ 00082 PP_STATEMENT /* expressions and statements differ */ 00083 } PPStringModes; 00084 00085 00086 /***************************************************/ 00087 /* ppembed-modules.c: load,access module objects */ 00088 /***************************************************/ 00089 00090 extern const char *PP_Init(const char *modname); 00091 extern int PP_Make_Dummy_Module(const char *modname); 00092 extern PyObject *PP_Load_Module(const char *modname); 00093 extern PyObject *PP_Load_Attribute(const char *modname, const char *attrname); 00094 extern int PP_Run_Command_Line(const char *prompt); 00095 00096 00097 /**********************************************************/ 00098 /* ppembed-globals.c: read,write module-level variables */ 00099 /**********************************************************/ 00100 00101 extern int 00102 PP_Convert_Result(PyObject *presult, const char *resFormat, void *resTarget); 00103 00104 extern int 00105 PP_Get_Global(const char *modname, const char *varname, const char *resfmt, void *cresult); 00106 00107 extern int 00108 PP_Set_Global(const char *modname, const char *varname, const char *valfmt, ... /*val*/); 00109 00110 00111 /***************************************************/ 00112 /* ppembed-strings.c: run strings of Python code */ 00113 /***************************************************/ 00114 00115 extern int /* run C string of code */ 00116 PP_Run_Codestr(PPStringModes mode, /* code=expr or stmt? */ 00117 const char *code, const char *modname, /* codestr, modnamespace */ 00118 const char *resfmt, void *cresult); /* result type, target */ 00119 00120 extern PyObject* 00121 PP_Debug_Codestr(PPStringModes mode, /* run string in pdb */ 00122 const char *codestring, PyObject *moddict); 00123 00124 extern PyObject * 00125 PP_Compile_Codestr(PPStringModes mode, 00126 const char *codestr); /* precompile to bytecode */ 00127 00128 extern int 00129 PP_Run_Bytecode(PyObject *codeobj, /* run a bytecode object */ 00130 const char *modname, 00131 const char *resfmt, void *restarget); 00132 00133 extern PyObject * /* run bytecode under pdb */ 00134 PP_Debug_Bytecode(PyObject *codeobject, PyObject *moddict); 00135 00136 00137 /*******************************************************/ 00138 /* ppembed-callables.c: call functions, classes, etc. */ 00139 /*******************************************************/ 00140 00141 extern BaseExport int /* mod.func(args) */ 00142 PP_Run_Function(const char *modname, const char *funcname, /* func|classname */ 00143 const char *resfmt, void *cresult, /* result target */ 00144 const char *argfmt, ... /* arg, arg... */ ); /* input arguments*/ 00145 00146 extern PyObject* 00147 PP_Debug_Function(PyObject *func, PyObject *args); /* call func in pdb */ 00148 00149 extern int 00150 PP_Run_Known_Callable(PyObject *object, /* func|class|method */ 00151 const char *resfmt, void *restarget, /* skip module fetch */ 00152 const char *argfmt, ... /* arg,.. */ ); 00153 00154 00155 /**************************************************************/ 00156 /* ppembed-attributes.c: run object methods, access members */ 00157 /**************************************************************/ 00158 00159 extern int 00160 PP_Run_Method(PyObject *pobject, const char *method, /* uses Debug_Function */ 00161 const char *resfmt, void *cresult, /* output */ 00162 const char *argfmt, ... /* arg, arg... */ ); /* inputs */ 00163 00164 extern int 00165 PP_Get_Member(PyObject *pobject, const char *attrname, 00166 const char *resfmt, void *cresult); /* output */ 00167 00168 extern int 00169 PP_Set_Member(PyObject *pobject, const char *attrname, 00170 const char *valfmt, ... /* val, val... */ ); /* input */ 00171 00172 00173 /**********************************************************/ 00174 /* ppembed-errors.c: get exception data after api error */ 00175 /**********************************************************/ 00176 00177 extern void PP_Fetch_Error_Text(); /* fetch (and clear) exception */ 00178 00179 extern char PP_last_error_type[]; /* exception name text */ 00180 extern char PP_last_error_info[]; /* exception data text */ 00181 extern char PP_last_error_trace[]; /* exception traceback text */ 00182 00183 extern PyObject *PP_last_traceback; /* saved exception traceback object */ 00184 00185 00186 #ifdef __cplusplus 00187 } 00188 #endif 00189 00190 #endif /*PREEMBED_H*/