From 011be343cd1427a1be68f9a0da38401b89de0fec Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 27 Jun 2021 16:20:52 +0200 Subject: Fix python backend calls --- backends/Makefile | 2 +- backends/python.c | 13 +++++++++++-- backends/python.md | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) (limited to 'backends') diff --git a/backends/Makefile b/backends/Makefile index 5054236..6c46007 100644 --- a/backends/Makefile +++ b/backends/Makefile @@ -92,7 +92,7 @@ lua.dll: LDLIBS += -L../ -llua53 # This one also spams a good deal of unwanted flags into CFLAGS, so we use only --includes. On the other hand, the --libs # info from this one seems to include the actual interpreter library only on some systems, which makes it worse than useless. python.so: CFLAGS += $(shell python3-config --includes || pkg-config --cflags python3 || pkg-config --cflags python || echo "-DBUILD_ERROR=\"Missing pkg-config data for python3\"") -python.so: LDLIBS += $(shell pkg-config --libs python3-embed || python3-config --libs || pkg-config --libs python || echo "-DBUILD_ERROR=\"Missing pkg-config data for python3\"") +python.so: LDLIBS += $(shell pkg-config --libs python3-embed || python3-config --libs || pkg-config --libs python3 || echo "-DBUILD_ERROR=\"Missing pkg-config data for python3\"") # Generic rules on how to build .SO/.DLL's from C and CPP sources %.so :: %.c %.h $(BACKEND_LIB) diff --git a/backends/python.c b/backends/python.c index b9b6518..c1a21be 100644 --- a/backends/python.c +++ b/backends/python.c @@ -1,4 +1,5 @@ #define BACKEND_NAME "python" +//#define DEBUG #define PY_SSIZE_T_CLEAN #include @@ -34,6 +35,8 @@ MM_PLUGIN_API int init(){ LOG("Failed to register backend"); return 1; } + + //Py_UnbufferedStdioFlag = 1; return 0; } @@ -159,7 +162,7 @@ static PyObject* mmpy_output(PyObject* self, PyObject* args){ else{ mm_channel_event(mm_channel(inst, u, 0), val); } - return 0; + break; } } @@ -401,6 +404,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; } @@ -415,6 +419,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); @@ -491,8 +496,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(); } @@ -717,6 +724,8 @@ static int python_start(size_t n, instance** inst){ //release interpreter PyEval_ReleaseThread(data->interpreter); } + + last_timestamp = mm_timestamp(); return 0; } diff --git a/backends/python.md b/backends/python.md index 2114a08..1c0c96f 100644 --- a/backends/python.md +++ b/backends/python.md @@ -79,8 +79,8 @@ The `python` backend does not take any global configuration. | Option | Example value | Default value | Description | |-----------------------|-----------------------|-----------------------|-----------------------------------------------| -| `module` | `my_handlers.py` | none | (Path to) Python module source file, relative to configuration file location | -| `default-handler` | `mu_handlers.default` | none | Function to be called as handler for all top-level channels (not belonging to a module) | +| `module` | `my_handlers` | none | Name of the python module to load (normally the name of a`.py` file without the extension) | +| `default-handler` | `my_handlers.default` | none | Function to be called as handler for all top-level channels (not belonging to a module) | A single instance may have multiple `module` options specified. This will make all handlers available within their module namespaces (see the section on channel specification). -- cgit v1.2.3