Browse code

app_python: renamed main files to match module name

Daniel-Constantin Mierla authored on 18/04/2016 12:13:03
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,384 +0,0 @@
1
-/*
2
- * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-#include <Python.h>
23
-#include <libgen.h>
24
-
25
-#include "../../str.h"
26
-#include "../../sr_module.h"
27
-
28
-#include "python_exec.h"
29
-#include "python_iface.h"
30
-#include "python_msgobj.h"
31
-#include "python_support.h"
32
-#include "python_mod.h"
33
-
34
-#include "mod_Router.h"
35
-#include "mod_Core.h"
36
-#include "mod_Ranks.h"
37
-#include "mod_Logger.h"
38
-
39
-MODULE_VERSION
40
-
41
-
42
-static str script_name = str_init("/usr/local/etc/" NAME "/handler.py");
43
-static str mod_init_fname = str_init("mod_init");
44
-static str child_init_mname = str_init("child_init");
45
-
46
-static int mod_init(void);
47
-static int child_init(int rank);
48
-static void mod_destroy(void);
49
-
50
-PyObject *handler_obj;
51
-
52
-char *dname = NULL, *bname = NULL;
53
-
54
-PyThreadState *myThreadState;
55
-
56
-/** module parameters */
57
-static param_export_t params[]={
58
-	{"script_name",        PARAM_STR, &script_name },
59
-	{"mod_init_function",  PARAM_STR, &mod_init_fname },
60
-	{"child_init_method",  PARAM_STR, &child_init_mname },
61
-	{0,0,0}
62
-};
63
-
64
-/*
65
- * Exported functions
66
- */
67
-static cmd_export_t cmds[] = {
68
-	{ "python_exec", (cmd_function)python_exec1, 1,  NULL, 0,	ANY_ROUTE },
69
-	{ "python_exec", (cmd_function)python_exec2, 2,  NULL, 0,	ANY_ROUTE },
70
-	{ 0, 0, 0, 0, 0, 0 }
71
-};
72
-
73
-/** module exports */
74
-struct module_exports exports = {
75
-	"app_python",                   /* module name */
76
-	RTLD_NOW | RTLD_GLOBAL,         /* dlopen flags */
77
-	cmds,                           /* exported functions */
78
-	params,                         /* exported parameters */
79
-	0,                              /* exported statistics */
80
-	0,                              /* exported MI functions */
81
-	0,                              /* exported pseudo-variables */
82
-	0,                              /* extra processes */
83
-	mod_init,                       /* module initialization function */
84
-	(response_function) NULL,       /* response handling function */
85
-	(destroy_function) mod_destroy, /* destroy function */
86
-	child_init                      /* per-child init function */
87
-};
88
-
89
-static int mod_init(void)
90
-{
91
-	char *dname_src, *bname_src;
92
-
93
-	int i;
94
-	PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
95
-	PyThreadState *mainThreadState;
96
-
97
-	dname_src = as_asciiz(&script_name);
98
-	bname_src = as_asciiz(&script_name);
99
-
100
-	if(dname_src==NULL || bname_src==NULL)
101
-	{
102
-		LM_ERR("no more pkg memory\n");
103
-		return -1;
104
-	}
105
-
106
-	dname = strdup(dirname(dname_src));
107
-	if (strlen(dname) == 0)
108
-		dname = ".";
109
-	bname = strdup(basename(bname_src));
110
-	i = strlen(bname);
111
-	if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
112
-		i -= 1;
113
-	if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
114
-		bname[i - 3] = '\0';
115
-	} else {
116
-		LM_ERR("%s: script_name doesn't look like a python script\n",
117
-				script_name.s);
118
-		return -1;
119
-	}
120
-
121
-	Py_Initialize();
122
-	PyEval_InitThreads();
123
-	mainThreadState = PyThreadState_Get();
124
-
125
-	format_exc_obj = InitTracebackModule();
126
-
127
-	if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
128
-	{
129
-		Py_XDECREF(format_exc_obj);
130
-		PyEval_ReleaseLock();
131
-		return -1;
132
-	}
133
-
134
-	sys_path = PySys_GetObject("path");
135
-	/* PySys_GetObject doesn't pass reference! No need to DEREF */
136
-	if (sys_path == NULL) {
137
-		if (!PyErr_Occurred())
138
-			PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
139
-		python_handle_exception("mod_init");
140
-		Py_DECREF(format_exc_obj);
141
-		PyEval_ReleaseLock();
142
-		return -1;
143
-	}
144
-
145
-	pDir = PyString_FromString(dname);
146
-	if (pDir == NULL) {
147
-		if (!PyErr_Occurred())
148
-			PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
149
-		python_handle_exception("mod_init");
150
-		Py_DECREF(format_exc_obj);
151
-		PyEval_ReleaseLock();
152
-		return -1;
153
-	}
154
-
155
-	PyList_Insert(sys_path, 0, pDir);
156
-	Py_DECREF(pDir);
157
-
158
-	if (ap_init_modules() != 0) {
159
-		if (!PyErr_Occurred())
160
-			PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
161
-		python_handle_exception("mod_init");
162
-		Py_DECREF(format_exc_obj);
163
-		PyEval_ReleaseLock();
164
-		return -1;
165
-	}
166
-
167
-	if (python_msgobj_init() != 0) {
168
-		if (!PyErr_Occurred())
169
-			PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
170
-		python_handle_exception("mod_init");
171
-		Py_DECREF(format_exc_obj);
172
-		PyEval_ReleaseLock();
173
-		return -1;
174
-	}
175
-
176
-	pModule = PyImport_ImportModule(bname);
177
-	if (pModule == NULL) {
178
-		if (!PyErr_Occurred())
179
-			PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
180
-		python_handle_exception("mod_init");
181
-		Py_DECREF(format_exc_obj);
182
-		PyEval_ReleaseLock();
183
-		return -1;
184
-	}
185
-
186
-	pkg_free(dname_src);
187
-	pkg_free(bname_src);
188
-
189
-	pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
190
-	Py_DECREF(pModule);
191
-
192
-	/* pFunc is a new reference */
193
-
194
-	if (pFunc == NULL) {
195
-		if (!PyErr_Occurred())
196
-			PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
197
-		python_handle_exception("mod_init");
198
-		Py_DECREF(format_exc_obj);
199
-		Py_XDECREF(pFunc);
200
-		PyEval_ReleaseLock();
201
-		return -1;
202
-	}
203
-
204
-	if (!PyCallable_Check(pFunc)) {
205
-		if (!PyErr_Occurred())
206
-			PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
207
-		python_handle_exception("mod_init");
208
-		Py_DECREF(format_exc_obj);
209
-		Py_XDECREF(pFunc);
210
-		PyEval_ReleaseLock();
211
-		return -1;
212
-	}
213
-
214
-
215
-	pArgs = PyTuple_New(0);
216
-	if (pArgs == NULL) {
217
-		python_handle_exception("mod_init");
218
-		Py_DECREF(format_exc_obj);
219
-		Py_DECREF(pFunc);
220
-		PyEval_ReleaseLock();
221
-		return -1;
222
-	}
223
-
224
-	handler_obj = PyObject_CallObject(pFunc, pArgs);
225
-
226
-	Py_XDECREF(pFunc);
227
-	Py_XDECREF(pArgs);
228
-
229
-	if (handler_obj == Py_None) {
230
-		if (!PyErr_Occurred())
231
-			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
232
-		python_handle_exception("mod_init");
233
-		Py_DECREF(format_exc_obj);
234
-		PyEval_ReleaseLock();
235
-		return -1;
236
-	}
237
-
238
-	if (PyErr_Occurred()) {
239
-		python_handle_exception("mod_init");
240
-		Py_XDECREF(handler_obj);
241
-		Py_DECREF(format_exc_obj);
242
-		PyEval_ReleaseLock();
243
-		return -1;
244
-	}
245
-
246
-	if (handler_obj == NULL) {
247
-		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
248
-		if (!PyErr_Occurred())
249
-			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
250
-		python_handle_exception("mod_init");
251
-		Py_DECREF(format_exc_obj);
252
-		PyEval_ReleaseLock();
253
-		return -1;
254
-	}
255
-
256
-	myThreadState = PyThreadState_New(mainThreadState->interp);
257
-	PyEval_ReleaseLock();
258
-
259
-	return 0;
260
-}
261
-
262
-static int child_init(int rank)
263
-{
264
-	PyObject *pFunc, *pArgs, *pValue, *pResult;
265
-	int rval;
266
-	char *classname;
267
-
268
-	PyEval_AcquireLock();
269
-	PyThreadState_Swap(myThreadState);
270
-
271
-	// get instance class name
272
-	classname = get_instance_class_name(handler_obj);
273
-	if (classname == NULL)
274
-	{
275
-		if (!PyErr_Occurred())
276
-			PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
277
-		python_handle_exception("child_init");
278
-		Py_DECREF(format_exc_obj);
279
-		PyThreadState_Swap(NULL);
280
-		PyEval_ReleaseLock();
281
-		return -1;
282
-	}
283
-
284
-	pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
285
-
286
-	if (pFunc == NULL) {
287
-		python_handle_exception("child_init");
288
-		Py_XDECREF(pFunc);
289
-		Py_DECREF(format_exc_obj);
290
-		PyThreadState_Swap(NULL);
291
-		PyEval_ReleaseLock();
292
-		return -1;
293
-	}
294
-
295
-	if (!PyCallable_Check(pFunc)) {
296
-		if (!PyErr_Occurred())
297
-			PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
298
-		python_handle_exception("child_init");
299
-		Py_DECREF(format_exc_obj);
300
-		Py_XDECREF(pFunc);
301
-		PyThreadState_Swap(NULL);
302
-		PyEval_ReleaseLock();
303
-		return -1;
304
-	}
305
-
306
-	pArgs = PyTuple_New(1);
307
-	if (pArgs == NULL) {
308
-		python_handle_exception("child_init");
309
-		Py_DECREF(format_exc_obj);
310
-		Py_DECREF(pFunc);
311
-		PyThreadState_Swap(NULL);
312
-		PyEval_ReleaseLock();
313
-		return -1;
314
-	}
315
-
316
-	pValue = PyInt_FromLong((long)rank);
317
-	if (pValue == NULL) {
318
-		python_handle_exception("child_init");
319
-		Py_DECREF(format_exc_obj);
320
-		Py_DECREF(pArgs);
321
-		Py_DECREF(pFunc);
322
-		PyThreadState_Swap(NULL);
323
-		PyEval_ReleaseLock();
324
-		return -1;
325
-	}
326
-	PyTuple_SetItem(pArgs, 0, pValue);
327
-	/* pValue has been stolen */
328
-
329
-	pResult = PyObject_CallObject(pFunc, pArgs);
330
-	Py_DECREF(pFunc);
331
-	Py_DECREF(pArgs);
332
-
333
-
334
-
335
-
336
-	if (PyErr_Occurred()) {
337
-		python_handle_exception("child_init");
338
-		Py_DECREF(format_exc_obj);
339
-		Py_XDECREF(pResult);
340
-		PyThreadState_Swap(NULL);
341
-		PyEval_ReleaseLock();
342
-		return -1;
343
-	}
344
-
345
-	if (pResult == NULL) {
346
-		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
347
-		PyThreadState_Swap(NULL);
348
-		PyEval_ReleaseLock();
349
-		return -1;
350
-	}
351
-
352
-	if (!PyInt_Check(pResult))
353
-	{
354
-		if (!PyErr_Occurred())
355
-			PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
356
-		python_handle_exception("child_init");
357
-		Py_DECREF(format_exc_obj);
358
-		Py_XDECREF(pResult);
359
-		PyThreadState_Swap(NULL);
360
-		PyEval_ReleaseLock();
361
-		return -1;
362
-	}
363
-
364
-
365
-	rval = PyInt_AsLong(pResult);
366
-	Py_DECREF(pResult);
367
-	PyThreadState_Swap(NULL);
368
-	PyEval_ReleaseLock();
369
-
370
-	return rval;
371
-}
372
-
373
-static void mod_destroy(void)
374
-{
375
-	if (dname)
376
-		free(dname);	// dname was strdup'ed
377
-	if (bname)
378
-		free(bname);	// bname was strdup'ed
379
-
380
-	destroy_mod_Core();
381
-	destroy_mod_Ranks();
382
-	destroy_mod_Logger();
383
-	destroy_mod_Router();
384
-}
Browse code

