-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_support.c
82 lines (78 loc) · 2.56 KB
/
python_support.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* $Id: python_support.c,v 1.1 2009/12/09 09:28:26 root Exp $
*
* Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "../../dprint.h"
#include "python_mod.h"
#include <stdio.h>
#include <Python.h>
void
python_handle_exception(const char *fname)
{
PyObject *pResult;
const char *msg;
PyObject *exception, *v, *tb, *args;
PyObject *line;
int i;
LM_ERR("%s: Unhandled exception in the Python code:\n", fname);
PyErr_Fetch(&exception, &v, &tb);
PyErr_Clear();
if (exception == NULL) {
LM_ERR("can't get traceback, PyErr_Fetch() has failed\n");
return;
}
PyErr_NormalizeException(&exception, &v, &tb);
if (exception == NULL) {
LM_ERR("can't get traceback, PyErr_NormalizeException() has failed\n");
return;
}
args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
Py_XDECREF(exception);
Py_XDECREF(v);
Py_XDECREF(tb);
if (args == NULL) {
LM_ERR("can't get traceback, PyTuple_Pack() has failed\n");
return;
}
pResult = PyObject_CallObject(format_exc_obj, args);
Py_DECREF(args);
if (pResult == NULL) {
LM_ERR("can't get traceback, traceback.format_exception() has failed\n");
return;
}
for (i = 0; i < PySequence_Size(pResult); i++) {
line = PySequence_GetItem(pResult, i);
if (line == NULL) {
LM_ERR("can't get traceback, PySequence_GetItem() has failed\n");
Py_DECREF(pResult);
return;
}
msg = PyString_AsString(line);
if (msg == NULL) {
LM_ERR("can't get traceback, PyString_AsString() has failed\n");
Py_DECREF(line);
Py_DECREF(pResult);
return;
}
LM_ERR("\t%s", msg);
Py_DECREF(line);
}
Py_DECREF(pResult);
}