diff options
Diffstat (limited to 'backends/python.c')
-rw-r--r-- | backends/python.c | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/backends/python.c b/backends/python.c index bd73a20..9c0caa1 100644 --- a/backends/python.c +++ b/backends/python.c @@ -1,4 +1,9 @@ #define BACKEND_NAME "python" +//#define DEBUG + +#ifdef _WIN32 + #include <direct.h> +#endif #define PY_SSIZE_T_CLEAN #include <string.h> @@ -34,6 +39,8 @@ MM_PLUGIN_API int init(){ LOG("Failed to register backend"); return 1; } + + //Py_UnbufferedStdioFlag = 1; return 0; } @@ -112,6 +119,24 @@ static int python_prepend_str(PyObject* list, char* str){ return 0; } +static PyObject* mmpy_channels(PyObject* self, PyObject* args){ + size_t u = 0; + PyObject* list = NULL; + instance* inst = *((instance**) PyModule_GetState(self)); + python_instance_data* data = (python_instance_data*) inst->impl; + + if(!last_timestamp){ + LOG("The channels() API will not return usable results before the configuration has been read completely"); + } + + list = PyList_New(data->channels); + for(u = 0; u < data->channels; u++){ + PyList_SET_ITEM(list, u, PyUnicode_FromString(data->channel[u].name)); + } + + return list; +} + static PyObject* mmpy_output(PyObject* self, PyObject* args){ instance* inst = *((instance**) PyModule_GetState(self)); python_instance_data* data = (python_instance_data*) inst->impl; @@ -141,7 +166,7 @@ static PyObject* mmpy_output(PyObject* self, PyObject* args){ else{ mm_channel_event(mm_channel(inst, u, 0), val); } - return 0; + break; } } @@ -383,6 +408,7 @@ static int mmpy_exec(PyObject* module) { PyObject* capsule = PyDict_GetItemString(PyThreadState_GetDict(), MMPY_INSTANCE_KEY); if(capsule && inst){ *inst = PyCapsule_GetPointer(capsule, NULL); + DBGPF("Initializing extension module on instance %s", (*inst)->name); return 0; } @@ -397,6 +423,7 @@ static int python_configure_instance(instance* inst, char* option, char* value){ //load python script if(!strcmp(option, "module")){ //swap to interpreter + //PyThreadState_Swap(data->interpreter); PyEval_RestoreThread(data->interpreter); //import the module module = PyImport_ImportModule(value); @@ -432,6 +459,7 @@ static PyObject* mmpy_init(){ {"timestamp", mmpy_timestamp, METH_VARARGS, "Get the core timestamp (in milliseconds)"}, {"manage", mmpy_manage_fd, METH_VARARGS, "(Un-)register a socket or file descriptor for notifications"}, {"interval", mmpy_interval, METH_VARARGS, "Register or update an interval handler"}, + {"channels", mmpy_channels, METH_VARARGS, "List currently registered instance channels"}, {"cleanup_handler", mmpy_cleanup_handler, METH_VARARGS, "Register or update the instances cleanup handler"}, {0} }; @@ -472,8 +500,10 @@ static int python_instance(instance* inst){ Py_SetProgramName(program_name); //initialize python Py_InitializeEx(0); - //create, acquire and release the GIL + #if PY_MINOR_VERSION < 7 + //in python 3.6 and earlier, this was required to set up the GIL PyEval_InitThreads(); + #endif python_main = PyEval_SaveThread(); } @@ -698,6 +728,8 @@ static int python_start(size_t n, instance** inst){ //release interpreter PyEval_ReleaseThread(data->interpreter); } + + last_timestamp = mm_timestamp(); return 0; } |