python_support.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include "../../dprint.h"
  25. #include "../../mem/mem.h"
  26. #include "app_python_mod.h"
  27. #include "python_support.h"
  28. void python_handle_exception(const char *fmt, ...)
  29. {
  30. PyObject *pResult;
  31. const char *msg;
  32. char *buf;
  33. size_t buflen, msglen;
  34. PyObject *exception, *v, *tb, *args;
  35. PyObject *line;
  36. int i;
  37. char *srcbuf;
  38. int exc_exit = 0;
  39. // We don't want to generate traceback when no errors occurred
  40. if (!PyErr_Occurred())
  41. return;
  42. if (fmt == NULL)
  43. srcbuf = NULL;
  44. else
  45. srcbuf = make_message(fmt);
  46. PyErr_Fetch(&exception, &v, &tb);
  47. PyErr_Clear();
  48. if (exception == NULL) {
  49. LM_ERR("Can't get traceback, PyErr_Fetch() has failed.\n");
  50. return;
  51. }
  52. PyErr_NormalizeException(&exception, &v, &tb);
  53. if (exception == NULL) {
  54. LM_ERR("Can't get traceback, PyErr_NormalizeException() has failed.\n");
  55. return;
  56. }
  57. exc_exit = PyErr_GivenExceptionMatches(exception, PyExc_SystemExit);
  58. args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
  59. Py_XDECREF(exception);
  60. Py_XDECREF(v);
  61. Py_XDECREF(tb);
  62. if (args == NULL) {
  63. LM_ERR("Can't get traceback, PyTuple_Pack() has failed.\n");
  64. return;
  65. }
  66. pResult = PyObject_CallObject(format_exc_obj, args);
  67. Py_DECREF(args);
  68. if (pResult == NULL) {
  69. LM_ERR("Can't get traceback, traceback.format_exception() has failed.\n");
  70. return;
  71. }
  72. buflen = 1;
  73. buf = (char *)pkg_realloc(NULL, buflen * sizeof(char));
  74. if (!buf)
  75. {
  76. LM_ERR("Can't allocate memory (%lu bytes), pkg_realloc() has failed."
  77. " Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
  78. return;
  79. }
  80. memset(buf, 0, buflen * sizeof(char));
  81. for (i = 0; i < PySequence_Size(pResult); i++) {
  82. line = PySequence_GetItem(pResult, i);
  83. if (line == NULL) {
  84. LM_ERR("Can't get traceback, PySequence_GetItem() has failed.\n");
  85. Py_DECREF(pResult);
  86. if (buf)
  87. pkg_free(buf);
  88. return;
  89. }
  90. msg = PyString_AsString(line);
  91. if (msg == NULL) {
  92. LM_ERR("Can't get traceback, PyString_AsString() has failed.\n");
  93. Py_DECREF(line);
  94. Py_DECREF(pResult);
  95. if (buf)
  96. pkg_free(buf);
  97. return;
  98. }
  99. msglen = strlen(msg);
  100. buflen += ++msglen;
  101. buf = (char *)pkg_realloc(buf, buflen * sizeof(char *));
  102. if (!buf)
  103. {
  104. LM_ERR("Can't allocate memory (%lu bytes), pkg_realloc() has failed."
  105. " Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
  106. Py_DECREF(line);
  107. Py_DECREF(pResult);
  108. return;
  109. }
  110. strncat(buf, msg, msglen >= buflen ? buflen-1 : msglen);
  111. Py_DECREF(line);
  112. }
  113. if (srcbuf == NULL) {
  114. if(exc_exit) {
  115. LM_DBG("Unhandled exception in the Python code:\n%s", buf);
  116. } else {
  117. LM_ERR("Unhandled exception in the Python code:\n%s", buf);
  118. }
  119. } else {
  120. if(exc_exit) {
  121. LM_DBG("%s: Unhandled exception in the Python code:\n%s", srcbuf, buf);
  122. } else {
  123. LM_ERR("%s: Unhandled exception in the Python code:\n%s", srcbuf, buf);
  124. }
  125. }
  126. if (buf)
  127. pkg_free(buf);
  128. if (srcbuf)
  129. pkg_free(srcbuf);
  130. Py_DECREF(pResult);
  131. }
  132. PyObject *InitTracebackModule()
  133. {
  134. PyObject *pModule, *pTracebackObject;
  135. pModule = PyImport_ImportModule("traceback");
  136. if (pModule == NULL) {
  137. LM_ERR("Cannot import module 'traceback'.\n");
  138. return NULL;
  139. }
  140. pTracebackObject = PyObject_GetAttrString(pModule, "format_exception");
  141. Py_DECREF(pModule);
  142. if (pTracebackObject == NULL || !PyCallable_Check(pTracebackObject)) {
  143. LM_ERR("AttributeError: 'module' object 'traceback' has no attribute"
  144. " 'format_exception'.\n");
  145. Py_XDECREF(pTracebackObject);
  146. return NULL;
  147. }
  148. return pTracebackObject;
  149. }
  150. char *make_message(const char *fmt, ...)
  151. {
  152. int n;
  153. size_t size;
  154. char *p, *np;
  155. va_list ap;
  156. size = 100; /* Guess we need no more than 100 bytes. */
  157. p = (char *)pkg_realloc(NULL, size * sizeof(char *));
  158. if (!p)
  159. {
  160. LM_ERR("Can't allocate memory (%lu bytes), pkg_malloc() has failed:"
  161. " Not enough memory.\n", (unsigned long)(size * sizeof(char *)));
  162. return NULL;
  163. }
  164. memset(p, 0, size * sizeof(char *));
  165. while (1)
  166. {
  167. va_start(ap, fmt);
  168. n = vsnprintf(p, size, fmt, ap);
  169. va_end(ap);
  170. if (n > -1 && n < size)
  171. return p;
  172. if (n > -1) /* glibc 2.1 */
  173. size = n+1;
  174. else /* glibc 2.0 */
  175. size *= 2;
  176. np = (char *)pkg_realloc(p, size * sizeof(char *));
  177. if (!np)
  178. {
  179. LM_ERR("Can't allocate memory (%lu bytes), pkg_realloc() has failed:"
  180. " Not enough memory.\n", (unsigned long)size * sizeof(char *));
  181. if (p)
  182. pkg_free(p);
  183. return NULL;
  184. }
  185. else
  186. p = np;
  187. }
  188. return NULL; // shall not happened, but who knows ;)
  189. }
  190. char *get_class_name(PyObject *y)
  191. {
  192. PyObject *p;
  193. char *name;
  194. p = PyObject_GetAttrString(y, "__name__");
  195. if (p == NULL || p == Py_None)
  196. {
  197. Py_XDECREF(p);
  198. return NULL;
  199. }
  200. name = PyString_AsString(p);
  201. Py_XDECREF(p);
  202. return name;
  203. }
  204. char *get_instance_class_name(PyObject *y)
  205. {
  206. PyObject *p, *n;
  207. char *name;
  208. n = PyObject_GetAttrString(y, "__class__");
  209. if (n == NULL || n == Py_None)
  210. {
  211. Py_XDECREF(n);
  212. return NULL;
  213. }
  214. p = PyObject_GetAttrString(n, "__name__");
  215. if (p == NULL || p == Py_None)
  216. {
  217. Py_XDECREF(p);
  218. return NULL;
  219. }
  220. name = PyString_AsString(p);
  221. Py_XDECREF(p);
  222. Py_XDECREF(n);
  223. return name;
  224. }