debug build option WITH_PYTHON_UI_INFO, so you can right click and edit the python...
[blender.git] / source / blender / python / generic / py_capi_utils.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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 Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21 */
22
23 /** \file blender/python/generic/py_capi_utils.c
24  *  \ingroup pygen
25  */
26
27
28 #include <Python.h>
29 #include <frameobject.h>
30
31 #include "py_capi_utils.h"
32
33 #include "BKE_font.h" /* only for utf8towchar, should replace with py funcs but too late in release now */
34
35 #ifdef _WIN32 /* BLI_setenv */
36 #include "BLI_path_util.h"
37 #endif
38
39 /* array utility function */
40 int PyC_AsArray(void *array, PyObject *value, const int length, const PyTypeObject *type, const short is_double, const char *error_prefix)
41 {
42         PyObject *value_fast;
43         int value_len;
44         int i;
45
46         if (!(value_fast=PySequence_Fast(value, error_prefix))) {
47                 return -1;
48         }
49
50         value_len= PySequence_Fast_GET_SIZE(value_fast);
51
52         if (value_len != length) {
53                 Py_DECREF(value);
54                 PyErr_Format(PyExc_TypeError,
55                              "%.200s: invalid sequence length. expected %d, got %d",
56                              error_prefix, length, value_len);
57                 return -1;
58         }
59
60         /* for each type */
61         if (type == &PyFloat_Type) {
62                 if (is_double) {
63                         double *array_double= array;
64                         for (i=0; i<length; i++) {
65                                 array_double[i]= PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i));
66                         }
67                 }
68                 else {
69                         float *array_float= array;
70                         for (i=0; i<length; i++) {
71                                 array_float[i]= PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i));
72                         }
73                 }
74         }
75         else if (type == &PyLong_Type) {
76                 /* could use is_double for 'long int' but no use now */
77                 int *array_int= array;
78                 for (i=0; i<length; i++) {
79                         array_int[i]= PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value_fast, i));
80                 }
81         }
82         else if (type == &PyBool_Type) {
83                 int *array_bool= array;
84                 for (i=0; i<length; i++) {
85                         array_bool[i]= (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value_fast, i)) != 0);
86                 }
87         }
88         else {
89                 Py_DECREF(value_fast);
90                 PyErr_Format(PyExc_TypeError,
91                              "%s: internal error %s is invalid",
92                              error_prefix, type->tp_name);
93                 return -1;
94         }
95
96         Py_DECREF(value_fast);
97
98         if (PyErr_Occurred()) {
99                 PyErr_Format(PyExc_TypeError,
100                              "%s: one or more items could not be used as a %s",
101                              error_prefix, type->tp_name);
102                 return -1;
103         }
104
105         return 0;
106 }
107
108
109 /* for debugging */
110 void PyC_ObSpit(const char *name, PyObject *var)
111 {
112         fprintf(stderr, "<%s> : ", name);
113         if (var==NULL) {
114                 fprintf(stderr, "<NIL>");
115         }
116         else {
117                 PyObject_Print(var, stderr, 0);
118                 fprintf(stderr, " ref:%d ", (int)var->ob_refcnt);
119                 fprintf(stderr, " ptr:%p", (void *)var);
120                 
121                 fprintf(stderr, " type:");
122                 if (Py_TYPE(var))
123                         fprintf(stderr, "%s", Py_TYPE(var)->tp_name);
124                 else
125                         fprintf(stderr, "<NIL>");
126         }
127         fprintf(stderr, "\n");
128 }
129
130 void PyC_LineSpit(void)
131 {
132
133         const char *filename;
134         int lineno;
135
136         /* Note, allow calling from outside python (RNA) */
137         if (!PYC_INTERPRETER_ACTIVE) {
138                 fprintf(stderr, "python line lookup failed, interpreter inactive\n");
139                 return;
140         }
141
142         PyErr_Clear();
143         PyC_FileAndNum(&filename, &lineno);
144         
145         fprintf(stderr, "%s:%d\n", filename, lineno);
146 }
147
148 void PyC_FileAndNum(const char **filename, int *lineno)
149 {
150         PyFrameObject *frame;
151         
152         if (filename)   *filename= NULL;
153         if (lineno)             *lineno = -1;
154
155         if (!(frame= PyThreadState_GET()->frame)) {
156                 return;
157         }
158
159         /* when executing a script */
160         if (filename) {
161                 *filename = _PyUnicode_AsString(frame->f_code->co_filename);
162         }
163
164         /* when executing a module */
165         if (filename && *filename == NULL) {
166                 /* try an alternative method to get the filename - module based
167                  * references below are all borrowed (double checked) */
168                 PyObject *mod_name= PyDict_GetItemString(PyEval_GetGlobals(), "__name__");
169                 if (mod_name) {
170                         PyObject *mod= PyDict_GetItem(PyImport_GetModuleDict(), mod_name);
171                         if (mod) {
172                                 *filename= PyModule_GetFilename(mod);
173                         }
174
175                         /* unlikely, fallback */
176                         if (*filename == NULL) {
177                                 *filename= _PyUnicode_AsString(mod_name);
178                         }
179                 }
180         }
181
182         if (lineno) {
183                 *lineno = PyFrame_GetLineNumber(frame);
184         }
185 }
186
187 void PyC_FileAndNum_Safe(const char **filename, int *lineno)
188 {
189         if(!PYC_INTERPRETER_ACTIVE) {
190                 return;
191         }
192
193         PyC_FileAndNum(filename, lineno);
194 }
195
196 /* Would be nice if python had this built in */
197 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
198 {
199         Py_ssize_t i;
200         PyObject *item= o;
201         char *attr;
202         
203         va_list vargs;
204
205         va_start(vargs, n);
206         for (i=0; i<n; i++) {
207                 attr = va_arg(vargs, char *);
208                 item = PyObject_GetAttrString(item, attr);
209                 
210                 if (item) 
211                         Py_DECREF(item);
212                 else /* python will set the error value here */
213                         break;
214                 
215         }
216         va_end(vargs);
217         
218         Py_XINCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
219         return item;
220 }
221
222 /* similar to PyErr_Format(),
223  *
224  * implimentation - we cant actually preprend the existing exception,
225  * because it could have _any_ argiments given to it, so instead we get its
226  * __str__ output and raise our own exception including it.
227  */
228 PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...)
229 {
230         PyObject *error_value_prefix;
231         va_list args;
232
233         va_start(args, format);
234         error_value_prefix= PyUnicode_FromFormatV(format, args); /* can fail and be NULL */
235         va_end(args);
236
237         if (PyErr_Occurred()) {
238                 PyObject *error_type, *error_value, *error_traceback;
239                 PyErr_Fetch(&error_type, &error_value, &error_traceback);
240                 PyErr_Format(exception_type_prefix,
241                              "%S, %.200s(%S)",
242                              error_value_prefix,
243                              Py_TYPE(error_value)->tp_name,
244                              error_value
245                              );
246         }
247         else {
248                 PyErr_SetObject(exception_type_prefix,
249                                 error_value_prefix
250                                 );
251         }
252
253         Py_XDECREF(error_value_prefix);
254
255         /* dumb to always return NULL but matches PyErr_Format */
256         return NULL;
257 }
258
259
260 /* returns the exception string as a new PyUnicode object, depends on external traceback module */
261 #if 0
262
263 /* this version uses traceback module but somehow fails on UI errors */
264
265 PyObject *PyC_ExceptionBuffer(void)
266 {
267         PyObject *traceback_mod= NULL;
268         PyObject *format_tb_func= NULL;
269         PyObject *ret= NULL;
270
271         if (! (traceback_mod= PyImport_ImportModule("traceback")) ) {
272                 goto error_cleanup;
273         }
274         else if (! (format_tb_func= PyObject_GetAttrString(traceback_mod, "format_exc"))) {
275                 goto error_cleanup;
276         }
277
278         ret= PyObject_CallObject(format_tb_func, NULL);
279
280         if (ret == Py_None) {
281                 Py_DECREF(ret);
282                 ret= NULL;
283         }
284
285 error_cleanup:
286         /* could not import the module so print the error and close */
287         Py_XDECREF(traceback_mod);
288         Py_XDECREF(format_tb_func);
289
290         return ret;
291 }
292 #else /* verbose, non-threadsafe version */
293 PyObject *PyC_ExceptionBuffer(void)
294 {
295         PyObject *stdout_backup = PySys_GetObject("stdout"); /* borrowed */
296         PyObject *stderr_backup = PySys_GetObject("stderr"); /* borrowed */
297         PyObject *string_io = NULL;
298         PyObject *string_io_buf = NULL;
299         PyObject *string_io_mod= NULL;
300         PyObject *string_io_getvalue= NULL;
301
302         PyObject *error_type, *error_value, *error_traceback;
303
304         if (!PyErr_Occurred())
305                 return NULL;
306
307         PyErr_Fetch(&error_type, &error_value, &error_traceback);
308
309         PyErr_Clear();
310
311         /* import io
312          * string_io = io.StringIO()
313          */
314
315         if (! (string_io_mod= PyImport_ImportModule("io")) ) {
316                 goto error_cleanup;
317         }
318         else if (! (string_io = PyObject_CallMethod(string_io_mod, (char *)"StringIO", NULL))) {
319                 goto error_cleanup;
320         }
321         else if (! (string_io_getvalue= PyObject_GetAttrString(string_io, "getvalue"))) {
322                 goto error_cleanup;
323         }
324
325         Py_INCREF(stdout_backup); // since these were borrowed we dont want them freed when replaced.
326         Py_INCREF(stderr_backup);
327
328         PySys_SetObject("stdout", string_io); // both of these are free'd when restoring
329         PySys_SetObject("stderr", string_io);
330
331         PyErr_Restore(error_type, error_value, error_traceback);
332         PyErr_Print(); /* print the error */
333         PyErr_Clear();
334
335         string_io_buf = PyObject_CallObject(string_io_getvalue, NULL);
336
337         PySys_SetObject("stdout", stdout_backup);
338         PySys_SetObject("stderr", stderr_backup);
339
340         Py_DECREF(stdout_backup); /* now sys owns the ref again */
341         Py_DECREF(stderr_backup);
342
343         Py_DECREF(string_io_mod);
344         Py_DECREF(string_io_getvalue);
345         Py_DECREF(string_io); /* free the original reference */
346
347         PyErr_Clear();
348         return string_io_buf;
349
350
351 error_cleanup:
352         /* could not import the module so print the error and close */
353         Py_XDECREF(string_io_mod);
354         Py_XDECREF(string_io);
355
356         PyErr_Restore(error_type, error_value, error_traceback);
357         PyErr_Print(); /* print the error */
358         PyErr_Clear();
359
360         return NULL;
361 }
362 #endif
363
364
365 /* string conversion, escape non-unicode chars, coerce must be set to NULL */
366 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
367 {
368         char *result;
369
370         result= _PyUnicode_AsString(py_str);
371
372         if (result) {
373                 /* 99% of the time this is enough but we better support non unicode
374                  * chars since blender doesnt limit this */
375                 return result;
376         }
377         else {
378                 PyErr_Clear();
379
380                 if (PyBytes_Check(py_str)) {
381                         return PyBytes_AS_STRING(py_str);
382                 }
383                 else {
384                         return PyBytes_AS_STRING((*coerce= PyUnicode_EncodeFSDefault(py_str)));
385                 }
386         }
387 }
388
389 PyObject *PyC_UnicodeFromByte(const char *str)
390 {
391         PyObject *result= PyUnicode_FromString(str);
392         if (result) {
393                 /* 99% of the time this is enough but we better support non unicode
394                  * chars since blender doesnt limit this */
395                 return result;
396         }
397         else {
398                 PyErr_Clear();
399                 /* this means paths will always be accessible once converted, on all OS's */
400                 result= PyUnicode_DecodeFSDefault(str);
401                 return result;
402         }
403 }
404
405 /*****************************************************************************
406 * Description: This function creates a new Python dictionary object.
407 * note: dict is owned by sys.modules["__main__"] module, reference is borrowed
408 * note: important we use the dict from __main__, this is what python expects
409   for 'pickle' to work as well as strings like this...
410  >> foo = 10
411  >> print(__import__("__main__").foo)
412 *
413 * note: this overwrites __main__ which gives problems with nested calles.
414 * be sure to run PyC_MainModule_Backup & PyC_MainModule_Restore if there is
415 * any chance that python is in the call stack.
416 *****************************************************************************/
417 PyObject *PyC_DefaultNameSpace(const char *filename)
418 {
419         PyInterpreterState *interp= PyThreadState_GET()->interp;
420         PyObject *mod_main= PyModule_New("__main__");   
421         PyDict_SetItemString(interp->modules, "__main__", mod_main);
422         Py_DECREF(mod_main); /* sys.modules owns now */
423         PyModule_AddStringConstant(mod_main, "__name__", "__main__");
424         if (filename)
425                 PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ only for nice UI'ness */
426         PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
427         Py_INCREF(interp->builtins); /* AddObject steals a reference */
428         return PyModule_GetDict(mod_main);
429 }
430
431 /* restore MUST be called after this */
432 void PyC_MainModule_Backup(PyObject **main_mod)
433 {
434         PyInterpreterState *interp= PyThreadState_GET()->interp;
435         *main_mod= PyDict_GetItemString(interp->modules, "__main__");
436         Py_XINCREF(*main_mod); /* dont free */
437 }
438
439 void PyC_MainModule_Restore(PyObject *main_mod)
440 {
441         PyInterpreterState *interp= PyThreadState_GET()->interp;
442         PyDict_SetItemString(interp->modules, "__main__", main_mod);
443         Py_XDECREF(main_mod);
444 }
445
446 /* must be called before Py_Initialize, expects output of BLI_get_folder(BLENDER_PYTHON, NULL) */
447 void PyC_SetHomePath(const char *py_path_bundle)
448 {
449         if (py_path_bundle==NULL) {
450                 /* Common enough to have bundled *nix python but complain on OSX/Win */
451 #if defined(__APPLE__) || defined(_WIN32)
452                 fprintf(stderr, "Warning! bundled python not found and is expected on this platform. (if you built with CMake: 'install' target may have not been built)\n");
453 #endif
454                 return;
455         }
456         /* set the environment path */
457         printf("found bundled python: %s\n", py_path_bundle);
458
459 #ifdef __APPLE__
460         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
461          but current Python lib (release 3.1.1) doesn't handle these correctly */
462         if (strchr(py_path_bundle, ':'))
463                 printf("Warning : Blender application is located in a path containing : or / chars\
464                            \nThis may make python import function fail\n");
465 #endif
466
467 #ifdef _WIN32
468         /* cmake/MSVC debug build crashes without this, why only
469            in this case is unknown.. */
470         {
471                 BLI_setenv("PYTHONPATH", py_path_bundle);
472         }
473 #endif
474
475         {
476                 static wchar_t py_path_bundle_wchar[1024];
477
478                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
479                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
480
481                 utf8towchar(py_path_bundle_wchar, py_path_bundle);
482
483                 Py_SetPythonHome(py_path_bundle_wchar);
484                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
485         }
486 }
487
488 /* Would be nice if python had this built in */
489 void PyC_RunQuicky(const char *filepath, int n, ...)
490 {
491         FILE *fp= fopen(filepath, "r");
492
493         if (fp) {
494                 PyGILState_STATE gilstate= PyGILState_Ensure();
495
496                 va_list vargs;  
497
498                 int *sizes= PyMem_MALLOC(sizeof(int) * (n / 2));
499                 int i;
500
501                 PyObject *py_dict = PyC_DefaultNameSpace(filepath);
502                 PyObject *values= PyList_New(n / 2); /* namespace owns this, dont free */
503
504                 PyObject *py_result, *ret;
505
506                 PyObject *struct_mod= PyImport_ImportModule("struct");
507                 PyObject *calcsize= PyObject_GetAttrString(struct_mod, "calcsize"); /* struct.calcsize */
508                 PyObject *pack= PyObject_GetAttrString(struct_mod, "pack"); /* struct.pack */
509                 PyObject *unpack= PyObject_GetAttrString(struct_mod, "unpack"); /* struct.unpack */
510
511                 Py_DECREF(struct_mod);
512
513                 va_start(vargs, n);
514                 for (i=0; i * 2<n; i++) {
515                         char *format = va_arg(vargs, char *);
516                         void *ptr = va_arg(vargs, void *);
517
518                         ret= PyObject_CallFunction(calcsize, (char *)"s", format);
519
520                         if (ret) {
521                                 sizes[i]= PyLong_AsSsize_t(ret);
522                                 Py_DECREF(ret);
523                                 ret = PyObject_CallFunction(unpack, (char *)"sy#", format, (char *)ptr, sizes[i]);
524                         }
525
526                         if (ret == NULL) {
527                                 printf("PyC_InlineRun error, line:%d\n", __LINE__);
528                                 PyErr_Print();
529                                 PyErr_Clear();
530
531                                 PyList_SET_ITEM(values, i, Py_None); /* hold user */
532                                 Py_INCREF(Py_None);
533
534                                 sizes[i]= 0;
535                         }
536                         else {
537                                 if (PyTuple_GET_SIZE(ret) == 1) {
538                                         /* convenience, convert single tuples into single values */
539                                         PyObject *tmp= PyTuple_GET_ITEM(ret, 0);
540                                         Py_INCREF(tmp);
541                                         Py_DECREF(ret);
542                                         ret = tmp;
543                                 }
544
545                                 PyList_SET_ITEM(values, i, ret); /* hold user */
546                         }
547                 }
548                 va_end(vargs);
549                 
550                 /* set the value so we can access it */
551                 PyDict_SetItemString(py_dict, "values", values);
552
553                 py_result = PyRun_File(fp, filepath, Py_file_input, py_dict, py_dict);
554
555                 fclose(fp);
556
557                 if (py_result) {
558
559                         /* we could skip this but then only slice assignment would work
560                          * better not be so strict */
561                         values= PyDict_GetItemString(py_dict, "values");
562
563                         if (values && PyList_Check(values)) {
564
565                                 /* dont use the result */
566                                 Py_DECREF(py_result);
567                                 py_result= NULL;
568
569                                 /* now get the values back */
570                                 va_start(vargs, n);
571                                 for (i=0; i*2 <n; i++) {
572                                         char *format = va_arg(vargs, char *);
573                                         void *ptr = va_arg(vargs, void *);
574                                         
575                                         PyObject *item;
576                                         PyObject *item_new;
577                                         /* prepend the string formatting and remake the tuple */
578                                         item= PyList_GET_ITEM(values, i);
579                                         if (PyTuple_CheckExact(item)) {
580                                                 int ofs= PyTuple_GET_SIZE(item);
581                                                 item_new= PyTuple_New(ofs + 1);
582                                                 while (ofs--) {
583                                                         PyObject *member= PyTuple_GET_ITEM(item, ofs);
584                                                         PyTuple_SET_ITEM(item_new, ofs + 1, member);
585                                                         Py_INCREF(member);
586                                                 }
587
588                                                 PyTuple_SET_ITEM(item_new, 0, PyUnicode_FromString(format));
589                                         }
590                                         else {
591                                                 item_new= Py_BuildValue("sO", format, item);
592                                         }
593
594                                         ret = PyObject_Call(pack, item_new, NULL);
595
596                                         if (ret) {
597                                                 /* copy the bytes back into memory */
598                                                 memcpy(ptr, PyBytes_AS_STRING(ret), sizes[i]);
599                                                 Py_DECREF(ret);
600                                         }
601                                         else {
602                                                 printf("PyC_InlineRun error on arg '%d', line:%d\n", i, __LINE__);
603                                                 PyC_ObSpit("failed converting:", item_new);
604                                                 PyErr_Print();
605                                                 PyErr_Clear();
606                                         }
607
608                                         Py_DECREF(item_new);
609                                 }
610                                 va_end(vargs);
611                         }
612                         else {
613                                 printf("PyC_InlineRun error, 'values' not a list, line:%d\n", __LINE__);
614                         }
615                 }
616                 else {
617                         printf("PyC_InlineRun error line:%d\n", __LINE__);
618                         PyErr_Print();
619                         PyErr_Clear();
620                 }
621
622                 Py_DECREF(calcsize);
623                 Py_DECREF(pack);
624                 Py_DECREF(unpack);
625
626                 PyMem_FREE(sizes);
627
628                 PyGILState_Release(gilstate);
629         }
630 }