app_python: coherent indetation and whitespacing

Daniel-Constantin Mierla authored on 18/04/2016 10:35:36
Showing 1 changed files
... ...
@@ -17,7 +17,7 @@
17 17
  * along with this program; if not, write to the Free Software
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  *
20
-*/
20
+ */
21 21
 
22 22
 #include <Python.h>
23 23
 #include <libgen.h>
... ...
@@ -55,330 +55,330 @@ PyThreadState *myThreadState;
55 55
 
56 56
 /** module parameters */
57 57
 static param_export_t params[]={
58
-    {"script_name",        PARAM_STR, &script_name },
59
-    {"mod_init_function",  PARAM_STR, &mod_init_fname },
60
-    {"child_init_method",  PARAM_STR, &child_init_mname },
61
-    {0,0,0}
58
+	{"script_name",        PARAM_STR, &script_name },
59
+	{"mod_init_function",  PARAM_STR, &mod_init_fname },
60
+	{"child_init_method",  PARAM_STR, &child_init_mname },
61
+	{0,0,0}
62 62
 };
63 63
 
64 64
 /*
65 65
  * Exported functions
66 66
  */
67 67
 static cmd_export_t cmds[] = {
68
-    { "python_exec", (cmd_function)python_exec1, 1,  NULL, 0,	ANY_ROUTE },
69
-    { "python_exec", (cmd_function)python_exec2, 2,  NULL, 0,	ANY_ROUTE },
70
-    { 0, 0, 0, 0, 0, 0 }
68
+	{ "python_exec", (cmd_function)python_exec1, 1,  NULL, 0,	ANY_ROUTE },
69
+	{ "python_exec", (cmd_function)python_exec2, 2,  NULL, 0,	ANY_ROUTE },
70
+	{ 0, 0, 0, 0, 0, 0 }
71 71
 };
