summaryrefslogtreecommitdiff
path: root/plugins/python/pyutils.c
blob: f4b1ef3dc048f8c7f49af3fad5f619b4da883f60 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "uwsgi_python.h"

extern struct uwsgi_server uwsgi;
extern struct uwsgi_python up;

int manage_python_response(struct wsgi_request *wsgi_req) {
	// use standard WSGI response parse
	return uwsgi_response_subhandler_wsgi(wsgi_req);
}

char *uwsgi_python_get_exception_type(PyObject *exc) {
	char *class_name = NULL;
#if !defined(PYTHREE)
	if (PyClass_Check(exc)) {
		class_name = PyString_AsString( ((PyClassObject*)(exc))->cl_name );
	}
	else {
#endif
		class_name = (char *) ((PyTypeObject*)exc)->tp_name;
#if !defined(PYTHREE)
	}
#endif

	if (class_name) {
		char *dot = strrchr(class_name, '.');
		if (dot) class_name = dot+1;

		PyObject *module_name = PyObject_GetAttrString(exc, "__module__");
		if (module_name) {
#ifdef PYTHREE
			char *mod_name = NULL;
			PyObject *zero = PyUnicode_AsUTF8String(module_name);
			if (zero) {
				mod_name = PyString_AsString(zero);
			}
#else
			char *mod_name = PyString_AsString(module_name);
#endif
			if (mod_name && strcmp(mod_name, "exceptions") ) {
				char *ret = uwsgi_concat3(mod_name, ".", class_name);
#ifdef PYTHREE
				Py_DECREF(zero);
#endif
				Py_DECREF(module_name);
				return ret;
			}
			Py_DECREF(module_name);
			return uwsgi_str(class_name);
		}
	}

	return NULL;
}

struct uwsgi_buffer *uwsgi_python_backtrace(struct wsgi_request *wsgi_req) {
	PyObject *type = NULL;
        PyObject *value = NULL;
        PyObject *traceback = NULL;
	struct uwsgi_buffer *ub = NULL;

	PyErr_Fetch(&type, &value, &traceback);
        PyErr_NormalizeException(&type, &value, &traceback);

	// traceback could not be available
	if (!traceback) goto end;

	PyObject *traceback_module = PyImport_ImportModule("traceback");
	if (!traceback_module) {
		goto end;
	}

	PyObject *traceback_dict = PyModule_GetDict(traceback_module);
	PyObject *extract_tb = PyDict_GetItemString(traceback_dict, "extract_tb");
	if (!extract_tb) goto end;
	PyObject *args = PyTuple_New(1);
	Py_INCREF(traceback);
	PyTuple_SetItem(args, 0, traceback);
	PyObject *result = PyEval_CallObject(extract_tb, args);
	Py_DECREF(args);

	if (!result) goto end;

	ub = uwsgi_buffer_new(4096);
	Py_ssize_t i;
	// we have to build a uwsgi array with 5 items (4 are taken from the python tb)
	for(i=0;i< PyList_Size(result);i++) {
		PyObject *t = PyList_GetItem(result, i);
		PyObject *tb_filename = PyTuple_GetItem(t, 0);
		PyObject *tb_lineno = PyTuple_GetItem(t, 1);
		PyObject *tb_function = PyTuple_GetItem(t, 2);
		PyObject *tb_text = PyTuple_GetItem(t, 3);

		int64_t line_no = PyInt_AsLong(tb_lineno);

#ifdef PYTHREE
		PyObject *zero = PyUnicode_AsUTF8String(tb_filename);
		if (!zero) goto end0;

		// filename
                if (uwsgi_buffer_u16le(ub, PyString_Size(zero))) { Py_DECREF(zero); goto end0; }
                if (uwsgi_buffer_append(ub, PyString_AsString(zero), PyString_Size(zero))) { Py_DECREF(zero); goto end0; }

		Py_DECREF(zero);

                // lineno
                if (uwsgi_buffer_append_valnum(ub, line_no)) goto end0;

		zero = PyUnicode_AsUTF8String(tb_function);
                if (!zero) goto end0;

                // function
                if (uwsgi_buffer_u16le(ub, PyString_Size(zero))) { Py_DECREF(zero); goto end0; }
                if (uwsgi_buffer_append(ub, PyString_AsString(zero), PyString_Size(zero))) { Py_DECREF(zero); goto end0; }

		Py_DECREF(zero);

		zero = PyUnicode_AsUTF8String(tb_text);
                if (!zero) goto end0;

                // text
                if (uwsgi_buffer_u16le(ub, PyString_Size(zero))) { Py_DECREF(zero); goto end0; }
                if (uwsgi_buffer_append(ub, PyString_AsString(zero), PyString_Size(zero))) { Py_DECREF(zero); goto end0; }

		Py_DECREF(zero);
		
#else
		// filename
		if (uwsgi_buffer_u16le(ub, PyString_Size(tb_filename))) goto end0;
		if (uwsgi_buffer_append(ub, PyString_AsString(tb_filename), PyString_Size(tb_filename))) goto end0;

		// lineno
		if (uwsgi_buffer_append_valnum(ub, line_no)) goto end0;

		// function
		if (uwsgi_buffer_u16le(ub, PyString_Size(tb_function))) goto end0;
                if (uwsgi_buffer_append(ub, PyString_AsString(tb_function), PyString_Size(tb_function))) goto end0;

		// text
		if (uwsgi_buffer_u16le(ub, PyString_Size(tb_text))) goto end0;
                if (uwsgi_buffer_append(ub, PyString_AsString(tb_text), PyString_Size(tb_text))) goto end0;
#endif

		// custom (unused)
		if (uwsgi_buffer_u16le(ub, 0)) goto end0;
                if (uwsgi_buffer_append(ub, "", 0)) goto end0;
		
	}

