|
| 1 | + |
| 2 | +#include <Python.h> |
| 3 | + |
| 4 | +#include <stdarg.h> |
| 5 | +#include <stdio.h> |
| 6 | + |
| 7 | +#include "py-eval.h" |
| 8 | + |
| 9 | +static void |
| 10 | +verbose(char* fmt, ...) |
| 11 | +{ |
| 12 | + static bool verbose = false; |
| 13 | + |
| 14 | + if (!verbose) return; |
| 15 | + |
| 16 | + va_list ap; |
| 17 | + va_start(ap, fmt); |
| 18 | + vprintf(fmt, ap); |
| 19 | + printf("\n"); |
| 20 | + va_end(ap); |
| 21 | +} |
| 22 | + |
| 23 | +static bool |
| 24 | +handle_python_exception(void) |
| 25 | +{ |
| 26 | + printf("\n"); |
| 27 | + printf("PYTHON EXCEPTION:\n"); |
| 28 | + |
| 29 | + #if PYTHON_VERSION_MAJOR >= 3 |
| 30 | + |
| 31 | + PyObject *exc,*val,*tb; |
| 32 | + PyErr_Fetch(&exc,&val,&tb); |
| 33 | + PyObject_Print(exc, stdout, Py_PRINT_RAW); |
| 34 | + printf("\n"); |
| 35 | + PyObject_Print(val, stdout, Py_PRINT_RAW); |
| 36 | + printf("\n"); |
| 37 | + |
| 38 | + #else // Python 2 |
| 39 | + |
| 40 | + PyErr_Print(); |
| 41 | + |
| 42 | + #endif |
| 43 | + |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +static bool |
| 48 | +handle_python_non_string(PyObject* o) |
| 49 | +{ |
| 50 | + printf("python: expression did not return a string!\n"); |
| 51 | + fflush(stdout); |
| 52 | + printf("python: expression evaluated to: "); |
| 53 | + PyObject_Print(o, stdout, 0); |
| 54 | + printf("\n"); |
| 55 | + return false; |
| 56 | +} |
| 57 | + |
| 58 | +static PyObject* main_module = NULL; |
| 59 | +static PyObject* main_dict = NULL; |
| 60 | +static PyObject* local_dict = NULL; |
| 61 | + |
| 62 | +static bool initialized = false; |
| 63 | + |
| 64 | +bool |
| 65 | +python_init() |
| 66 | +{ |
| 67 | + if (initialized) return true; |
| 68 | + verbose("python: initializing..."); |
| 69 | + Py_InitializeEx(1); |
| 70 | + main_module = PyImport_AddModule("__main__"); |
| 71 | + if (main_module == NULL) return handle_python_exception(); |
| 72 | + main_dict = PyModule_GetDict(main_module); |
| 73 | + if (main_dict == NULL) return handle_python_exception(); |
| 74 | + local_dict = PyDict_New(); |
| 75 | + if (local_dict == NULL) return handle_python_exception(); |
| 76 | + initialized = true; |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +static char* python_result_default = "NOTHING"; |
| 81 | + |
| 82 | +/** |
| 83 | + @param persist: If true, retain the Python interpreter, |
| 84 | + else finalize it |
| 85 | + @param code: The multiline string of Python code. |
| 86 | +
|
| 87 | + @return true on success, false on error |
| 88 | + */ |
| 89 | +bool |
| 90 | +python_code(const char* code) |
| 91 | +{ |
| 92 | + // Execute code: |
| 93 | + verbose("python: code: %s", code); |
| 94 | + PyRun_String(code, Py_file_input, main_dict, local_dict); |
| 95 | + if (PyErr_Occurred()) return handle_python_exception(); |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + The expr is evaluated to the returned result |
| 101 | + @param output: Store result pointer here |
| 102 | +*/ |
| 103 | +bool python_eval(const char* expr, char** output) |
| 104 | +{ |
| 105 | + char* result = python_result_default; |
| 106 | + |
| 107 | + // Evaluate expression: |
| 108 | + verbose("python: expr: %s", expr); |
| 109 | + PyObject* o = PyRun_String(expr, Py_eval_input, main_dict, local_dict); |
| 110 | + if (o == NULL) return handle_python_exception(); |
| 111 | + |
| 112 | + // Convert Python result to C string |
| 113 | + int pc = PyArg_Parse(o, "s", &result); |
| 114 | + if (pc != 1) return handle_python_non_string(o); |
| 115 | + verbose("python: result: %s\n", result); |
| 116 | + *output = strdup(result); |
| 117 | + |
| 118 | + // Clean up and return: |
| 119 | + Py_DECREF(o); |
| 120 | + |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +bool |
| 125 | +python_reset() |
| 126 | +{ |
| 127 | + python_finalize(); |
| 128 | + return python_init(); |
| 129 | +} |
| 130 | + |
| 131 | +void |
| 132 | +python_finalize() |
| 133 | +{ |
| 134 | + Py_Finalize(); |
| 135 | + initialized = false; |
| 136 | +} |
0 commit comments