72 72
 
73 73
 /** module exports */
74 74
 struct module_exports exports = {
75
-    "app_python",                   /* module name */
76
-    RTLD_NOW | RTLD_GLOBAL,         /* dlopen flags */
77
-    cmds,                           /* exported functions */
78
-    params,                         /* exported parameters */
79
-    0,                              /* exported statistics */
80
-    0,                              /* exported MI functions */
81
-    0,                              /* exported pseudo-variables */
82
-    0,                              /* extra processes */
83
-    mod_init,                       /* module initialization function */
84
-    (response_function) NULL,       /* response handling function */
85
-    (destroy_function) mod_destroy, /* destroy function */
86
-    child_init                      /* per-child init function */
75
+	"app_python",                   /* module name */
76
+	RTLD_NOW | RTLD_GLOBAL,         /* dlopen flags */
77
+	cmds,                           /* exported functions */
78
+	params,                         /* exported parameters */
79
+	0,                              /* exported statistics */
80
+	0,                              /* exported MI functions */
81
+	0,                              /* exported pseudo-variables */
82
+	0,                              /* extra processes */
83
+	mod_init,                       /* module initialization function */
84
+	(response_function) NULL,       /* response handling function */
85
+	(destroy_function) mod_destroy, /* destroy function */
86
+	child_init                      /* per-child init function */
87 87
 };
