python_support.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "../../dprint.h"
  23. #include "python_mod.h"
  24. #include <stdio.h>
  25. #include <Python.h>
  26. void
  27. python_handle_exception(const char *fname)
  28. {
  29. PyObject *pResult;
  30. const char *msg;
  31. PyObject *exception, *v, *tb, *args;
  32. PyObject *line;
  33. int i;
  34. LM_ERR("%s: Unhandled exception in the Python code:\n", fname);
  35. PyErr_Fetch(&exception, &v, &tb);
  36. PyErr_Clear();
  37. if (exception == NULL) {
  38. LM_ERR("can't get traceback, PyErr_Fetch() has failed\n");
  39. return;
  40. }
  41. PyErr_NormalizeException(&exception, &v, &tb);
  42. if (exception == NULL) {
  43. LM_ERR("can't get traceback, PyErr_NormalizeException() has failed\n");
  44. return;
  45. }
  46. args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
  47. Py_XDECREF(exception);
  48. Py_XDECREF(v);
  49. Py_XDECREF(tb);
  50. if (args == NULL) {
  51. LM_ERR("can't get traceback, PyTuple_Pack() has failed\n");
  52. return;
  53. }
  54. pResult = PyObject_CallObject(format_exc_obj, args);
  55. Py_DECREF(args);
  56. if (pResult == NULL) {
  57. LM_ERR("can't get traceback, traceback.format_exception() has failed\n");
  58. return;
  59. }
  60. for (i = 0; i < PySequence_Size(pResult); i++) {
  61. line = PySequence_GetItem(pResult, i);
  62. if (line == NULL) {
  63. LM_ERR("can't get traceback, PySequence_GetItem() has failed\n");
  64. Py_DECREF(pResult);
  65. return;
  66. }
  67. msg = PyString_AsString(line);
  68. if (msg == NULL) {
  69. LM_ERR("can't get traceback, PyString_AsString() has failed\n");
  70. Py_DECREF(line);
  71. Py_DECREF(pResult);
  72. return;
  73. }
  74. LM_ERR("\t%s", msg);
  75. Py_DECREF(line);
  76. }
  77. Py_DECREF(pResult);
  78. }