| 1 |
// clang-format off |
| 2 |
/************************************************************************* |
| 3 |
* PPEMBED, VERSION 2.0 |
| 4 |
* AN ENHANCED PYTHON EMBEDDED-CALL INTERFACE |
| 5 |
* |
| 6 |
* Wraps Python's run-time embedding API functions for easy use. |
| 7 |
* Most utilities assume the call is qualified by an enclosing module |
| 8 |
* (namespace). The module can be a file-name reference or a dummy module |
| 9 |
* created to provide a namespace for file-less strings. These routines |
| 10 |
* automate debugging, module (re)loading, input/output conversions, etc. |
| 11 |
* |
| 12 |
* Python is automatically initialized when the first API call occurs. |
| 13 |
* Input/output conversions use the standard Python conversion format |
| 14 |
* codes (described in the C API manual). Errors are flagged as either |
| 15 |
* a -1 int, or a NULL pointer result. Exported names use a PP_ prefix |
| 16 |
* to minimize clashes; names in the built-in Python API use Py prefixes |
| 17 |
* instead (alas, there is no "import" equivalent in C, just "from*"). |
| 18 |
* Also note that the varargs code here may not be portable to certain |
| 19 |
* C compilers; to do it portably, see the text or file 'vararg.txt' |
| 20 |
* here, or search for string STDARG in Python's source code files. |
| 21 |
* |
| 22 |
* New in this version/edition: names now have a PP_ prefix, files |
| 23 |
* renamed, compiles to a single .a file, fixed pdb retval bug for |
| 24 |
* strings, and char* results returned by the "s" convert code now |
| 25 |
* point to new char arrays which the caller should free() when no |
| 26 |
* longer needed (this was a potential bug in prior version). Also |
| 27 |
* added new API interfaces for fetching exception info after errors, |
| 28 |
* precompiling code strings to byte code, and calling simple objects. |
| 29 |
* |
| 30 |
* Also fully supports Python 1.5 module package imports: module names |
| 31 |
* in this API can take the form "package.package.[...].module", where |
| 32 |
* Python maps the package names to a nested directories path in your |
| 33 |
* file system hierarchy; package dirs all contain __init__.py files, |
| 34 |
* and the leftmost one is in a directory found on PYTHONPATH. This |
| 35 |
* API's dynamic reload feature also works for modules in packages; |
| 36 |
* Python stores the full path name in the sys.modules dictionary. |
| 37 |
* |
| 38 |
* Caveats: there is no support for advanced things like threading or |
| 39 |
* restricted execution mode here, but such things may be added with |
| 40 |
* extra Python API calls external to this API (see the Python/C API |
| 41 |
* manual for C-level threading calls; see modules rexec and bastion |
| 42 |
* in the library manual for restricted mode details). For threading, |
| 43 |
* you may also be able to get by with C threads and distinct Python |
| 44 |
* namespaces per Python code segments, or Python language threads |
| 45 |
* started by Python code run from C (see the Python thread module). |
| 46 |
* |
| 47 |
* Note that Python can only reload Python modules, not C extensions, |
| 48 |
* but it's okay to leave the dynamic reload flag on even if you might |
| 49 |
* access dynamically-loaded C extension modules--in 1.5.2, Python |
| 50 |
* simply resets C extension modules to their initial attribute state |
| 51 |
* when reloaded, but doesn't actually reload the C extension file. |
| 52 |
*************************************************************************/ |
| 53 |
|
| 54 |
/* |
| 55 |
PPEMBED, VERSION 2.0 |
| 56 |
AN ENHANCED PYTHON EMBEDDED-CALL INTERFACE |
| 57 |
|
| 58 |
Copyright 1996-2000, by Mark Lutz, and O'Reilly and Associates. |
| 59 |
Permission to use, copy, modify, and distribute this software |
| 60 |
for any purpose and without fee is hereby granted. This software |
| 61 |
is provided on an as is basis, without warranties of any kind. |
| 62 |
*/ |
| 63 |
|
| 64 |
#ifndef PPEMBED_H |
| 65 |
#define PPEMBED_H |
| 66 |
|
| 67 |
#ifdef __cplusplus |
| 68 |
extern "C" { /* a C library, but callable from C++ */ |
| 69 |
#endif |
| 70 |
|
| 71 |
#include <stdio.h> //NOLINT |
| 72 |
// Python |
| 73 |
#if defined (_POSIX_C_SOURCE) |
| 74 |
# undef _POSIX_C_SOURCE |
| 75 |
#endif // (re-)defined in pyconfig.h |
| 76 |
#include <Python.h> |
| 77 |
#ifndef FC_GLOBAL_H |
| 78 |
#include <FCGlobal.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
extern int PP_RELOAD; /* 1=reload py modules when attributes referenced */ |
| 82 |
extern int PP_DEBUG; /* 1=start debugger when string/function/member run */ |
| 83 |
|
| 84 |
typedef enum { //NOLINT |
| 85 |
PP_EXPRESSION, /* which kind of code-string */ |
| 86 |
PP_STATEMENT /* expressions and statements differ */ |
| 87 |
} PPStringModes; |
| 88 |
|
| 89 |
|
| 90 |
/***************************************************/ |
| 91 |
/* ppembed-modules.c: load,access module objects */ |
| 92 |
/***************************************************/ |
| 93 |
|
| 94 |
extern const char *PP_Init(const char *modname); |
| 95 |
extern int PP_Make_Dummy_Module(const char *modname); |
| 96 |
extern PyObject *PP_Load_Module(const char *modname); |
| 97 |
extern PyObject *PP_Load_Attribute(const char *modname, const char *attrname); |
| 98 |
extern int PP_Run_Command_Line(const char *prompt); |
| 99 |
|
| 100 |
|
| 101 |
/**********************************************************/ |
| 102 |
/* ppembed-globals.c: read,write module-level variables */ |
| 103 |
/**********************************************************/ |
| 104 |
|
| 105 |
extern int |
| 106 |
PP_Convert_Result(PyObject *presult, const char *resFormat, void *resTarget); |
| 107 |
|
| 108 |
extern int |
| 109 |
PP_Get_Global(const char *modname, const char *varname, const char *resfmt, void *cresult); |
| 110 |
|
| 111 |
extern int |
| 112 |
PP_Set_Global(const char *modname, const char *varname, const char *valfmt, ... /*val*/); |
| 113 |
|
| 114 |
|
| 115 |
/***************************************************/ |
| 116 |
/* ppembed-strings.c: run strings of Python code */ |
| 117 |
/***************************************************/ |
| 118 |
|
| 119 |
extern int /* run C string of code */ |
| 120 |
PP_Run_Codestr(PPStringModes mode, /* code=expr or stmt? */ |
| 121 |
const char *code, const char *modname, /* codestr, modnamespace */ |
| 122 |
const char *resfmt, void *cresult); /* result type, target */ |
| 123 |
|
| 124 |
extern PyObject* |
| 125 |
PP_Debug_Codestr(PPStringModes mode, /* run string in pdb */ |
| 126 |
const char *codestring, PyObject *moddict); |
| 127 |
|
| 128 |
extern PyObject * |
| 129 |
PP_Compile_Codestr(PPStringModes mode, |
| 130 |
const char *codestr); /* precompile to bytecode */ |
| 131 |
|
| 132 |
extern int |
| 133 |
PP_Run_Bytecode(PyObject *codeobj, /* run a bytecode object */ |
| 134 |
const char *modname, |
| 135 |
const char *resfmt, void *restarget); |
| 136 |
|
| 137 |
extern PyObject * /* run bytecode under pdb */ |
| 138 |
PP_Debug_Bytecode(PyObject *codeobject, PyObject *moddict); |
| 139 |
|
| 140 |
|
| 141 |
/*******************************************************/ |
| 142 |
/* ppembed-callables.c: call functions, classes, etc. */ |
| 143 |
/*******************************************************/ |
| 144 |
|
| 145 |
extern BaseExport int /* mod.func(args) */ |
| 146 |
PP_Run_Function(const char *modname, const char *funcname, /* func|classname */ |
| 147 |
const char *resfmt, void *cresult, /* result target */ |
| 148 |
const char *argfmt, ... /* arg, arg... */ ); /* input arguments*/ |
| 149 |
|
| 150 |
extern PyObject* |
| 151 |
PP_Debug_Function(PyObject *func, PyObject *args); /* call func in pdb */ |
| 152 |
|
| 153 |
extern int |
| 154 |
PP_Run_Known_Callable(PyObject *object, /* func|class|method */ |
| 155 |
const char *resfmt, void *restarget, /* skip module fetch */ |
| 156 |
const char *argfmt, ... /* arg,.. */ ); |
| 157 |
|
| 158 |
|
| 159 |
/**************************************************************/ |
| 160 |
/* ppembed-attributes.c: run object methods, access members */ |
| 161 |
/**************************************************************/ |
| 162 |
|
| 163 |
extern int |
| 164 |
PP_Run_Method(PyObject *pobject, const char *method, /* uses Debug_Function */ |
| 165 |
const char *resfmt, void *cresult, /* output */ |
| 166 |
const char *argfmt, ... /* arg, arg... */ ); /* inputs */ |
| 167 |
|
| 168 |
extern int |
| 169 |
PP_Get_Member(PyObject *pobject, const char *attrname, |
| 170 |
const char *resfmt, void *cresult); /* output */ |
| 171 |
|
| 172 |
extern int |
| 173 |
PP_Set_Member(PyObject *pobject, const char *attrname, |
| 174 |
const char *valfmt, ... /* val, val... */ ); /* input */ |
| 175 |
|
| 176 |
|
| 177 |
/**********************************************************/ |
| 178 |
/* ppembed-errors.c: get exception data after api error */ |
| 179 |
/**********************************************************/ |
| 180 |
|
| 181 |
extern void PP_Fetch_Error_Text(); /* fetch (and clear) exception */ |
| 182 |
|
| 183 |
extern char PP_last_error_type[]; /* exception name text */ |
| 184 |
extern char PP_last_error_info[]; /* exception data text */ |
| 185 |
extern char PP_last_error_trace[]; /* exception traceback text */ |
| 186 |
|
| 187 |
extern PyObject *PP_PyDict_Object; /* saved PyDict object */ |
| 188 |
extern PyObject *PP_last_traceback; /* saved exception traceback object */ |
| 189 |
extern PyObject *PP_last_exception_type; /* saved exception type */ |
| 190 |
|
| 191 |
|
| 192 |
#ifdef __cplusplus |
| 193 |
} |
| 194 |
#endif |
| 195 |
|
| 196 |
#endif /*PREEMBED_H*/ |
| 197 |
// clang-format on |