88 88
 
89 89
 static int mod_init(void)
90 90
 {
91
-    char *dname_src, *bname_src;
92
-
93
-    int i;
94
-    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
95
-    PyThreadState *mainThreadState;
96
-
97
-    dname_src = as_asciiz(&script_name);
98
-    bname_src = as_asciiz(&script_name);
99
-
100
-    if(dname_src==NULL || bname_src==NULL)
101
-    {
102
-        LM_ERR("no more pkg memory\n");
103
-        return -1;
104
-    }
105
-
106
-    dname = strdup(dirname(dname_src));
107
-    if (strlen(dname) == 0)
108
-        dname = ".";
109
-    bname = strdup(basename(bname_src));
110
-    i = strlen(bname);
111
-    if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
112
-        i -= 1;
113
-    if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
114
-        bname[i - 3] = '\0';
115
-    } else {
116
-        LM_ERR("%s: script_name doesn't look like a python script\n",
117
-          script_name.s);
118
-        return -1;
119
-    }
120
-
121
-    Py_Initialize();
122
-    PyEval_InitThreads();
123
-    mainThreadState = PyThreadState_Get();
124
-
125
-    format_exc_obj = InitTracebackModule();
126
-
127
-    if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
128
-    {
129
-        Py_XDECREF(format_exc_obj);
130
-        PyEval_ReleaseLock();
131
-        return -1;
132
-    }
133
-
134
-    sys_path = PySys_GetObject("path");
135
-    /* PySys_GetObject doesn't pass reference! No need to DEREF */
136
-    if (sys_path == NULL) {
137
-	if (!PyErr_Occurred())
138
-	    PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
139
-	python_handle_exception("mod_init");
140
-	Py_DECREF(format_exc_obj);
141
-        PyEval_ReleaseLock();
142
-        return -1;
143
-    }
144
-
145
-    pDir = PyString_FromString(dname);
146
-    if (pDir == NULL) {
147
-	if (!PyErr_Occurred())
148
-	    PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
149
-	python_handle_exception("mod_init");
150
-	Py_DECREF(format_exc_obj);
151
-        PyEval_ReleaseLock();
152
-        return -1;
153
-    }
154
-
155
-    PyList_Insert(sys_path, 0, pDir);
156
-    Py_DECREF(pDir);
157
-
158
-    if (ap_init_modules() != 0) {
159
-	if (!PyErr_Occurred())
160
-	    PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
161
-	python_handle_exception("mod_init");
162
-	Py_DECREF(format_exc_obj);
163
-        PyEval_ReleaseLock();
164
-        return -1;
165
-    }
166
-
167
-    if (python_msgobj_init() != 0) {
168
-	if (!PyErr_Occurred())
169
-	    PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
170
-	python_handle_exception("mod_init");
171
-	Py_DECREF(format_exc_obj);
172
-        PyEval_ReleaseLock();
173
-        return -1;
174
-    }
175
-
176
-    pModule = PyImport_ImportModule(bname);
177
-    if (pModule == NULL) {
178
-	if (!PyErr_Occurred())
179
-	    PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
180
-	python_handle_exception("mod_init");
181
-	Py_DECREF(format_exc_obj);
182
-        PyEval_ReleaseLock();
183
-        return -1;
184
-    }
185
-
186
-    pkg_free(dname_src);
187
-    pkg_free(bname_src);
188
-
189
-    pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
190
-    Py_DECREF(pModule);
191
-
192
-    /* pFunc is a new reference */
193
-
194
-    if (pFunc == NULL) {
195
-	if (!PyErr_Occurred())
196
-	    PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
197
-	python_handle_exception("mod_init");
198
-	Py_DECREF(format_exc_obj);
199
-        Py_XDECREF(pFunc);
200
-        PyEval_ReleaseLock();
201
-        return -1;
202
-    }
203
-
204
-    if (!PyCallable_Check(pFunc)) {
205
-	if (!PyErr_Occurred())
206
-	    PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
207
-	python_handle_exception("mod_init");
208
-	Py_DECREF(format_exc_obj);
209
-        Py_XDECREF(pFunc);
210
-        PyEval_ReleaseLock();
211
-        return -1;
212
-    }
213
-
214
-
215
-    pArgs = PyTuple_New(0);
216
-    if (pArgs == NULL) {
217
-	python_handle_exception("mod_init");
218
-        Py_DECREF(format_exc_obj);
219
-        Py_DECREF(pFunc);
220
-        PyEval_ReleaseLock();
221
-        return -1;
222
-    }
223
-
224
-    handler_obj = PyObject_CallObject(pFunc, pArgs);
225
-
226
-    Py_XDECREF(pFunc);
227
-    Py_XDECREF(pArgs);
228
-
229
-    if (handler_obj == Py_None) {
230
-	if (!PyErr_Occurred())
231
-	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
232
-	python_handle_exception("mod_init");
233
-        Py_DECREF(format_exc_obj);
234
-        PyEval_ReleaseLock();
235
-        return -1;
236
-    }
237
-
238
-    if (PyErr_Occurred()) {
239
-        python_handle_exception("mod_init");
240
-        Py_XDECREF(handler_obj);
241
-        Py_DECREF(format_exc_obj);
242
-        PyEval_ReleaseLock();
243
-        return -1;
244
-    }
245
-
246
-    if (handler_obj == NULL) {
247
-        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
248
-	if (!PyErr_Occurred())
249
-	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
250
-	python_handle_exception("mod_init");
251
-        Py_DECREF(format_exc_obj);
252
-        PyEval_ReleaseLock();
253
-        return -1;
254
-    }
255
-
256
-    myThreadState = PyThreadState_New(mainThreadState->interp);
257
-    PyEval_ReleaseLock();
258
-
259
-    return 0;
260
-}
91
+	char *dname_src, *bname_src;
92
+
93
+	int i;
94
+	PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
95
+	PyThreadState *mainThreadState;
96
+
97
+	dname_src = as_asciiz(&script_name);
98
+	bname_src = as_asciiz(&script_name);
99
+
100
+	if(dname_src==NULL || bname_src==NULL)
101
+	{
102
+		LM_ERR("no more pkg memory\n");
103
+		return -1;
104
+	}
105
+
106
+	dname = strdup(dirname(dname_src));
107
+	if (strlen(dname) == 0)
108
+		dname = ".";
109
+	bname = strdup(basename(bname_src));
110
+	i = strlen(bname);
111
+	if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
112
+		i -= 1;
113
+	if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
114
+		bname[i - 3] = '\0';
115
+	} else {
116
+		LM_ERR("%s: script_name doesn't look like a python script\n",
117
+				script_name.s);
118
+		return -1;
119
+	}
120
+
121
+	Py_Initialize();
122
+	PyEval_InitThreads();
123
+	mainThreadState = PyThreadState_Get();
124
+
125
+	format_exc_obj = InitTracebackModule();
126
+
127
+	if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
128
+	{
129
+		Py_XDECREF(format_exc_obj);
130
+		PyEval_ReleaseLock();
131
+		return -1;
132
+	}
133
+
134
+	sys_path = PySys_GetObject("path");
135
+	/* PySys_GetObject doesn't pass reference! No need to DEREF */
136
+	if (sys_path == NULL) {
137
+		if (!PyErr_Occurred())
138
+			PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
139
+		python_handle_exception("mod_init");
140
+		Py_DECREF(format_exc_obj);
141
+		PyEval_ReleaseLock();
142
+		return -1;
143
+	}
144
+
145
+	pDir = PyString_FromString(dname);
146
+	if (pDir == NULL) {
147
+		if (!PyErr_Occurred())
148
+			PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
149
+		python_handle_exception("mod_init");
150
+		Py_DECREF(format_exc_obj);
151
+		PyEval_ReleaseLock();
152
+		return -1;
153
+	}
154
+
155
+	PyList_Insert(sys_path, 0, pDir);
156
+	Py_DECREF(pDir);
157
+
158
+	if (ap_init_modules() != 0) {
159
+		if (!PyErr_Occurred())
160
+			PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
161
+		python_handle_exception("mod_init");
162
+		Py_DECREF(format_exc_obj);
163
+		PyEval_ReleaseLock();
164
+		return -1;
165
+	}
166
+
167
+	if (python_msgobj_init() != 0) {
168
+		if (!PyErr_Occurred())
169
+			PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
170
+		python_handle_exception("mod_init");
171
+		Py_DECREF(format_exc_obj);
172
+		PyEval_ReleaseLock();
173
+		return -1;
174
+	}
175
+
176
+	pModule = PyImport_ImportModule(bname);
177
+	if (pModule == NULL) {
178
+		if (!PyErr_Occurred())
179
+			PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
180
+		python_handle_exception("mod_init");
181
+		Py_DECREF(format_exc_obj);
182
+		PyEval_ReleaseLock();
183
+		return -1;
184
+	}
185
+
186
+	pkg_free(dname_src);
187
+	pkg_free(bname_src);
188
+
189
+	pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
190
+	Py_DECREF(pModule);
191
+
192
+	/* pFunc is a new reference */
193
+
194
+	if (pFunc == NULL) {
195
+		if (!PyErr_Occurred())
196
+			PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
197
+		python_handle_exception("mod_init");
198
+		Py_DECREF(format_exc_obj);
199
+		Py_XDECREF(pFunc);
200
+		PyEval_ReleaseLock();
201
+		return -1;
202
+	}
203
+
204
+	if (!PyCallable_Check(pFunc)) {
205
+		if (!PyErr_Occurred())
206
+			PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
207
+		python_handle_exception("mod_init");
208
+		Py_DECREF(format_exc_obj);
209
+		Py_XDECREF(pFunc);
210
+		PyEval_ReleaseLock();
211
+		return -1;
212
+	}
213
+
214
+
215
+	pArgs = PyTuple_New(0);
216
+	if (pArgs == NULL) {
217
+		python_handle_exception("mod_init");
218
+		Py_DECREF(format_exc_obj);
219
+		Py_DECREF(pFunc);
220
+		PyEval_ReleaseLock();
221
+		return -1;
222
+	}
223
+
224
+	handler_obj = PyObject_CallObject(pFunc, pArgs);
261 225
 
