2
0

python_support.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <Python.h>
  23. #include <stdio.h>
  24. #include "../../dprint.h"
  25. #include "../../mem/mem.h"
  26. #include "python_mod.h"
  27. void
  28. python_handle_exception(const char *fname)
  29. {
  30. PyObject *pResult;
  31. const char *msg;
  32. char *buf;
  33. size_t buflen;
  34. PyObject *exception, *v, *tb, *args;
  35. PyObject *line;
  36. int i;
  37. PyErr_Fetch(&exception, &v, &tb);
  38. PyErr_Clear();
  39. if (exception == NULL) {
  40. LM_ERR("can't get traceback, PyErr_Fetch() has failed\n");
  41. return;
  42. }
  43. PyErr_NormalizeException(&exception, &v, &tb);
  44. if (exception == NULL) {
  45. LM_ERR("can't get traceback, PyErr_NormalizeException() has failed\n");
  46. return;
  47. }
  48. args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
  49. Py_XDECREF(exception);
  50. Py_XDECREF(v);
  51. Py_XDECREF(tb);
  52. if (args == NULL) {
  53. LM_ERR("can't get traceback, PyTuple_Pack() has failed\n");
  54. return;
  55. }
  56. pResult = PyObject_CallObject(format_exc_obj, args);
  57. Py_DECREF(args);
  58. if (pResult == NULL) {
  59. LM_ERR("can't get traceback, traceback.format_exception() has failed\n");
  60. return;
  61. }
  62. buflen = 1;
  63. buf = (char *)pkg_malloc(buflen * sizeof(char *));
  64. if (!buf)
  65. {
  66. LM_ERR("python_handle_exception(): Not enough memory\n");
  67. return;
  68. }
  69. memset(&buf, 0, buflen * sizeof(char *));
  70. for (i = 0; i < PySequence_Size(pResult); i++) {
  71. line = PySequence_GetItem(pResult, i);
  72. if (line == NULL) {
  73. LM_ERR("can't get traceback, PySequence_GetItem() has failed\n");
  74. Py_DECREF(pResult);
  75. if (buf)
  76. pkg_free(buf);
  77. return;
  78. }
  79. msg = PyString_AsString(line);
  80. if (msg == NULL) {
  81. LM_ERR("can't get traceback, PyString_AsString() has failed\n");
  82. Py_DECREF(line);
  83. Py_DECREF(pResult);
  84. if (buf)
  85. pkg_free(buf);
  86. return;
  87. }
  88. buflen += strlen(msg);
  89. buf = (char *)pkg_realloc(buf, (buflen + 1) * sizeof(char *));
  90. if (!buf)
  91. {
  92. LM_ERR("python_handle_exception(): Not enough memory\n");
  93. Py_DECREF(line);
  94. Py_DECREF(pResult);
  95. return;
  96. }
  97. strcat(buf, msg);
  98. Py_DECREF(line);
  99. }
  100. LM_ERR("%s: Unhandled exception in the Python code:\n%s", fname, buf);
  101. if (buf)
  102. pkg_free(buf);
  103. Py_DECREF(pResult);
  104. }