	Py_DECREF(result);
	goto end;

end0:
	Py_DECREF(result);
	uwsgi_buffer_destroy(ub);
	ub = NULL;
end:
        PyErr_Restore(type, value, traceback);
        return ub;
}


struct uwsgi_buffer *uwsgi_python_exception_class(struct wsgi_request *wsgi_req) {
	PyObject *type = NULL;
        PyObject *value = NULL;
        PyObject *traceback = NULL;
	struct uwsgi_buffer *ub = NULL;

	PyErr_Fetch(&type, &value, &traceback);
	PyErr_NormalizeException(&type, &value, &traceback);

	char *class = uwsgi_python_get_exception_type(type);
	if (class) {
		size_t class_len = strlen(class);
		ub = uwsgi_buffer_new(class_len);
		if (uwsgi_buffer_append(ub, class, class_len)) {
			uwsgi_buffer_destroy(ub);
			ub = NULL;
			goto end;
		}
	}
end:
	free(class);
	PyErr_Restore(type, value, traceback);
	return ub;
}

struct uwsgi_buffer *uwsgi_python_exception_msg(struct wsgi_request *wsgi_req) {
        PyObject *type = NULL;
        PyObject *value = NULL;
        PyObject *traceback = NULL;
        struct uwsgi_buffer *ub = NULL;

        PyErr_Fetch(&type, &value, &traceback);
        PyErr_NormalizeException(&type, &value, &traceback);

	// value could be NULL ?
	if (!value) goto end;

#ifdef PYTHREE
	char *msg = NULL;
	PyObject *zero = PyUnicode_AsUTF8String( PyObject_Str(value) );
	if (zero) {
        	msg = PyString_AsString( zero );
	}
#else
        char *msg = PyString_AsString( PyObject_Str(value) );
#endif
        if (msg) {
                size_t msg_len = strlen(msg);
                ub = uwsgi_buffer_new(msg_len);
                if (uwsgi_buffer_append(ub, msg, msg_len)) {
#ifdef PYTHREE
			Py_DECREF(zero);
#endif
                        uwsgi_buffer_destroy(ub);
                        ub = NULL;
                        goto end;
                }
#ifdef PYTHREE
		Py_DECREF(zero);
#endif
        }
end:
        PyErr_Restore(type, value, traceback);
        return ub;
}

struct uwsgi_buffer *uwsgi_python_exception_repr(struct wsgi_request *wsgi_req) {
	
	struct uwsgi_buffer *ub_class = uwsgi_python_exception_class(wsgi_req);
	if (!ub_class) return NULL;

	struct uwsgi_buffer *ub_msg = uwsgi_python_exception_msg(wsgi_req);
	if (!ub_msg) {
		uwsgi_buffer_destroy(ub_class);
		return NULL;
	}

	struct uwsgi_buffer *ub = uwsgi_buffer_new(ub_class->pos + 2 + ub_msg->pos);
	if (uwsgi_buffer_append(ub, ub_class->buf, ub_class->pos)) goto error;
	if (uwsgi_buffer_append(ub, ": ", 2)) goto error;
	if (uwsgi_buffer_append(ub, ub_msg->buf, ub_msg->pos)) goto error;

