[sr-dev] git:master: app_python: better printing stacktrace

Ovidiu Sas osas at voipembedded.com
Tue Dec 4 04:41:21 CET 2012


Module: sip-router
Branch: master
Commit: 97fe5fec90999491d455b10b1fea7529e786719b
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=97fe5fec90999491d455b10b1fea7529e786719b

Author: Konstantin M <evilzluk at gmail.com>
Committer: Ovidiu Sas <osas at voipembedded.com>
Date:   Mon Dec  3 22:35:21 2012 -0500

app_python: better printing stacktrace

---

 modules/app_python/TestCase_Traceback.py |   46 ++++++++++++++++++++++++++++++
 modules/app_python/python_support.c      |   41 +++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/modules/app_python/TestCase_Traceback.py b/modules/app_python/TestCase_Traceback.py
new file mode 100644
index 0000000..6a673a0
--- /dev/null
+++ b/modules/app_python/TestCase_Traceback.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+
+"""
+    Script for traceback test.
+    2012.12.03: Created by: Konstantin M. <evilzluk at gmail.com>
+"""
+
+import pprint
+
+class Loggers:
+
+    def __init__(self):
+	pass
+
+    def __del__(self):
+	pass
+
+    def child_init(self, y):
+	return 0
+
+    def BuggyCode_lvl5(self, a):
+	a / 0
+
+    def BuggyCode_lvl4(self, a):
+	return self.BuggyCode_lvl5(a)
+
+    def BuggyCode_lvl3(self, a):
+	return self.BuggyCode_lvl4(a)
+
+    def BuggyCode_lvl2(self, a):
+	return self.BuggyCode_lvl3(a)
+
+    def BuggyCode(self, a, b=None):
+	return self.BuggyCode_lvl2(a)
+
+
+
+def mod_init():
+    return Loggers()
+
+
+
+if __name__ != "__main__":
+    import Router
+else:
+    mod_init().BuggyCode(0)
diff --git a/modules/app_python/python_support.c b/modules/app_python/python_support.c
index e0ac4eb..01b290b 100644
--- a/modules/app_python/python_support.c
+++ b/modules/app_python/python_support.c
@@ -24,6 +24,8 @@
 #include <stdio.h>
 
 #include "../../dprint.h"
+#include "../../mem/mem.h"
+
 #include "python_mod.h"
 
 void
@@ -31,11 +33,12 @@ python_handle_exception(const char *fname)
 {
     PyObject *pResult;
     const char *msg;
+    char *buf;
+    size_t buflen;
     PyObject *exception, *v, *tb, *args;
     PyObject *line;
     int i;
 
-    LM_ERR("%s: Unhandled exception in the Python code:\n", fname);
     PyErr_Fetch(&exception, &v, &tb);
     PyErr_Clear();
     if (exception == NULL) {
@@ -61,22 +64,56 @@ python_handle_exception(const char *fname)
         LM_ERR("can't get traceback, traceback.format_exception() has failed\n");
         return;
     }
+
+    buflen = 1;
+    buf = (char *)pkg_malloc(buflen * sizeof(char *));
+    if (!buf)
+    {
+	LM_ERR("python_handle_exception(): Not enough memory\n");
+	return;
+    }
+    memset(&buf, 0, buflen * sizeof(char *));
+
     for (i = 0; i < PySequence_Size(pResult); i++) {
         line = PySequence_GetItem(pResult, i);
         if (line == NULL) {
             LM_ERR("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("can't get traceback, PyString_AsString() has failed\n");
             Py_DECREF(line);
             Py_DECREF(pResult);
+	    if (buf)
+		pkg_free(buf);
             return;
         }
-        LM_ERR("\t%s", msg);
+
+	buflen += strlen(msg);
+	buf = (char *)pkg_realloc(buf, (buflen + 1) * sizeof(char *));
+	if (!buf)
+	{
+	    LM_ERR("python_handle_exception(): Not enough memory\n");
+	    Py_DECREF(line);
+	    Py_DECREF(pResult);
+	    return;
+	}
+
+	strcat(buf, msg);
+
         Py_DECREF(line);
     }
+
+    LM_ERR("%s: Unhandled exception in the Python code:\n%s", fname, buf);
+
+    if (buf)
+	pkg_free(buf);
+
     Py_DECREF(pResult);
 }




More information about the sr-dev mailing list