2024-12-31 05:44:19 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN 1
|
|
|
|
#include "pgs_api.h"
|
2024-12-31 01:57:48 +00:00
|
|
|
#include <Python.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
PyObject *_pModule = NULL;
|
|
|
|
bool python_initialized = false;
|
|
|
|
|
|
|
|
PyObject *py_construct() {
|
2024-12-31 05:44:19 +00:00
|
|
|
|
|
|
|
PyStatus status;
|
|
|
|
PyConfig config;
|
2024-12-31 01:57:48 +00:00
|
|
|
if (!python_initialized) {
|
2024-12-31 05:44:19 +00:00
|
|
|
if (PyImport_AppendInittab("pgs", PyInit_mymodule) == -1) {
|
|
|
|
fprintf(stderr, "Failed to add mymodule to the interpreter's table\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
|
|
|
|
|
|
|
/* optional but recommended */
|
|
|
|
status = PyConfig_SetBytesString(&config, &config.program_name, "pgs");
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
goto exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
goto exception;
|
|
|
|
}
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
// Py_Initialize();
|
|
|
|
if (!Py_IsInitialized()) {
|
|
|
|
fprintf(stderr, "Python initialization failed!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyGILState_STATE gstate = PyGILState_Ensure();
|
|
|
|
|
|
|
|
PyRun_SimpleString("import pgs");
|
|
|
|
|
|
|
|
// Py_InitModule("pgscript", NULL);
|
2024-12-31 01:57:48 +00:00
|
|
|
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);
|
|
|
|
|
2024-12-31 05:44:19 +00:00
|
|
|
PyGILState_Release(gstate);
|
2024-12-31 01:57:48 +00:00
|
|
|
python_initialized = true;
|
|
|
|
}
|
|
|
|
return _pModule;
|
2024-12-31 05:44:19 +00:00
|
|
|
exception:
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
|
|
|
return NULL;
|
2024-12-31 01:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2024-12-31 05:44:19 +00:00
|
|
|
|
|
|
|
PyGILState_STATE gstate = PyGILState_Ensure();
|
|
|
|
|
2024-12-31 01:57:48 +00:00
|
|
|
PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
|
2024-12-31 05:44:19 +00:00
|
|
|
PyGILState_Release(gstate);
|
|
|
|
|
2024-12-31 01:57:48 +00:00
|
|
|
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;
|
|
|
|
}
|