python_support.c 6.3 KB

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