-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_exec.c
More file actions
138 lines (123 loc) · 3.9 KB
/
python_exec.c
File metadata and controls
138 lines (123 loc) · 3.9 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* $Id: python_exec.c,v 1.1 2009/12/09 09:28:57 root Exp root $
*
* 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 <Python.h>
#include "../../mem/mem.h"
#include "../../data_lump.h"
#include "../../parser/parse_param.h"
#include "../../parser/msg_parser.h"
#include "../../dprint.h"
#include "../../action.h"
#include "../../config.h"
#include "../../parser/parse_uri.h"
#include "python_exec.h"
#include "python_mod.h"
#include "python_msgobj.h"
#include "python_support.h"
int
python_exec1(struct sip_msg* _msg, char* method_name, char *foobar)
{
return python_exec2(_msg, method_name, NULL);
}
int
python_exec2(struct sip_msg *_msg, char *method_name, char *mystr)
{
PyObject *pFunc, *pArgs, *pValue, *pResult, *pMethodName;
PyObject *msg;
PyObject *tmp;
const char *s;
int rval;
PyEval_AcquireLock();
PyThreadState_Swap(myThreadState);
pFunc = PyObject_GetAttrString(handler_obj, "call_routing_function");
if (pFunc == NULL || !PyCallable_Check(pFunc)) {
LM_ERR("%s not found or is not callable\n", method_name);
Py_XDECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
msg = newmsgobject(_msg);
if (msg == NULL) {
LM_ERR("can't create MSGtype instance\n");
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pArgs = PyTuple_New(mystr == NULL ? 2 : 3);
if (pArgs == NULL) {
LM_ERR("PyTuple_New() has failed\n");
msg_invalidate(msg);
Py_DECREF(msg);
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pMethodName = Py_BuildValue("s", method_name);
PyTuple_SetItem(pArgs, 0, pMethodName);
PyTuple_SetItem(pArgs, 1, msg);
/* Tuple steals msg */
if (mystr != NULL) {
pValue = PyString_FromString(mystr);
if (pValue == NULL) {
LM_ERR("PyString_FromString(%s) has failed\n", mystr);
msg_invalidate(msg);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
PyTuple_SetItem(pArgs, 2, pValue);
/* Tuple steals pValue */
}
pResult = PyObject_CallObject(pFunc, pArgs);
msg_invalidate(msg);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
if (PyErr_Occurred()) {
Py_XDECREF(pResult);
python_handle_exception("python_exec2");
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
if (pResult == NULL || pResult == Py_None) {
LM_ERR("PyObject_CallObject() returned NULL\n");
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
rval = PyInt_AsLong(pResult);
if (PyErr_Occurred()) {
tmp = PyObject_Repr(pResult);
s = PyString_AsString(tmp);
LM_ERR("Python routing function return something other than an integer or None: %s", s);
PyErr_Clear();
rval = -1;
}
Py_DECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return rval;
}