Procházet zdrojové kódy

app_python: function to allow optional execution of python method

Daniel-Constantin Mierla před 9 roky
rodič
revize
cd6b3d9717
2 změnil soubory, kde provedl 98 přidání a 79 odebrání
  1. 95 77
      modules/app_python/python_exec.c
  2. 3 2
      modules/app_python/python_exec.h

+ 95 - 77
modules/app_python/python_exec.c

@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
-*/
+ */
 
 #include <Python.h>
 
@@ -35,90 +35,108 @@
 #include "python_msgobj.h"
 #include "python_support.h"
 
-int
-python_exec1(struct sip_msg* _msg, char* method_name, char *foobar)
-{
-    return python_exec2(_msg, method_name, NULL);
-}
 
-int
-python_exec2(struct sip_msg *_msg, char *method_name, char *mystr)
+int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
 {
-    PyObject *pFunc, *pArgs, *pValue, *pResult;
-    PyObject *msg;
-    int rval;
+	PyObject *pFunc, *pArgs, *pValue, *pResult;
+	PyObject *msg;
+	int rval;
+
+	PyEval_AcquireLock();
+	PyThreadState_Swap(myThreadState);
 
-    PyEval_AcquireLock();
-    PyThreadState_Swap(myThreadState);
+	pFunc = PyObject_GetAttrString(handler_obj, fname);
+	if (pFunc == NULL || !PyCallable_Check(pFunc)) {
+		if(emode==0) {
+			LM_ERR("%s not found or is not callable\n", fname);
+		} else {
+			LM_DBG("%s not found or is not callable\n", fname);
+		}
+		Py_XDECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		if(emode==0) {
+			return -1;
+		} else {
+			return 1;
+		}
+	}
 
-    pFunc = PyObject_GetAttrString(handler_obj, method_name);
-    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
-        LM_ERR("%s not found or is not callable\n", method_name);
-        Py_XDECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
+	msg = newmsgobject(_msg);
+	if (msg == NULL) {
+		LM_ERR("can't create MSGtype instance\n");
+		Py_DECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
 
-    msg = newmsgobject(_msg);
-    if (msg == NULL) {
-        LM_ERR("can't create MSGtype instance\n");
-        Py_DECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
+	pArgs = PyTuple_New(fparam == NULL ? 1 : 2);
+	if (pArgs == NULL) {
+		LM_ERR("PyTuple_New() has failed\n");
+		msg_invalidate(msg);
+		Py_DECREF(msg);
+		Py_DECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+	PyTuple_SetItem(pArgs, 0, msg);
+	/* Tuple steals msg */
 
-    pArgs = PyTuple_New(mystr == NULL ? 1 : 2);
-    if (pArgs == NULL) {
-        LM_ERR("PyTuple_New() has failed\n");
-        msg_invalidate(msg);
-        Py_DECREF(msg);
-        Py_DECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-    PyTuple_SetItem(pArgs, 0, msg);
-    /* Tuple steals msg */
+	if (fparam != NULL) {
+		pValue = PyString_FromString(fparam);
+		if (pValue == NULL) {
+			LM_ERR("PyString_FromString(%s) has failed\n", fparam);
+			msg_invalidate(msg);
+			Py_DECREF(pArgs);
+			Py_DECREF(pFunc);
+			PyThreadState_Swap(NULL);
+			PyEval_ReleaseLock();
+			return -1;
+		}
+		PyTuple_SetItem(pArgs, 1, pValue);
+		/* Tuple steals pValue */
+	}
 
-    if (mystr != NULL) {
-        pValue = PyString_FromString(mystr);
-        if (pValue == NULL) {
-            LM_ERR("PyString_FromString(%s) has failed\n", mystr);
-            msg_invalidate(msg);
-            Py_DECREF(pArgs);
-            Py_DECREF(pFunc);
-            PyThreadState_Swap(NULL);
-            PyEval_ReleaseLock();
-            return -1;
-        }
-        PyTuple_SetItem(pArgs, 1, pValue);
-        /* Tuple steals pValue */
-    }
+	pResult = PyObject_CallObject(pFunc, pArgs);
+	msg_invalidate(msg);
+	Py_DECREF(pArgs);
+	Py_DECREF(pFunc);
+	if (PyErr_Occurred()) {
+		Py_XDECREF(pResult);
+		python_handle_exception("python_exec2");
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
 
-    pResult = PyObject_CallObject(pFunc, pArgs);
-    msg_invalidate(msg);
-    Py_DECREF(pArgs);
-    Py_DECREF(pFunc);
-    if (PyErr_Occurred()) {
-        Py_XDECREF(pResult);
-        python_handle_exception("python_exec2");
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
+	if (pResult == NULL) {
+		LM_ERR("PyObject_CallObject() returned NULL\n");
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	rval = PyInt_AsLong(pResult);
+	Py_DECREF(pResult);
+	PyThreadState_Swap(NULL);
+	PyEval_ReleaseLock();
+	return rval;
+}
 
-    if (pResult == NULL) {
-        LM_ERR("PyObject_CallObject() returned NULL\n");
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
+/**
+ *
+ */
+int python_exec1(sip_msg_t *_msg, char *method_name, char *foobar)
+{
+	return apy_exec(_msg, method_name, NULL, 0);
+}
 
-    rval = PyInt_AsLong(pResult);
-    Py_DECREF(pResult);
-    PyThreadState_Swap(NULL);
-    PyEval_ReleaseLock();
-    return rval;
+/**
+ *
+ */
+int python_exec2(sip_msg_t *_msg, char *method_name, char *mystr)
+{
+	return apy_exec(_msg, method_name, mystr, 0);
 }

+ 3 - 2
modules/app_python/python_exec.h

@@ -24,7 +24,8 @@
 
 #include "../../parser/msg_parser.h"
 
-int python_exec1(struct sip_msg *, char *, char *);
-int python_exec2(struct sip_msg *, char *, char *);
+int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode);
+int python_exec1(sip_msg_t *, char *, char *);
+int python_exec2(sip_msg_t *, char *, char *);
 
 #endif