262
-static int child_init(int rank)
263
-{
264
-    PyObject *pFunc, *pArgs, *pValue, *pResult;
265
-    int rval;
266
-    char *classname;
267
-
268
-    PyEval_AcquireLock();
269
-    PyThreadState_Swap(myThreadState);
270
-
271
-    // get instance class name
272
-    classname = get_instance_class_name(handler_obj);
273
-    if (classname == NULL)
274
-    {
275
-	if (!PyErr_Occurred())
276
-	    PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
277
-	python_handle_exception("child_init");
278
-	Py_DECREF(format_exc_obj);
279
-	PyThreadState_Swap(NULL);
226
+	Py_XDECREF(pFunc);
227
+	Py_XDECREF(pArgs);
228
+
229
+	if (handler_obj == Py_None) {
230
+		if (!PyErr_Occurred())
231
+			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
232
+		python_handle_exception("mod_init");
233
+		Py_DECREF(format_exc_obj);
234
+		PyEval_ReleaseLock();
235
+		return -1;
236
+	}
237
+
238
+	if (PyErr_Occurred()) {
239
+		python_handle_exception("mod_init");
240
+		Py_XDECREF(handler_obj);
241
+		Py_DECREF(format_exc_obj);
242
+		PyEval_ReleaseLock();
243
+		return -1;
244
+	}
245
+
246
+	if (handler_obj == NULL) {
247
+		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
248
+		if (!PyErr_Occurred())
249
+			PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
250
+		python_handle_exception("mod_init");
251
+		Py_DECREF(format_exc_obj);
252
+		PyEval_ReleaseLock();
253
+		return -1;
254
+	}
255
+
256
+	myThreadState = PyThreadState_New(mainThreadState->interp);
280 257
 	PyEval_ReleaseLock();
281
-	return -1;
282
-    }
283 258
 
284
-    pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
259
+	return 0;
260
+}
285 261
 
