python_support.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "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. // We don't want to generate traceback when no errors occured
  39. if (!PyErr_Occurred())
  40. return;
  41. if (fmt == NULL)
  42. srcbuf = NULL;
  43. else
  44. srcbuf = make_message(fmt);
  45. PyErr_Fetch(&exception, &v, &tb);
  46. PyErr_Clear();
  47. if (exception == NULL) {
  48. LM_ERR("python_handle_exception(): Can't get traceback, PyErr_Fetch() has failed.\n");
  49. return;
  50. }
  51. PyErr_NormalizeException(&exception, &v, &tb);
  52. if (exception == NULL) {
  53. LM_ERR("python_handle_exception(): Can't get traceback, PyErr_NormalizeException() has failed.\n");
  54. return;
  55. }
  56. args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
  57. Py_XDECREF(exception);
  58. Py_XDECREF(v);
  59. Py_XDECREF(tb);
  60. if (args == NULL) {
  61. LM_ERR("python_handle_exception(): Can't get traceback, PyTuple_Pack() has failed.\n");
  62. return;
  63. }
  64. pResult = PyObject_CallObject(format_exc_obj, args);
  65. Py_DECREF(args);
  66. if (pResult == NULL) {
  67. LM_ERR("python_handle_exception(): Can't get traceback, traceback.format_exception() has failed.\n");
  68. return;
  69. }
  70. buflen = 1;
  71. buf = (char *)pkg_realloc(NULL, buflen * sizeof(char *));
  72. if (!buf)
  73. {
  74. LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
  75. return;
  76. }
  77. memset(buf, 0, sizeof(char *));
  78. for (i = 0; i < PySequence_Size(pResult); i++) {
  79. line = PySequence_GetItem(pResult, i);
  80. if (line == NULL) {
  81. LM_ERR("python_handle_exception(): Can't get traceback, PySequence_GetItem() has failed.\n");
  82. Py_DECREF(pResult);
  83. if (buf)
  84. pkg_free(buf);
  85. return;
  86. }
  87. msg = PyString_AsString(line);
  88. if (msg == NULL) {
  89. LM_ERR("python_handle_exception(): Can't get traceback, PyString_AsString() has failed.\n");
  90. Py_DECREF(line);
  91. Py_DECREF(pResult);
  92. if (buf)
  93. pkg_free(buf);
  94. return;
  95. }
  96. msglen = strlen(msg);
  97. buflen += ++msglen;
  98. buf = (char *)pkg_realloc(buf, buflen * sizeof(char *));
  99. if (!buf)
  100. {
  101. LM_ERR("python_handle_exception(): Can't allocate memory (%lu bytes), pkg_realloc() has failed. Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
  102. Py_DECREF(line);
  103. Py_DECREF(pResult);
  104. return;
  105. }
  106. strncat(buf, msg, msglen >= buflen ? buflen-1 : msglen);
  107. Py_DECREF(line);
  108. }
  109. if (srcbuf == NULL)
  110. LM_ERR("Unhandled exception in the Python code:\n%s", buf);
  111. else
  112. LM_ERR("%s: Unhandled exception in the Python code:\n%s", srcbuf, buf);
  113. if (buf)
  114. pkg_free(buf);
  115. if (srcbuf)
  116. pkg_free(srcbuf);
  117. Py_DECREF(pResult);
  118. }
  119. PyObject *InitTracebackModule()
  120. {
  121. PyObject *pModule, *pTracebackObject;
  122. pModule = PyImport_ImportModule("traceback");
  123. if (pModule == NULL) {
  124. LM_ERR("InitTracebackModule(): Cannot import module 'traceback'.\n");
  125. return NULL;
  126. }
  127. pTracebackObject = PyObject_GetAttrString(pModule, "format_exception");
  128. Py_DECREF(pModule);
  129. if (pTracebackObject == NULL || !PyCallable_Check(pTracebackObject)) {
  130. LM_ERR("InitTracebackModule(): AttributeError: 'module' object 'traceback' has no attribute 'format_exception'.\n");
  131. Py_XDECREF(pTracebackObject);
  132. return NULL;
  133. }
  134. return pTracebackObject;
  135. }
  136. char *make_message(const char *fmt, ...)
  137. {
  138. int n;
  139. size_t size;
  140. char *p, *np;
  141. va_list ap;
  142. size = 100; /* Guess we need no more than 100 bytes. */
  143. p = (char *)pkg_realloc(NULL, size * sizeof(char *));
  144. if (!p)
  145. {
  146. LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_malloc() has failed: Not enough memory.\n", (unsigned long)(size * sizeof(char *)));
  147. return NULL;
  148. }
  149. memset(p, 0, size * sizeof(char *));
  150. while (1)
  151. {
  152. va_start(ap, fmt);
  153. n = vsnprintf(p, size, fmt, ap);
  154. va_end(ap);
  155. if (n > -1 && n < size)
  156. return p;
  157. if (n > -1) /* glibc 2.1 */
  158. size = n+1;
  159. else /* glibc 2.0 */
  160. size *= 2;
  161. np = (char *)pkg_realloc(p, size * sizeof(char *));
  162. if (!np)
  163. {
  164. LM_ERR("make_message(): Can't allocate memory (%lu bytes), pkg_realloc() has failed: Not enough memory.\n", (unsigned long)size * sizeof(char *));
  165. if (p)
  166. pkg_free(p);
  167. return NULL;
  168. }
  169. else
  170. p = np;
  171. }
  172. return NULL; // shall not happened, but who knows ;)
  173. }
  174. char *get_class_name(PyObject *y)
  175. {
  176. PyObject *p;
  177. char *name;
  178. p = PyObject_GetAttrString(y, "__name__");
  179. if (p == NULL || p == Py_None)
  180. {
  181. Py_XDECREF(p);
  182. return NULL;
  183. }
  184. name = PyString_AsString(p);
  185. Py_XDECREF(p);
  186. return name;
  187. }
  188. char *get_instance_class_name(PyObject *y)
  189. {
  190. PyObject *p, *n;
  191. char *name;
  192. n = PyObject_GetAttrString(y, "__class__");
  193. if (n == NULL || n == Py_None)
  194. {
  195. Py_XDECREF(n);
  196. return NULL;
  197. }
  198. p = PyObject_GetAttrString(n, "__name__");
  199. if (p == NULL || p == Py_None)
  200. {
  201. Py_XDECREF(p);
  202. return NULL;
  203. }
  204. name = PyString_AsString(p);
  205. Py_XDECREF(p);
  206. Py_XDECREF(n);
  207. return name;
  208. }