	uwsgi_buffer_destroy(ub_class);
	uwsgi_buffer_destroy(ub_msg);

	return ub;

error:
	uwsgi_buffer_destroy(ub_class);
	uwsgi_buffer_destroy(ub_msg);
	uwsgi_buffer_destroy(ub);
	return NULL;
}

void uwsgi_python_exception_log(struct wsgi_request *wsgi_req) {
	PyErr_Print();
}

PyObject *python_call(PyObject *callable, PyObject *args, int catch, struct wsgi_request *wsgi_req) {

	//uwsgi_log("ready to call %p %p\n", callable, args);

	PyObject *pyret = PyEval_CallObject(callable, args);

	//uwsgi_log("called\n");

	if (PyErr_Occurred()) {
		if (wsgi_req) {
			uwsgi_manage_exception(wsgi_req, catch);
		}
		else {
			PyErr_Print();
		}
	}

#ifdef UWSGI_DEBUG
	if (pyret) {
		uwsgi_debug("called %p %p %d\n", callable, args, pyret->ob_refcnt);
	}
#endif

	return pyret;
}

int uwsgi_python_call(struct wsgi_request *wsgi_req, PyObject *callable, PyObject *args) {

	wsgi_req->async_result = python_call(callable, args, 0, wsgi_req);

	if (wsgi_req->async_result) {
		while ( manage_python_response(wsgi_req) != UWSGI_OK) {
			if (uwsgi.async > 1) {
				return UWSGI_AGAIN;
			}
		}
	}

	return UWSGI_OK;
}

void init_pyargv() {

	char *ap;

	char *argv0 = "uwsgi";

	if (up.pyrun) {
		argv0 = up.pyrun;
	}

#ifdef PYTHREE
	wchar_t *pname = uwsgi_calloc(sizeof(wchar_t) * (strlen(argv0)+1));
	mbstowcs(pname, argv0, strlen(argv0)+1);
#else
	char *pname = argv0;
#endif

	up.argc = 1;
	if (up.argv) {
		char *tmp_ptr = uwsgi_str(up.argv);
#ifdef __sun__
                // FIX THIS !!!
		char *ctx = NULL;
                ap = strtok_r(tmp_ptr, " ", &ctx);
                while ((ap = strtok_r(NULL, " ", &ctx)) != NULL) {
#else
                while ((ap = strsep(&tmp_ptr, " \t")) != NULL) {
#endif
			if (*ap != '\0') {
				up.argc++;
			}
		}

		free(tmp_ptr);
	}

#ifdef PYTHREE
	up.py_argv = uwsgi_calloc(sizeof(wchar_t *) * up.argc+1);
#else
	up.py_argv = uwsgi_calloc(sizeof(char *) * up.argc+1);
#endif

	up.py_argv[0] = pname;


	if (up.argv) {

		char *py_argv_copy = uwsgi_str(up.argv);
		up.argc = 1;
#ifdef PYTHREE
		wchar_t *wcargv = uwsgi_calloc( sizeof( wchar_t ) * (strlen(py_argv_copy)+1));
#endif

#ifdef __sun__
		// FIX THIS !!!
		char *ctx = NULL;
		ap = strtok_r(py_argv_copy, " ", &ctx);
		while ((ap = strtok_r(NULL, " ", &ctx)) != NULL) {
#else
		while ((ap = strsep(&py_argv_copy, " \t")) != NULL) {
#endif
				if (*ap != '\0') {
#ifdef PYTHREE
					mbstowcs( wcargv, ap, strlen(ap));
					up.py_argv[up.argc] = wcargv;
					wcargv += strlen(ap) + 1;
#else
					up.py_argv[up.argc] = ap;
#endif
					up.argc++;
				}
		}

	}

	PySys_SetArgv(up.argc, up.py_argv);

	PyObject *sys_dict = get_uwsgi_pydict("sys");
	if (!sys_dict) {
		uwsgi_log("unable to load python sys module !!!\n");
		exit(1);
	}
#ifdef PYTHREE
	PyDict_SetItemString(sys_dict, "executable", PyUnicode_FromString(uwsgi.binary_path));
#else
	PyDict_SetItemString(sys_dict, "executable", PyString_FromString(uwsgi.binary_path));
#endif


}