#include #include PyObject *_pModule = NULL; bool python_initialized = false; PyObject *py_construct() { if (!python_initialized) { Py_Initialize(); python_initialized = true; PyObject *sysPath = PySys_GetObject("path"); PyList_Append(sysPath, PyUnicode_FromString(".")); // Py_DECREF(sysPath); PyObject *pName = PyUnicode_DecodeFSDefault("pgscript"); _pModule = PyImport_Import(pName); Py_DECREF(pName); python_initialized = true; } return _pModule; } void py_destruct() { if (!python_initialized) return; Py_DECREF(_pModule); Py_Finalize(); python_initialized = false; } int py_route(int downstream, int upstream) { PyObject *pModule = py_construct(); long upstream_fd = 0; if (pModule != NULL) { PyObject *pFunc = PyObject_GetAttrString(pModule, "route"); if (PyCallable_Check(pFunc)) { PyObject *pArgs = PyTuple_Pack(2, PyLong_FromLong(downstream), PyLong_FromLong(upstream)); PyObject *pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { upstream_fd = PyLong_AsLong(pValue); Py_DECREF(pValue); } else { PyErr_Print(); fprintf(stderr, "Call failed\n"); } } else { PyErr_Print(); fprintf(stderr, "Cannot find function 'route'\n"); } Py_XDECREF(pFunc); } else { PyErr_Print(); fprintf(stderr, "Failed to load 'script'\n"); } return (int)upstream_fd; }