286
-    if (pFunc == NULL) {
287
-	python_handle_exception("child_init");
288
-	Py_XDECREF(pFunc);
289
-	Py_DECREF(format_exc_obj);
290
-        PyThreadState_Swap(NULL);
291
-        PyEval_ReleaseLock();
292
-        return -1;
293
-    }
294
-
295
-    if (!PyCallable_Check(pFunc)) {
296
-	if (!PyErr_Occurred())
297
-	    PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
298
-	python_handle_exception("child_init");
299
-	Py_DECREF(format_exc_obj);
300
-	Py_XDECREF(pFunc);
301
-        PyThreadState_Swap(NULL);
302
-        PyEval_ReleaseLock();
303
-        return -1;
304
-    }
305
-
306
-    pArgs = PyTuple_New(1);
307
-    if (pArgs == NULL) {
308
-	python_handle_exception("child_init");
309
-	Py_DECREF(format_exc_obj);
310
-        Py_DECREF(pFunc);
311
-        PyThreadState_Swap(NULL);
312
-        PyEval_ReleaseLock();
313
-        return -1;
314
-    }
315
-
316
-    pValue = PyInt_FromLong((long)rank);
317
-    if (pValue == NULL) {
318
-	python_handle_exception("child_init");
319
-	Py_DECREF(format_exc_obj);
320
-        Py_DECREF(pArgs);
321
-        Py_DECREF(pFunc);
322
-        PyThreadState_Swap(NULL);
323
-        PyEval_ReleaseLock();
324
-        return -1;
325
-    }
326
-    PyTuple_SetItem(pArgs, 0, pValue);
327
-    /* pValue has been stolen */
328
-
329
-    pResult = PyObject_CallObject(pFunc, pArgs);
330
-    Py_DECREF(pFunc);
331
-    Py_DECREF(pArgs);
332
-
333
-    
334
-
335
-
336
-    if (PyErr_Occurred()) {
337
-        python_handle_exception("child_init");
338
-	Py_DECREF(format_exc_obj);
339
-        Py_XDECREF(pResult);
340
-        PyThreadState_Swap(NULL);
341
-        PyEval_ReleaseLock();
342
-        return -1;
343
-    }
344
-
345
-    if (pResult == NULL) {
346
-        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
347
-        PyThreadState_Swap(NULL);
348
-        PyEval_ReleaseLock();
349
-        return -1;
350
-    }
351
-
352
-    if (!PyInt_Check(pResult))
353
-    {
354
-	if (!PyErr_Occurred())
355
-	    PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
356
-	python_handle_exception("child_init");
357
-	Py_DECREF(format_exc_obj);
358
-	Py_XDECREF(pResult);
262
+static int child_init(int rank)
263
+{
264
+	PyObject *pFunc, *pArgs, *pValue, *pResult;
265
+	int rval;
266
+	char *classname;
267
+
268
+	PyEval_AcquireLock();
269
+	PyThreadState_Swap(myThreadState);
270
+
271
+	// get instance class name
272
+	classname = get_instance_class_name(handler_obj);
273
+	if (classname == NULL)
274
+	{
275
+		if (!PyErr_Occurred())
276
+			PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
277
+		python_handle_exception("child_init");
278
+		Py_DECREF(format_exc_obj);
279
+		PyThreadState_Swap(NULL);
280