Browse Source

app_python: coherent indetation and whitespacing

Daniel-Constantin Mierla 9 năm trước cách đây
mục cha
commit
8d7c12ab29

+ 7 - 7
modules/app_python/mod_Core.c

@@ -42,27 +42,27 @@
 #include "mod_Core.h"
 
 PyMethodDef CoreMethods[] = {
-    {NULL, NULL, 0, NULL}
+	{NULL, NULL, 0, NULL}
 };
 
 void init_mod_Core(void)
 {
-    core_module = Py_InitModule("Router.Core", CoreMethods);
-    PyDict_SetItemString(main_module_dict, "Core", core_module);
+	core_module = Py_InitModule("Router.Core", CoreMethods);
+	PyDict_SetItemString(main_module_dict, "Core", core_module);
 
-    Py_INCREF(core_module);
+	Py_INCREF(core_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Core' has been initialized\n");
+	LM_ERR("Module 'Router.Core' has been initialized\n");
 #endif
 
 }
 
 void destroy_mod_Core(void)
 {
-    Py_XDECREF(core_module);
+	Py_XDECREF(core_module);
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Core' has been destroyed\n");
+	LM_ERR("Module 'Router.Core' has been destroyed\n");
 #endif
 
 }

+ 94 - 94
modules/app_python/mod_Logger.c

@@ -46,16 +46,16 @@
  */
 static PyObject *logger_LM_GEN1(PyObject *self, PyObject *args)
 {
-    int log_level;
-    char *msg;
+	int log_level;
+	char *msg;
 
-    if (!PyArg_ParseTuple(args, "is:LM_GEN1", &log_level, &msg))
-        return NULL;
+	if (!PyArg_ParseTuple(args, "is:LM_GEN1", &log_level, &msg))
+		return NULL;
 
-    LM_GEN1(log_level, "%s", msg);
+	LM_GEN1(log_level, "%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -63,17 +63,17 @@ static PyObject *logger_LM_GEN1(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_GEN2(PyObject *self, PyObject *args)
 {
-    int log_facility;
-    int log_level;
-    char *msg;
+	int log_facility;
+	int log_level;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "iis:LM_GEN2", &log_facility, &log_level, &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "iis:LM_GEN2", &log_facility, &log_level, &msg))
+		return NULL;
 
-    LM_GEN2(log_facility, log_level, "%s", msg);
+	LM_GEN2(log_facility, log_level, "%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -81,15 +81,15 @@ static PyObject *logger_LM_GEN2(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_ALERT(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_ALERT", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_ALERT", &msg))
+		return NULL;
 
-    LM_ALERT("%s", msg);
+	LM_ALERT("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 
@@ -98,15 +98,15 @@ static PyObject *logger_LM_ALERT(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_CRIT(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_CRIT", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_CRIT", &msg))
+		return NULL;
 
-    LM_CRIT("%s", msg);
+	LM_CRIT("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -114,15 +114,15 @@ static PyObject *logger_LM_CRIT(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_WARN(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_WARN", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_WARN", &msg))
+		return NULL;
 
-    LM_WARN("%s", msg);
+	LM_WARN("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -130,15 +130,15 @@ static PyObject *logger_LM_WARN(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_NOTICE(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_NOTICE", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_NOTICE", &msg))
+		return NULL;
 
-    LM_NOTICE("%s", msg);
+	LM_NOTICE("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -146,15 +146,15 @@ static PyObject *logger_LM_NOTICE(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_ERR(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_ERR", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_ERR", &msg))
+		return NULL;
 
-    LM_ERR("%s", msg);
+	LM_ERR("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -162,15 +162,15 @@ static PyObject *logger_LM_ERR(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_INFO(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_INFO", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_INFO", &msg))
+		return NULL;
 
-    LM_INFO("%s", msg);
+	LM_INFO("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 /*
@@ -178,69 +178,69 @@ static PyObject *logger_LM_INFO(PyObject *self, PyObject *args)
  */
 static PyObject *logger_LM_DBG(PyObject *self, PyObject *args)
 {
-    char *msg;
+	char *msg;
 
-    if(!PyArg_ParseTuple(args, "s:LM_DBG", &msg))
-        return NULL;
+	if(!PyArg_ParseTuple(args, "s:LM_DBG", &msg))
+		return NULL;
 
-    LM_DBG("%s", msg);
+	LM_DBG("%s", msg);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 PyMethodDef LoggerMethods[] = {
-    {"LM_GEN1",		(PyCFunction)logger_LM_GEN1,		METH_VARARGS, "Print GEN1 message."},
-    {"LM_GEN2",		(PyCFunction)logger_LM_GEN2,		METH_VARARGS, "Print GEN2 message."},
-    {"LM_ALERT",	(PyCFunction)logger_LM_ALERT,		METH_VARARGS, "Print alert message."},
-    {"LM_CRIT",		(PyCFunction)logger_LM_CRIT,		METH_VARARGS, "Print critical message."},
-    {"LM_ERR",		(PyCFunction)logger_LM_ERR,		METH_VARARGS, "Print error message."},
-    {"LM_WARN",		(PyCFunction)logger_LM_WARN,		METH_VARARGS, "Print warning message."},
-    {"LM_NOTICE",	(PyCFunction)logger_LM_NOTICE,		METH_VARARGS, "Print notice message."},
-    {"LM_INFO",		(PyCFunction)logger_LM_INFO,		METH_VARARGS, "Print info message."},
-    {"LM_DBG",		(PyCFunction)logger_LM_DBG,		METH_VARARGS, "Print debug message."},
-    {NULL, 		NULL, 			0, 		NULL}
+	{"LM_GEN1",		(PyCFunction)logger_LM_GEN1,		METH_VARARGS, "Print GEN1 message."},
+	{"LM_GEN2",		(PyCFunction)logger_LM_GEN2,		METH_VARARGS, "Print GEN2 message."},
+	{"LM_ALERT",	(PyCFunction)logger_LM_ALERT,		METH_VARARGS, "Print alert message."},
+	{"LM_CRIT",		(PyCFunction)logger_LM_CRIT,		METH_VARARGS, "Print critical message."},
+	{"LM_ERR",		(PyCFunction)logger_LM_ERR,		METH_VARARGS, "Print error message."},
+	{"LM_WARN",		(PyCFunction)logger_LM_WARN,		METH_VARARGS, "Print warning message."},
+	{"LM_NOTICE",	(PyCFunction)logger_LM_NOTICE,		METH_VARARGS, "Print notice message."},
+	{"LM_INFO",		(PyCFunction)logger_LM_INFO,		METH_VARARGS, "Print info message."},
+	{"LM_DBG",		(PyCFunction)logger_LM_DBG,		METH_VARARGS, "Print debug message."},
+	{NULL, 		NULL, 			0, 		NULL}
 };
 
 void init_mod_Logger(void)
 {
-    logger_module = Py_InitModule("Router.Logger", LoggerMethods);
-    PyDict_SetItemString(main_module_dict, "Logger", logger_module);
-
-    /*
-    * Log levels
-    * Reference: dprint.h
-    */
-    PyModule_AddObject(logger_module, "L_ALERT",  PyInt_FromLong((long)L_ALERT));
-    PyModule_AddObject(logger_module, "L_BUG",    PyInt_FromLong((long)L_BUG));
-    PyModule_AddObject(logger_module, "L_CRIT2",  PyInt_FromLong((long)L_CRIT2)); /* like L_CRIT, but adds prefix */
-    PyModule_AddObject(logger_module, "L_CRIT",   PyInt_FromLong((long)L_CRIT));  /* no prefix added */
-    PyModule_AddObject(logger_module, "L_ERR",    PyInt_FromLong((long)L_ERR));
-    PyModule_AddObject(logger_module, "L_WARN",   PyInt_FromLong((long)L_WARN));
-    PyModule_AddObject(logger_module, "L_NOTICE", PyInt_FromLong((long)L_NOTICE));
-    PyModule_AddObject(logger_module, "L_INFO",   PyInt_FromLong((long)L_INFO));
-    PyModule_AddObject(logger_module, "L_DBG",    PyInt_FromLong((long)L_DBG));
-
-    /*
-    * Facility
-    * Reference: dprint.h
-    */
-    PyModule_AddObject(logger_module, "DEFAULT_FACILITY", PyInt_FromLong((long)DEFAULT_FACILITY));
-
-    Py_INCREF(logger_module);
+	logger_module = Py_InitModule("Router.Logger", LoggerMethods);
+	PyDict_SetItemString(main_module_dict, "Logger", logger_module);
+
+	/*
+	 * Log levels
+	 * Reference: dprint.h
+	 */
+	PyModule_AddObject(logger_module, "L_ALERT",  PyInt_FromLong((long)L_ALERT));
+	PyModule_AddObject(logger_module, "L_BUG",    PyInt_FromLong((long)L_BUG));
+	PyModule_AddObject(logger_module, "L_CRIT2",  PyInt_FromLong((long)L_CRIT2)); /* like L_CRIT, but adds prefix */
+	PyModule_AddObject(logger_module, "L_CRIT",   PyInt_FromLong((long)L_CRIT));  /* no prefix added */
+	PyModule_AddObject(logger_module, "L_ERR",    PyInt_FromLong((long)L_ERR));
+	PyModule_AddObject(logger_module, "L_WARN",   PyInt_FromLong((long)L_WARN));
+	PyModule_AddObject(logger_module, "L_NOTICE", PyInt_FromLong((long)L_NOTICE));
+	PyModule_AddObject(logger_module, "L_INFO",   PyInt_FromLong((long)L_INFO));
+	PyModule_AddObject(logger_module, "L_DBG",    PyInt_FromLong((long)L_DBG));
+
+	/*
+	 * Facility
+	 * Reference: dprint.h
+	 */
+	PyModule_AddObject(logger_module, "DEFAULT_FACILITY", PyInt_FromLong((long)DEFAULT_FACILITY));
+
+	Py_INCREF(logger_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Logger' has been initialized\n");
+	LM_ERR("Module 'Router.Logger' has been initialized\n");
 #endif
 
 }
 
 void destroy_mod_Logger(void)
 {
-    Py_XDECREF(logger_module);
+	Py_XDECREF(logger_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Logger' has been destroyed\n");
+	LM_ERR("Module 'Router.Logger' has been destroyed\n");
 #endif
 
 }

+ 19 - 19
modules/app_python/mod_Ranks.c

@@ -42,41 +42,41 @@
 #include "mod_Ranks.h"
 
 PyMethodDef RanksMethods[] = {
-    {NULL, NULL, 0, NULL}
+	{NULL, NULL, 0, NULL}
 };
 
 void init_mod_Ranks(void)
 {
-    ranks_module = Py_InitModule("Router.Ranks", RanksMethods);
-    PyDict_SetItemString(main_module_dict, "Ranks", ranks_module);
+	ranks_module = Py_InitModule("Router.Ranks", RanksMethods);
+	PyDict_SetItemString(main_module_dict, "Ranks", ranks_module);
 
-    PyModule_AddObject(ranks_module, "PROC_MAIN",		PyInt_FromLong((long)PROC_MAIN));		
-    PyModule_AddObject(ranks_module, "PROC_TIMER",		PyInt_FromLong((long)PROC_TIMER));
-    PyModule_AddObject(ranks_module, "PROC_RPC",		PyInt_FromLong((long)PROC_RPC));
-    PyModule_AddObject(ranks_module, "PROC_FIFO",		PyInt_FromLong((long)PROC_FIFO));
-    PyModule_AddObject(ranks_module, "PROC_TCP_MAIN",		PyInt_FromLong((long)PROC_TCP_MAIN));
-    PyModule_AddObject(ranks_module, "PROC_UNIXSOCK",		PyInt_FromLong((long)PROC_UNIXSOCK));
-    PyModule_AddObject(ranks_module, "PROC_ATTENDANT",		PyInt_FromLong((long)PROC_ATTENDANT));
-    PyModule_AddObject(ranks_module, "PROC_INIT",		PyInt_FromLong((long)PROC_INIT));
-    PyModule_AddObject(ranks_module, "PROC_NOCHLDINIT",		PyInt_FromLong((long)PROC_NOCHLDINIT));
-    PyModule_AddObject(ranks_module, "PROC_SIPINIT",		PyInt_FromLong((long)PROC_SIPINIT));
-    PyModule_AddObject(ranks_module, "PROC_SIPRPC",		PyInt_FromLong((long)PROC_SIPRPC));
-    PyModule_AddObject(ranks_module, "PROC_MIN",		PyInt_FromLong((long)PROC_MIN));
+	PyModule_AddObject(ranks_module, "PROC_MAIN",		PyInt_FromLong((long)PROC_MAIN));
+	PyModule_AddObject(ranks_module, "PROC_TIMER",		PyInt_FromLong((long)PROC_TIMER));
+	PyModule_AddObject(ranks_module, "PROC_RPC",		PyInt_FromLong((long)PROC_RPC));
+	PyModule_AddObject(ranks_module, "PROC_FIFO",		PyInt_FromLong((long)PROC_FIFO));
+	PyModule_AddObject(ranks_module, "PROC_TCP_MAIN",		PyInt_FromLong((long)PROC_TCP_MAIN));
+	PyModule_AddObject(ranks_module, "PROC_UNIXSOCK",		PyInt_FromLong((long)PROC_UNIXSOCK));
+	PyModule_AddObject(ranks_module, "PROC_ATTENDANT",		PyInt_FromLong((long)PROC_ATTENDANT));
+	PyModule_AddObject(ranks_module, "PROC_INIT",		PyInt_FromLong((long)PROC_INIT));
+	PyModule_AddObject(ranks_module, "PROC_NOCHLDINIT",		PyInt_FromLong((long)PROC_NOCHLDINIT));
+	PyModule_AddObject(ranks_module, "PROC_SIPINIT",		PyInt_FromLong((long)PROC_SIPINIT));
+	PyModule_AddObject(ranks_module, "PROC_SIPRPC",		PyInt_FromLong((long)PROC_SIPRPC));
+	PyModule_AddObject(ranks_module, "PROC_MIN",		PyInt_FromLong((long)PROC_MIN));
 
-    Py_INCREF(ranks_module);
+	Py_INCREF(ranks_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Ranks' has been initialized\n");
+	LM_ERR("Module 'Router.Ranks' has been initialized\n");
 #endif
 
 }
 
 void destroy_mod_Ranks(void)
 {
-    Py_XDECREF(ranks_module);
+	Py_XDECREF(ranks_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router.Ranks' has been destroyed\n");
+	LM_ERR("Module 'Router.Ranks' has been destroyed\n");
 #endif
 
 }

+ 8 - 8
modules/app_python/mod_Router.c

@@ -41,29 +41,29 @@
 
 
 PyMethodDef RouterMethods[] = {
-    {NULL, NULL, 0, NULL}
+	{NULL, NULL, 0, NULL}
 };
 
 void init_mod_Router(void)
 {
-    main_module = Py_InitModule("Router", RouterMethods);
-    main_module_dict = PyModule_GetDict(main_module);
+	main_module = Py_InitModule("Router", RouterMethods);
+	main_module_dict = PyModule_GetDict(main_module);
 
-    Py_INCREF(main_module);
+	Py_INCREF(main_module);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router' has been initialized\n");
+	LM_ERR("Module 'Router' has been initialized\n");
 #endif
 
 }
 
 void destroy_mod_Router(void)
 {
-    Py_XDECREF(main_module);
-    Py_XDECREF(main_module_dict);
+	Py_XDECREF(main_module);
+	Py_XDECREF(main_module_dict);
 
 #ifdef WITH_EXTRA_DEBUG
-    LM_ERR("Module 'Router' has been destroyed\n");
+	LM_ERR("Module 'Router' has been destroyed\n");
 #endif
 
 }

+ 6 - 6
modules/app_python/python_iface.c

@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  *
  * This file is part of Kamailio, a free SIP server.
@@ -38,11 +38,11 @@
 
 int ap_init_modules(void)
 {
-    init_mod_Router();
-    init_mod_Core();
-    init_mod_Ranks();
-    init_mod_Logger();
+	init_mod_Router();
+	init_mod_Core();
+	init_mod_Ranks();
+	init_mod_Logger();
 
-    return 0;
+	return 0;
 }
 

+ 302 - 302
modules/app_python/python_mod.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>
 #include <libgen.h>
@@ -55,330 +55,330 @@ PyThreadState *myThreadState;
 
 /** module parameters */
 static param_export_t params[]={
-    {"script_name",        PARAM_STR, &script_name },
-    {"mod_init_function",  PARAM_STR, &mod_init_fname },
-    {"child_init_method",  PARAM_STR, &child_init_mname },
-    {0,0,0}
+	{"script_name",        PARAM_STR, &script_name },
+	{"mod_init_function",  PARAM_STR, &mod_init_fname },
+	{"child_init_method",  PARAM_STR, &child_init_mname },
+	{0,0,0}
 };
 
 /*
  * Exported functions
  */
 static cmd_export_t cmds[] = {
-    { "python_exec", (cmd_function)python_exec1, 1,  NULL, 0,	ANY_ROUTE },
-    { "python_exec", (cmd_function)python_exec2, 2,  NULL, 0,	ANY_ROUTE },
-    { 0, 0, 0, 0, 0, 0 }
+	{ "python_exec", (cmd_function)python_exec1, 1,  NULL, 0,	ANY_ROUTE },
+	{ "python_exec", (cmd_function)python_exec2, 2,  NULL, 0,	ANY_ROUTE },
+	{ 0, 0, 0, 0, 0, 0 }
 };
 
 /** module exports */
 struct module_exports exports = {
-    "app_python",                   /* module name */
-    RTLD_NOW | RTLD_GLOBAL,         /* dlopen flags */
-    cmds,                           /* exported functions */
-    params,                         /* exported parameters */
-    0,                              /* exported statistics */
-    0,                              /* exported MI functions */
-    0,                              /* exported pseudo-variables */
-    0,                              /* extra processes */
-    mod_init,                       /* module initialization function */
-    (response_function) NULL,       /* response handling function */
-    (destroy_function) mod_destroy, /* destroy function */
-    child_init                      /* per-child init function */
+	"app_python",                   /* module name */
+	RTLD_NOW | RTLD_GLOBAL,         /* dlopen flags */
+	cmds,                           /* exported functions */
+	params,                         /* exported parameters */
+	0,                              /* exported statistics */
+	0,                              /* exported MI functions */
+	0,                              /* exported pseudo-variables */
+	0,                              /* extra processes */
+	mod_init,                       /* module initialization function */
+	(response_function) NULL,       /* response handling function */
+	(destroy_function) mod_destroy, /* destroy function */
+	child_init                      /* per-child init function */
 };
 
 static int mod_init(void)
 {
-    char *dname_src, *bname_src;
-
-    int i;
-    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
-    PyThreadState *mainThreadState;
-
-    dname_src = as_asciiz(&script_name);
-    bname_src = as_asciiz(&script_name);
-
-    if(dname_src==NULL || bname_src==NULL)
-    {
-        LM_ERR("no more pkg memory\n");
-        return -1;
-    }
-
-    dname = strdup(dirname(dname_src));
-    if (strlen(dname) == 0)
-        dname = ".";
-    bname = strdup(basename(bname_src));
-    i = strlen(bname);
-    if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
-        i -= 1;
-    if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
-        bname[i - 3] = '\0';
-    } else {
-        LM_ERR("%s: script_name doesn't look like a python script\n",
-          script_name.s);
-        return -1;
-    }
-
-    Py_Initialize();
-    PyEval_InitThreads();
-    mainThreadState = PyThreadState_Get();
-
-    format_exc_obj = InitTracebackModule();
-
-    if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
-    {
-        Py_XDECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    sys_path = PySys_GetObject("path");
-    /* PySys_GetObject doesn't pass reference! No need to DEREF */
-    if (sys_path == NULL) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    pDir = PyString_FromString(dname);
-    if (pDir == NULL) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    PyList_Insert(sys_path, 0, pDir);
-    Py_DECREF(pDir);
-
-    if (ap_init_modules() != 0) {
-	if (!PyErr_Occurred())
-	    PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (python_msgobj_init() != 0) {
-	if (!PyErr_Occurred())
-	    PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    pModule = PyImport_ImportModule(bname);
-    if (pModule == NULL) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    pkg_free(dname_src);
-    pkg_free(bname_src);
-
-    pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
-    Py_DECREF(pModule);
-
-    /* pFunc is a new reference */
-
-    if (pFunc == NULL) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        Py_XDECREF(pFunc);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (!PyCallable_Check(pFunc)) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
-	python_handle_exception("mod_init");
-	Py_DECREF(format_exc_obj);
-        Py_XDECREF(pFunc);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-
-    pArgs = PyTuple_New(0);
-    if (pArgs == NULL) {
-	python_handle_exception("mod_init");
-        Py_DECREF(format_exc_obj);
-        Py_DECREF(pFunc);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    handler_obj = PyObject_CallObject(pFunc, pArgs);
-
-    Py_XDECREF(pFunc);
-    Py_XDECREF(pArgs);
-
-    if (handler_obj == Py_None) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
-	python_handle_exception("mod_init");
-        Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (PyErr_Occurred()) {
-        python_handle_exception("mod_init");
-        Py_XDECREF(handler_obj);
-        Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (handler_obj == NULL) {
-        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
-	python_handle_exception("mod_init");
-        Py_DECREF(format_exc_obj);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    myThreadState = PyThreadState_New(mainThreadState->interp);
-    PyEval_ReleaseLock();
-
-    return 0;
-}
+	char *dname_src, *bname_src;
+
+	int i;
+	PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
+	PyThreadState *mainThreadState;
+
+	dname_src = as_asciiz(&script_name);
+	bname_src = as_asciiz(&script_name);
+
+	if(dname_src==NULL || bname_src==NULL)
+	{
+		LM_ERR("no more pkg memory\n");
+		return -1;
+	}
+
+	dname = strdup(dirname(dname_src));
+	if (strlen(dname) == 0)
+		dname = ".";
+	bname = strdup(basename(bname_src));
+	i = strlen(bname);
+	if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
+		i -= 1;
+	if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
+		bname[i - 3] = '\0';
+	} else {
+		LM_ERR("%s: script_name doesn't look like a python script\n",
+				script_name.s);
+		return -1;
+	}
+
+	Py_Initialize();
+	PyEval_InitThreads();
+	mainThreadState = PyThreadState_Get();
+
+	format_exc_obj = InitTracebackModule();
+
+	if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
+	{
+		Py_XDECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	sys_path = PySys_GetObject("path");
+	/* PySys_GetObject doesn't pass reference! No need to DEREF */
+	if (sys_path == NULL) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pDir = PyString_FromString(dname);
+	if (pDir == NULL) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	PyList_Insert(sys_path, 0, pDir);
+	Py_DECREF(pDir);
+
+	if (ap_init_modules() != 0) {
+		if (!PyErr_Occurred())
+			PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (python_msgobj_init() != 0) {
+		if (!PyErr_Occurred())
+			PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pModule = PyImport_ImportModule(bname);
+	if (pModule == NULL) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pkg_free(dname_src);
+	pkg_free(bname_src);
+
+	pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
+	Py_DECREF(pModule);
+
+	/* pFunc is a new reference */
+
+	if (pFunc == NULL) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		Py_XDECREF(pFunc);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (!PyCallable_Check(pFunc)) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		Py_XDECREF(pFunc);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+
+	pArgs = PyTuple_New(0);
+	if (pArgs == NULL) {
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		Py_DECREF(pFunc);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	handler_obj = PyObject_CallObject(pFunc, pArgs);
 
-static int child_init(int rank)
-{
-    PyObject *pFunc, *pArgs, *pValue, *pResult;
-    int rval;
-    char *classname;
-
-    PyEval_AcquireLock();
-    PyThreadState_Swap(myThreadState);
-
-    // get instance class name
-    classname = get_instance_class_name(handler_obj);
-    if (classname == NULL)
-    {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
-	python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-	PyThreadState_Swap(NULL);
+	Py_XDECREF(pFunc);
+	Py_XDECREF(pArgs);
+
+	if (handler_obj == Py_None) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (PyErr_Occurred()) {
+		python_handle_exception("mod_init");
+		Py_XDECREF(handler_obj);
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (handler_obj == NULL) {
+		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
+		python_handle_exception("mod_init");
+		Py_DECREF(format_exc_obj);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	myThreadState = PyThreadState_New(mainThreadState->interp);
 	PyEval_ReleaseLock();
-	return -1;
-    }
 
-    pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
+	return 0;
+}
 
-    if (pFunc == NULL) {
-	python_handle_exception("child_init");
-	Py_XDECREF(pFunc);
-	Py_DECREF(format_exc_obj);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (!PyCallable_Check(pFunc)) {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
-	python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-	Py_XDECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    pArgs = PyTuple_New(1);
-    if (pArgs == NULL) {
-	python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-        Py_DECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    pValue = PyInt_FromLong((long)rank);
-    if (pValue == NULL) {
-	python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-        Py_DECREF(pArgs);
-        Py_DECREF(pFunc);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-    PyTuple_SetItem(pArgs, 0, pValue);
-    /* pValue has been stolen */
-
-    pResult = PyObject_CallObject(pFunc, pArgs);
-    Py_DECREF(pFunc);
-    Py_DECREF(pArgs);
-
-    
-
-
-    if (PyErr_Occurred()) {
-        python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-        Py_XDECREF(pResult);
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (pResult == NULL) {
-        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
-        PyThreadState_Swap(NULL);
-        PyEval_ReleaseLock();
-        return -1;
-    }
-
-    if (!PyInt_Check(pResult))
-    {
-	if (!PyErr_Occurred())
-	    PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
-	python_handle_exception("child_init");
-	Py_DECREF(format_exc_obj);
-	Py_XDECREF(pResult);
+static int child_init(int rank)
+{
+	PyObject *pFunc, *pArgs, *pValue, *pResult;
+	int rval;
+	char *classname;
+
+	PyEval_AcquireLock();
+	PyThreadState_Swap(myThreadState);
+
+	// get instance class name
+	classname = get_instance_class_name(handler_obj);
+	if (classname == NULL)
+	{
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
+
+	if (pFunc == NULL) {
+		python_handle_exception("child_init");
+		Py_XDECREF(pFunc);
+		Py_DECREF(format_exc_obj);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (!PyCallable_Check(pFunc)) {
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		Py_XDECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pArgs = PyTuple_New(1);
+	if (pArgs == NULL) {
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		Py_DECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	pValue = PyInt_FromLong((long)rank);
+	if (pValue == NULL) {
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		Py_DECREF(pArgs);
+		Py_DECREF(pFunc);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+	PyTuple_SetItem(pArgs, 0, pValue);
+	/* pValue has been stolen */
+
+	pResult = PyObject_CallObject(pFunc, pArgs);
+	Py_DECREF(pFunc);
+	Py_DECREF(pArgs);
+
+
+
+
+	if (PyErr_Occurred()) {
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		Py_XDECREF(pResult);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (pResult == NULL) {
+		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+	if (!PyInt_Check(pResult))
+	{
+		if (!PyErr_Occurred())
+			PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
+		python_handle_exception("child_init");
+		Py_DECREF(format_exc_obj);
+		Py_XDECREF(pResult);
+		PyThreadState_Swap(NULL);
+		PyEval_ReleaseLock();
+		return -1;
+	}
+
+
+	rval = PyInt_AsLong(pResult);
+	Py_DECREF(pResult);
 	PyThreadState_Swap(NULL);
 	PyEval_ReleaseLock();
-	return -1;
-    }
-
-
-    rval = PyInt_AsLong(pResult);
-    Py_DECREF(pResult);
-    PyThreadState_Swap(NULL);
-    PyEval_ReleaseLock();
 
-    return rval;
+	return rval;
 }
 
 static void mod_destroy(void)
 {
-    if (dname)
-	free(dname);	// dname was strdup'ed
-    if (bname)
-	free(bname);	// bname was strdup'ed
-
-    destroy_mod_Core();
-    destroy_mod_Ranks();
-    destroy_mod_Logger();
-    destroy_mod_Router();
+	if (dname)
+		free(dname);	// dname was strdup'ed
+	if (bname)
+		free(bname);	// bname was strdup'ed
+
+	destroy_mod_Core();
+	destroy_mod_Ranks();
+	destroy_mod_Logger();
+	destroy_mod_Router();
 }

+ 379 - 379
modules/app_python/python_msgobj.c

@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  *
  * This file is part of Kamailio, a free SIP server.
@@ -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>
 
@@ -30,8 +30,8 @@
 #include "structmember.h"
 
 typedef struct {
-    PyObject_HEAD
-    struct sip_msg *msg;
+	PyObject_HEAD
+	struct sip_msg *msg;
 } msgobject;
 
 static PyTypeObject MSGtype;
@@ -40,453 +40,453 @@ static PyTypeObject MSGtype;
 
 msgobject *newmsgobject(struct sip_msg *msg)
 {
-    msgobject *msgp;
+	msgobject *msgp;
 
-    msgp = PyObject_New(msgobject, &MSGtype);
-    if (msgp == NULL)
-        return NULL;
+	msgp = PyObject_New(msgobject, &MSGtype);
+	if (msgp == NULL)
+		return NULL;
 
-    msgp->msg = msg;
-    return msgp;
+	msgp->msg = msg;
+	return msgp;
 }
 
 void msg_invalidate(msgobject *self)
 {
-    self->msg = NULL;
+	self->msg = NULL;
 }
 
 static void msg_dealloc(msgobject *msgp)
 {
-    PyObject_Del(msgp);
+	PyObject_Del(msgp);
 }
 
 static PyObject *msg_copy(msgobject *self)
 {
-    msgobject *msgp;
+	msgobject *msgp;
 
-    if ((msgp = newmsgobject(self->msg)) == NULL)
-        return NULL;
+	if ((msgp = newmsgobject(self->msg)) == NULL)
+		return NULL;
 
-    return (PyObject *)msgp;
+	return (PyObject *)msgp;
 }
 
 static PyObject *msg_rewrite_ruri(msgobject *self, PyObject *args)
 {
-    char *ruri;
-    struct action act;
-    struct run_act_ctx ra_ctx;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if ((self->msg->first_line).type != SIP_REQUEST) {
-        PyErr_SetString(PyExc_RuntimeError, "Not a request message - rewrite is not possible.\n");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if(!PyArg_ParseTuple(args, "s:rewrite_ruri", &ruri))
-        return NULL;
-
-    memset(&act, '\0', sizeof(act));
-
-    act.type = SET_URI_T;
-    act.val[0].type = STRING_ST;
-    act.val[0].u.str.s = ruri;
-    act.val[0].u.str.len = strlen(ruri);
-
-    init_run_actions_ctx(&ra_ctx);
-    if (do_action(&ra_ctx, &act, self->msg) < 0) {
-        LM_ERR("Error in do_action\n");
-    }
-
-    Py_INCREF(Py_None);
-    return Py_None;
+	char *ruri;
+	struct action act;
+	struct run_act_ctx ra_ctx;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if ((self->msg->first_line).type != SIP_REQUEST) {
+		PyErr_SetString(PyExc_RuntimeError, "Not a request message - rewrite is not possible.\n");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if(!PyArg_ParseTuple(args, "s:rewrite_ruri", &ruri))
+		return NULL;
+
+	memset(&act, '\0', sizeof(act));
+
+	act.type = SET_URI_T;
+	act.val[0].type = STRING_ST;
+	act.val[0].u.str.s = ruri;
+	act.val[0].u.str.len = strlen(ruri);
+
+	init_run_actions_ctx(&ra_ctx);
+	if (do_action(&ra_ctx, &act, self->msg) < 0) {
+		LM_ERR("Error in do_action\n");
+	}
+
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 static PyObject *msg_set_dst_uri(msgobject *self, PyObject *args)
 {
-    str ruri;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if ((self->msg->first_line).type != SIP_REQUEST) {
-        PyErr_SetString(PyExc_RuntimeError, "Not a request message - set destination is not possible.\n");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if(!PyArg_ParseTuple(args, "s:set_dst_uri", &ruri.s))
-        return NULL;
-
-    ruri.len = strlen(ruri.s);
-
-    if (set_dst_uri(self->msg, &ruri) < 0) {
-        LM_ERR("Error in set_dst_uri\n");
-        PyErr_SetString(PyExc_RuntimeError, "Error in set_dst_uri\n");
-    }
-    /* dst_uri changes, so it makes sense to re-use the current uri for
-       forking */
-    ruri_mark_new(); /* re-use uri for serial forking */
-
-    Py_INCREF(Py_None);
-    return Py_None;
+	str ruri;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if ((self->msg->first_line).type != SIP_REQUEST) {
+		PyErr_SetString(PyExc_RuntimeError, "Not a request message - set destination is not possible.\n");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if(!PyArg_ParseTuple(args, "s:set_dst_uri", &ruri.s))
+		return NULL;
+
+	ruri.len = strlen(ruri.s);
+
+	if (set_dst_uri(self->msg, &ruri) < 0) {
+		LM_ERR("Error in set_dst_uri\n");
+		PyErr_SetString(PyExc_RuntimeError, "Error in set_dst_uri\n");
+	}
+	/* dst_uri changes, so it makes sense to re-use the current uri for
+	 * forking */
+	ruri_mark_new(); /* re-use uri for serial forking */
+
+	Py_INCREF(Py_None);
+	return Py_None;
 }
 
 static PyObject *msg_getHeader(msgobject *self, PyObject *args)
 {
-    struct hdr_field *hf;
-    str hname, *hbody;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if(!PyArg_ParseTuple(args, "s:getHeader", &hname.s))
-        return NULL;
-    hname.len = strlen(hname.s);
-
-    parse_headers(self->msg, ~0, 0);
-    hbody = NULL;
-    for (hf = self->msg->headers; hf != NULL; hf = hf->next) {
-        if (hname.len == hf->name.len &&
-          strncasecmp(hname.s, hf->name.s, hname.len) == 0) {
-            hbody = &(hf->body);
-            break;
-        }
-    }
-
-    if (hbody == NULL) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    return PyString_FromStringAndSize(hbody->s, hbody->len);
+	struct hdr_field *hf;
+	str hname, *hbody;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if(!PyArg_ParseTuple(args, "s:getHeader", &hname.s))
+		return NULL;
+	hname.len = strlen(hname.s);
+
+	parse_headers(self->msg, ~0, 0);
+	hbody = NULL;
+	for (hf = self->msg->headers; hf != NULL; hf = hf->next) {
+		if (hname.len == hf->name.len &&
+				strncasecmp(hname.s, hf->name.s, hname.len) == 0) {
+			hbody = &(hf->body);
+			break;
+		}
+	}
+
+	if (hbody == NULL) {
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	return PyString_FromStringAndSize(hbody->s, hbody->len);
 }
 
 static PyObject *msg_call_function(msgobject *self, PyObject *args)
 {
-    int i, rval;
-    char *fname, *arg1, *arg2;
-    sr31_cmd_export_t* fexport;
-    struct action *act;
-    struct run_act_ctx ra_ctx;
-    unsigned mod_ver;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    i = PySequence_Size(args);
-    if (i < 1 || i > 3) {
-        PyErr_SetString(PyExc_RuntimeError, "call_function() should " \
-          "have from 1 to 3 arguments");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2))
-        return NULL;
-
-    fexport = find_export_record(fname, i - 1, 0, &mod_ver);
-    if (fexport == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "no such function");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */,
-                    MODEXP_ST, fexport, /* function */
-                    NUMBER_ST, 2,       /* parameter number */
-                    STRING_ST, arg1,    /* param. 1 */
-                    STRING_ST, arg2     /* param. 2 */
-                   );
-
-    if (act == NULL) {
-        PyErr_SetString(PyExc_RuntimeError,
-          "action structure could not be created");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if (fexport->fixup != NULL) {
-        if (i >= 3) {
-            rval = fexport->fixup(&(act->val[3].u.data), 2);
-            if (rval < 0) {
-                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
-                Py_INCREF(Py_None);
-                return Py_None;
-            }
-            act->val[3].type = MODFIXUP_ST;
-        }
-        if (i >= 2) {
-            rval = fexport->fixup(&(act->val[2].u.data), 1);
-            if (rval < 0) {
-                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
-                Py_INCREF(Py_None);
-                return Py_None;
-            }
-            act->val[2].type = MODFIXUP_ST;
-        }
-        if (i == 1) {
-            rval = fexport->fixup(0, 0);
-            if (rval < 0) {
-                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
-                Py_INCREF(Py_None);
-                return Py_None;
-            }
-        }
-    }
-
-    init_run_actions_ctx(&ra_ctx);
-    rval = do_action(&ra_ctx, act, self->msg);
-
-    if ((act->val[3].type == MODFIXUP_ST) && (act->val[3].u.data)) {
-       pkg_free(act->val[3].u.data);
-    }
-
-    if ((act->val[2].type == MODFIXUP_ST) && (act->val[2].u.data)) {
-        pkg_free(act->val[2].u.data);
-    }
-
-    pkg_free(act);
-
-    return PyInt_FromLong(rval);
+	int i, rval;
+	char *fname, *arg1, *arg2;
+	sr31_cmd_export_t* fexport;
+	struct action *act;
+	struct run_act_ctx ra_ctx;
+	unsigned mod_ver;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	i = PySequence_Size(args);
+	if (i < 1 || i > 3) {
+		PyErr_SetString(PyExc_RuntimeError, "call_function() should " \
+				"have from 1 to 3 arguments");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2))
+		return NULL;
+
+	fexport = find_export_record(fname, i - 1, 0, &mod_ver);
+	if (fexport == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "no such function");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */,
+			MODEXP_ST, fexport, /* function */
+			NUMBER_ST, 2,       /* parameter number */
+			STRING_ST, arg1,    /* param. 1 */
+			STRING_ST, arg2     /* param. 2 */
+			);
+
+	if (act == NULL) {
+		PyErr_SetString(PyExc_RuntimeError,
+				"action structure could not be created");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if (fexport->fixup != NULL) {
+		if (i >= 3) {
+			rval = fexport->fixup(&(act->val[3].u.data), 2);
+			if (rval < 0) {
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
+				Py_INCREF(Py_None);
+				return Py_None;
+			}
+			act->val[3].type = MODFIXUP_ST;
+		}
+		if (i >= 2) {
+			rval = fexport->fixup(&(act->val[2].u.data), 1);
+			if (rval < 0) {
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
+				Py_INCREF(Py_None);
+				return Py_None;
+			}
+			act->val[2].type = MODFIXUP_ST;
+		}
+		if (i == 1) {
+			rval = fexport->fixup(0, 0);
+			if (rval < 0) {
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
+				Py_INCREF(Py_None);
+				return Py_None;
+			}
+		}
+	}
+
+	init_run_actions_ctx(&ra_ctx);
+	rval = do_action(&ra_ctx, act, self->msg);
+
+	if ((act->val[3].type == MODFIXUP_ST) && (act->val[3].u.data)) {
+		pkg_free(act->val[3].u.data);
+	}
+
+	if ((act->val[2].type == MODFIXUP_ST) && (act->val[2].u.data)) {
+		pkg_free(act->val[2].u.data);
+	}
+
+	pkg_free(act);
+
+	return PyInt_FromLong(rval);
 }
 
 PyDoc_STRVAR(copy_doc,
-"copy() -> msg object\n\
-\n\
-Return a copy (``clone'') of the msg object.");
+		"copy() -> msg object\n\
+		\n\
+		Return a copy (``clone'') of the msg object.");
 
 static PyMethodDef msg_methods[] = {
-    {"copy",          (PyCFunction)msg_copy,          METH_NOARGS,		copy_doc},
-    {"rewrite_ruri",  (PyCFunction)msg_rewrite_ruri,  METH_VARARGS,		"Rewrite Request-URI."},
-    {"set_dst_uri",   (PyCFunction)msg_set_dst_uri,   METH_VARARGS,		"Set destination URI."},
-    {"getHeader",     (PyCFunction)msg_getHeader,     METH_VARARGS,		"Get SIP header field by name."},
-    {"call_function", (PyCFunction)msg_call_function, METH_VARARGS,		"Invoke function exported by the other module."},
-    {NULL, NULL, 0, NULL} /* sentinel */
+	{"copy",          (PyCFunction)msg_copy,          METH_NOARGS,		copy_doc},
+	{"rewrite_ruri",  (PyCFunction)msg_rewrite_ruri,  METH_VARARGS,		"Rewrite Request-URI."},
+	{"set_dst_uri",   (PyCFunction)msg_set_dst_uri,   METH_VARARGS,		"Set destination URI."},
+	{"getHeader",     (PyCFunction)msg_getHeader,     METH_VARARGS,		"Get SIP header field by name."},
+	{"call_function", (PyCFunction)msg_call_function, METH_VARARGS,		"Invoke function exported by the other module."},
+	{NULL, NULL, 0, NULL} /* sentinel */
 };
 
 static PyObject *msg_getType(msgobject *self, PyObject *unused)
 {
-    const char *rval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    switch ((self->msg->first_line).type)
-    {
-	case SIP_REQUEST:
-	    rval = "SIP_REQUEST";
-	    break;
-
-	case SIP_REPLY:
-	    rval = "SIP_REPLY";
-	    break;
-
-	default:
-            rval = "SIP_INVALID";
-            break;
-    }
-
-    return PyString_FromString(rval);
+	const char *rval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	switch ((self->msg->first_line).type)
+	{
+		case SIP_REQUEST:
+			rval = "SIP_REQUEST";
+			break;
+
+		case SIP_REPLY:
+			rval = "SIP_REPLY";
+			break;
+
+		default:
+			rval = "SIP_INVALID";
+			break;
+	}
+
+	return PyString_FromString(rval);
 }
 
 static PyObject *msg_getMethod(msgobject *self, PyObject *unused)
 {
-    str *rval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if ((self->msg->first_line).type != SIP_REQUEST) {
-        PyErr_SetString(PyExc_RuntimeError, "Not a request message - no method available.\n");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-    rval = &((self->msg->first_line).u.request.method);
-    return PyString_FromStringAndSize(rval->s, rval->len);
+	str *rval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if ((self->msg->first_line).type != SIP_REQUEST) {
+		PyErr_SetString(PyExc_RuntimeError, "Not a request message - no method available.\n");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+	rval = &((self->msg->first_line).u.request.method);
+	return PyString_FromStringAndSize(rval->s, rval->len);
 }
 
 static PyObject *msg_getStatus(msgobject *self, PyObject *unused)
 {
-    str *rval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if ((self->msg->first_line).type != SIP_REPLY) {
-        PyErr_SetString(PyExc_RuntimeError, "Not a non-reply message - no status available.\n");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    rval = &((self->msg->first_line).u.reply.status);
-    return PyString_FromStringAndSize(rval->s, rval->len);
+	str *rval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if ((self->msg->first_line).type != SIP_REPLY) {
+		PyErr_SetString(PyExc_RuntimeError, "Not a non-reply message - no status available.\n");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	rval = &((self->msg->first_line).u.reply.status);
+	return PyString_FromStringAndSize(rval->s, rval->len);
 }
 
 static PyObject *msg_getRURI(msgobject *self, PyObject *unused)
 {
-    str *rval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    if ((self->msg->first_line).type != SIP_REQUEST) {
-        PyErr_SetString(PyExc_RuntimeError, "Not a request message - RURI is not available.\n");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    rval = &((self->msg->first_line).u.request.uri);
-    return PyString_FromStringAndSize(rval->s, rval->len);
+	str *rval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	if ((self->msg->first_line).type != SIP_REQUEST) {
+		PyErr_SetString(PyExc_RuntimeError, "Not a request message - RURI is not available.\n");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	rval = &((self->msg->first_line).u.request.uri);
+	return PyString_FromStringAndSize(rval->s, rval->len);
 }
 
 static PyObject *msg_get_src_address(msgobject *self, PyObject *unused)
 {
-    PyObject *src_ip, *src_port, *pyRval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    src_ip = PyString_FromString(ip_addr2a(&self->msg->rcv.src_ip));
-    if (src_ip == NULL) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    src_port = PyInt_FromLong(self->msg->rcv.src_port);
-    if (src_port == NULL) {
-        Py_DECREF(src_ip);
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    pyRval = PyTuple_Pack(2, src_ip, src_port);
-    Py_DECREF(src_ip);
-    Py_DECREF(src_port);
-    if (pyRval == NULL) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    return pyRval;
+	PyObject *src_ip, *src_port, *pyRval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	src_ip = PyString_FromString(ip_addr2a(&self->msg->rcv.src_ip));
+	if (src_ip == NULL) {
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	src_port = PyInt_FromLong(self->msg->rcv.src_port);
+	if (src_port == NULL) {
+		Py_DECREF(src_ip);
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	pyRval = PyTuple_Pack(2, src_ip, src_port);
+	Py_DECREF(src_ip);
+	Py_DECREF(src_port);
+	if (pyRval == NULL) {
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	return pyRval;
 }
 
 static PyObject *msg_get_dst_address(msgobject *self, PyObject *unused)
 {
-    PyObject *dst_ip, *dst_port, *pyRval;
-
-    if (self->msg == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    dst_ip = PyString_FromString(ip_addr2a(&self->msg->rcv.dst_ip));
-    if (dst_ip == NULL) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    dst_port = PyInt_FromLong(self->msg->rcv.dst_port);
-    if (dst_port == NULL) {
-        Py_DECREF(dst_ip);
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    pyRval = PyTuple_Pack(2, dst_ip, dst_port);
-    Py_DECREF(dst_ip);
-    Py_DECREF(dst_port);
-    if (pyRval == NULL) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-
-    return pyRval;
+	PyObject *dst_ip, *dst_port, *pyRval;
+
+	if (self->msg == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	dst_ip = PyString_FromString(ip_addr2a(&self->msg->rcv.dst_ip));
+	if (dst_ip == NULL) {
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	dst_port = PyInt_FromLong(self->msg->rcv.dst_port);
+	if (dst_port == NULL) {
+		Py_DECREF(dst_ip);
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	pyRval = PyTuple_Pack(2, dst_ip, dst_port);
+	Py_DECREF(dst_ip);
+	Py_DECREF(dst_port);
+	if (pyRval == NULL) {
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+
+	return pyRval;
 }
 
 static PyGetSetDef msg_getseters[] = {
-    {"Type",		(getter)msg_getType, NULL, NULL,		"Get message type - \"SIP_REQUEST\" or \"SIP_REPLY\"."},
-    {"Method",		(getter)msg_getMethod, NULL, NULL,		"Get SIP method name."},
-    {"Status",		(getter)msg_getStatus, NULL, NULL,		"Get SIP status code string."},
-    {"RURI",		(getter)msg_getRURI, NULL, NULL,		"Get SIP Request-URI."},
-    {"src_address",	(getter)msg_get_src_address, NULL, NULL,	"Get (IP, port) tuple representing source address of the message."},
-    {"dst_address",	(getter)msg_get_dst_address, NULL, NULL,	"Get (IP, port) tuple representing destination address of the message."},
-    {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
+	{"Type",		(getter)msg_getType, NULL, NULL,		"Get message type - \"SIP_REQUEST\" or \"SIP_REPLY\"."},
+	{"Method",		(getter)msg_getMethod, NULL, NULL,		"Get SIP method name."},
+	{"Status",		(getter)msg_getStatus, NULL, NULL,		"Get SIP status code string."},
+	{"RURI",		(getter)msg_getRURI, NULL, NULL,		"Get SIP Request-URI."},
+	{"src_address",	(getter)msg_get_src_address, NULL, NULL,	"Get (IP, port) tuple representing source address of the message."},
+	{"dst_address",	(getter)msg_get_dst_address, NULL, NULL,	"Get (IP, port) tuple representing destination address of the message."},
+	{NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
 };
 
 static PyTypeObject MSGtype = {
 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6
-    PyVarObject_HEAD_INIT(NULL, 0)
+	PyVarObject_HEAD_INIT(NULL, 0)
 #else
-    PyObject_HEAD_INIT(NULL)
-    0,                        /*ob_size*/
+	PyObject_HEAD_INIT(NULL)
+	0,                        /*ob_size*/
 #endif
-    "Router.msg",             /*tp_name*/
-    sizeof(msgobject),        /*tp_size*/
-    0,                        /*tp_itemsize*/
-    /* methods */
-    (destructor)msg_dealloc,  /*tp_dealloc*/
-    0,                        /*tp_print*/
-    0,                        /*tp_getattr*/
-    0,                        /*tp_setattr*/
-    0,                        /*tp_compare*/
-    0,                        /*tp_repr*/
-    0,                        /*tp_as_number*/
-    0,                        /*tp_as_sequence*/
-    0,                        /*tp_as_mapping*/
-    0,                        /*tp_hash*/
-    0,                        /*tp_call*/
-    0,                        /*tp_str*/
-    0,                        /*tp_getattro*/
-    0,                        /*tp_setattro*/
-    0,                        /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT,       /*tp_flags*/
-    0,                        /*tp_doc*/
-    0,                        /*tp_traverse*/
-    0,                        /*tp_clear*/
-    0,                        /*tp_richcompare*/
-    0,                        /*tp_weaklistoffset*/
-    0,                        /*tp_iter*/
-    0,                        /*tp_iternext*/
-    msg_methods,              /*tp_methods*/
-    0,                        /*tp_members*/
-    msg_getseters,            /*tp_getset*/
+	"Router.msg",             /*tp_name*/
+	sizeof(msgobject),        /*tp_size*/
+	0,                        /*tp_itemsize*/
+	/* methods */
+	(destructor)msg_dealloc,  /*tp_dealloc*/
+	0,                        /*tp_print*/
+	0,                        /*tp_getattr*/
+	0,                        /*tp_setattr*/
+	0,                        /*tp_compare*/
+	0,                        /*tp_repr*/
+	0,                        /*tp_as_number*/
+	0,                        /*tp_as_sequence*/
+	0,                        /*tp_as_mapping*/
+	0,                        /*tp_hash*/
+	0,                        /*tp_call*/
+	0,                        /*tp_str*/
+	0,                        /*tp_getattro*/
+	0,                        /*tp_setattro*/
+	0,                        /*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT,       /*tp_flags*/
+	0,                        /*tp_doc*/
+	0,                        /*tp_traverse*/
+	0,                        /*tp_clear*/
+	0,                        /*tp_richcompare*/
+	0,                        /*tp_weaklistoffset*/
+	0,                        /*tp_iter*/
+	0,                        /*tp_iternext*/
+	msg_methods,              /*tp_methods*/
+	0,                        /*tp_members*/
+	msg_getseters,            /*tp_getset*/
 };
 
 int python_msgobj_init(void)
 {
-    MSGtype.ob_type = &PyType_Type;
-    if (PyType_Ready(&MSGtype) < 0)
-        return -1;
-    return 0;
+	MSGtype.ob_type = &PyType_Type;
+	if (PyType_Ready(&MSGtype) < 0)
+		return -1;
+	return 0;
 }

+ 182 - 182
modules/app_python/python_support.c

@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  *
  * This file is part of Kamailio, a free SIP server.
@@ -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>
 #include <stdio.h>
@@ -31,223 +31,223 @@
 
 void python_handle_exception(const char *fmt, ...)
 {
-    PyObject *pResult;
-    const char *msg;
-    char *buf;
-    size_t buflen, msglen;
-    PyObject *exception, *v, *tb, *args;
-    PyObject *line;
-    int i;
-    char *srcbuf;
-
-    // We don't want to generate traceback when no errors occured
-    if (!PyErr_Occurred())
-	return;
-
-    if (fmt == NULL)
-	srcbuf = NULL;
-    else
-	srcbuf = make_message(fmt);
-
-    PyErr_Fetch(&exception, &v, &tb);
-    PyErr_Clear();
-    if (exception == NULL) {
-        LM_ERR("python_handle_exception(): Can't get traceback, PyErr_Fetch() has failed.\n");
-        return;
-    }
-
-    PyErr_NormalizeException(&exception, &v, &tb);
-    if (exception == NULL) {
-        LM_ERR("python_handle_exception(): Can't get traceback, PyErr_NormalizeException() has failed.\n");
-        return;
-    }
-
-    args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
-    Py_XDECREF(exception);
-    Py_XDECREF(v);
-    Py_XDECREF(tb);
-    if (args == NULL) {
-        LM_ERR("python_handle_exception(): Can't get traceback, PyTuple_Pack() has failed.\n");
-        return;
-    }
-
-    pResult = PyObject_CallObject(format_exc_obj, args);
-    Py_DECREF(args);
-    if (pResult == NULL) {
-        LM_ERR("python_handle_exception(): Can't get traceback, traceback.format_exception() has failed.\n");
-        return;
-    }
-
-    buflen = 1;
-    buf = (char *)pkg_realloc(NULL, buflen * sizeof(char *));
-    if (!buf)
-    {
-	LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
-	return;
-    }
-    memset(buf, 0, sizeof(char *));
-
-    for (i = 0; i < PySequence_Size(pResult); i++) {
-        line = PySequence_GetItem(pResult, i);
-        if (line == NULL) {
-            LM_ERR("python_handle_exception(): Can't get traceback, PySequence_GetItem() has failed.\n");
-            Py_DECREF(pResult);
-	    if (buf)
-		pkg_free(buf);
-            return;
-        }
+	PyObject *pResult;
+	const char *msg;
+	char *buf;
+	size_t buflen, msglen;
+	PyObject *exception, *v, *tb, *args;
+	PyObject *line;
+	int i;
+	char *srcbuf;
+
+	// We don't want to generate traceback when no errors occured
+	if (!PyErr_Occurred())
+		return;
+
+	if (fmt == NULL)
+		srcbuf = NULL;
+	else
+		srcbuf = make_message(fmt);
 
-        msg = PyString_AsString(line);
+	PyErr_Fetch(&exception, &v, &tb);
+	PyErr_Clear();
+	if (exception == NULL) {
+		LM_ERR("python_handle_exception(): Can't get traceback, PyErr_Fetch() has failed.\n");
+		return;
+	}
 
-        if (msg == NULL) {
-            LM_ERR("python_handle_exception(): Can't get traceback, PyString_AsString() has failed.\n");
-            Py_DECREF(line);
-            Py_DECREF(pResult);
-	    if (buf)
-		pkg_free(buf);
-            return;
-        }
+	PyErr_NormalizeException(&exception, &v, &tb);
+	if (exception == NULL) {
+		LM_ERR("python_handle_exception(): Can't get traceback, PyErr_NormalizeException() has failed.\n");
+		return;
+	}
 
-	msglen = strlen(msg);
-	buflen += ++msglen;
+	args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
+	Py_XDECREF(exception);
+	Py_XDECREF(v);
+	Py_XDECREF(tb);
+	if (args == NULL) {
+		LM_ERR("python_handle_exception(): Can't get traceback, PyTuple_Pack() has failed.\n");
+		return;
+	}
+
+	pResult = PyObject_CallObject(format_exc_obj, args);
+	Py_DECREF(args);
+	if (pResult == NULL) {
+		LM_ERR("python_handle_exception(): Can't get traceback, traceback.format_exception() has failed.\n");
+		return;
+	}
 
-	buf = (char *)pkg_realloc(buf, buflen * sizeof(char *));
+	buflen = 1;
+	buf = (char *)pkg_realloc(NULL, buflen * sizeof(char *));
 	if (!buf)
 	{
-	    LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
-	    Py_DECREF(line);
-	    Py_DECREF(pResult);
-	    return;
+		LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
+		return;
+	}
+	memset(buf, 0, sizeof(char *));
+
+	for (i = 0; i < PySequence_Size(pResult); i++) {
+		line = PySequence_GetItem(pResult, i);
+		if (line == NULL) {
+			LM_ERR("python_handle_exception(): Can't get traceback, PySequence_GetItem() has failed.\n");
+			Py_DECREF(pResult);
+			if (buf)
+				pkg_free(buf);
+			return;
+		}
+
+		msg = PyString_AsString(line);
+
+		if (msg == NULL) {
+			LM_ERR("python_handle_exception(): Can't get traceback, PyString_AsString() has failed.\n");
+			Py_DECREF(line);
+			Py_DECREF(pResult);
+			if (buf)
+				pkg_free(buf);
+			return;
+		}
+
+		msglen = strlen(msg);
+		buflen += ++msglen;
+
+		buf = (char *)pkg_realloc(buf, buflen * sizeof(char *));
+		if (!buf)
+		{
+			LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
+			Py_DECREF(line);
+			Py_DECREF(pResult);
+			return;
+		}
+
+		strncat(buf, msg, msglen >= buflen ? buflen-1 : msglen);
+
+		Py_DECREF(line);
 	}
 
-	strncat(buf, msg, msglen >= buflen ? buflen-1 : msglen);
-
-        Py_DECREF(line);
-    }
-
-    if (srcbuf == NULL)
-	LM_ERR("Unhandled exception in the Python code:\n%s", buf);
-    else
-	LM_ERR("%s: Unhandled exception in the Python code:\n%s", srcbuf, buf);
+	if (srcbuf == NULL)
+		LM_ERR("Unhandled exception in the Python code:\n%s", buf);
+	else
+		LM_ERR("%s: Unhandled exception in the Python code:\n%s", srcbuf, buf);
 
-    if (buf)
-	pkg_free(buf);
+	if (buf)
+		pkg_free(buf);
 
-    if (srcbuf)
-	pkg_free(srcbuf);
+	if (srcbuf)
+		pkg_free(srcbuf);
 
-    Py_DECREF(pResult);
+	Py_DECREF(pResult);
 }
 
 
 PyObject *InitTracebackModule()
 {
-    PyObject *pModule, *pTracebackObject;
-
-    pModule = PyImport_ImportModule("traceback");
-    if (pModule == NULL) {
-        LM_ERR("InitTracebackModule(): Cannot import module 'traceback'.\n");
-        return NULL;
-    }
-
-    pTracebackObject = PyObject_GetAttrString(pModule, "format_exception");
-    Py_DECREF(pModule);
-    if (pTracebackObject == NULL || !PyCallable_Check(pTracebackObject)) {
-        LM_ERR("InitTracebackModule(): AttributeError: 'module' object 'traceback' has no attribute 'format_exception'.\n");
-        Py_XDECREF(pTracebackObject);
-        return NULL;
-    }
-
-    return pTracebackObject;
+	PyObject *pModule, *pTracebackObject;
+
+	pModule = PyImport_ImportModule("traceback");
+	if (pModule == NULL) {
+		LM_ERR("InitTracebackModule(): Cannot import module 'traceback'.\n");
+		return NULL;
+	}
+
+	pTracebackObject = PyObject_GetAttrString(pModule, "format_exception");
+	Py_DECREF(pModule);
+	if (pTracebackObject == NULL || !PyCallable_Check(pTracebackObject)) {
+		LM_ERR("InitTracebackModule(): AttributeError: 'module' object 'traceback' has no attribute 'format_exception'.\n");
+		Py_XDECREF(pTracebackObject);
+		return NULL;
+	}
+
+	return pTracebackObject;
 }
 
 
 char *make_message(const char *fmt, ...)
 {
-    int n;
-    size_t size;
-    char *p, *np;
-    va_list ap;
-
-    size = 100;     /* Guess we need no more than 100 bytes. */
-    p = (char *)pkg_realloc(NULL, size * sizeof(char *));
-    if (!p)
-    {
-	LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_malloc() has failed: Not enough memory.\n", (unsigned long)(size * sizeof(char *)));
-	return NULL;
-    }
-    memset(p, 0, size * sizeof(char *));
-    
-    while (1)
-    {
-        va_start(ap, fmt);
-        n = vsnprintf(p, size, fmt, ap);
-        va_end(ap);
-
-        if (n > -1 && n < size)
-    	    return p;
-
-        if (n > -1)    /* glibc 2.1 */
-    	    size = n+1;
-        else           /* glibc 2.0 */
-    	    size *= 2;
-
-	np = (char *)pkg_realloc(p, size * sizeof(char *));
-        if (!np)
+	int n;
+	size_t size;
+	char *p, *np;
+	va_list ap;
+
+	size = 100;     /* Guess we need no more than 100 bytes. */
+	p = (char *)pkg_realloc(NULL, size * sizeof(char *));
+	if (!p)
 	{
-	    LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_realloc() has failed: Not enough memory.\n", (unsigned long)size * sizeof(char *));
-	    if (p)
-    		pkg_free(p);
-    	    return NULL;
-        }
-	else
-    	    p = np;
-    }
+		LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_malloc() has failed: Not enough memory.\n", (unsigned long)(size * sizeof(char *)));
+		return NULL;
+	}
+	memset(p, 0, size * sizeof(char *));
+
+	while (1)
+	{
+		va_start(ap, fmt);
+		n = vsnprintf(p, size, fmt, ap);
+		va_end(ap);
+
+		if (n > -1 && n < size)
+			return p;
+
+		if (n > -1)    /* glibc 2.1 */
+			size = n+1;
+		else           /* glibc 2.0 */
+			size *= 2;
+
+		np = (char *)pkg_realloc(p, size * sizeof(char *));
+		if (!np)
+		{
+			LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_realloc() has failed: Not enough memory.\n", (unsigned long)size * sizeof(char *));
+			if (p)
+				pkg_free(p);
+			return NULL;
+		}
+		else
+			p = np;
+	}
 
-    return NULL;	// shall not happened, but who knows ;)
+	return NULL;	// shall not happened, but who knows ;)
 }
 
 char *get_class_name(PyObject *y)
 {
-    PyObject *p;
-    char *name;
+	PyObject *p;
+	char *name;
 
-    p = PyObject_GetAttrString(y, "__name__");
-    if (p == NULL || p == Py_None)
-    {
-	Py_XDECREF(p);
-	return NULL;
-    }
+	p = PyObject_GetAttrString(y, "__name__");
+	if (p == NULL || p == Py_None)
+	{
+		Py_XDECREF(p);
+		return NULL;
+	}
 
-    name = PyString_AsString(p);
-    Py_XDECREF(p);
+	name = PyString_AsString(p);
+	Py_XDECREF(p);
 
-    return name;
+	return name;
 }
 
 
 char *get_instance_class_name(PyObject *y)
 {
-    PyObject *p, *n;
-    char *name;
+	PyObject *p, *n;
+	char *name;
 
-    n = PyObject_GetAttrString(y, "__class__");
-    if (n == NULL || n == Py_None)
-    {
-	Py_XDECREF(n);
-	return NULL;
-    }
+	n = PyObject_GetAttrString(y, "__class__");
+	if (n == NULL || n == Py_None)
+	{
+		Py_XDECREF(n);
+		return NULL;
+	}
 
-    p = PyObject_GetAttrString(n, "__name__");
-    if (p == NULL || p == Py_None)
-    {
-	Py_XDECREF(p);
-	return NULL;
-    }
+	p = PyObject_GetAttrString(n, "__name__");
+	if (p == NULL || p == Py_None)
+	{
+		Py_XDECREF(p);
+		return NULL;
+	}
 
-    name = PyString_AsString(p);
-    Py_XDECREF(p);
-    Py_XDECREF(n);
+	name = PyString_AsString(p);
+	Py_XDECREF(p);
+	Py_XDECREF(n);
 
-    return name;
+	return name;
 }