Logging: use for Python API
[blender.git] / source / blender / python / generic / py_capi_utils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/python/generic/py_capi_utils.c
22  *  \ingroup pygen
23  *
24  * Extend upon CPython's API, filling in some gaps, these functions use PyC_
25  * prefix to distinguish them apart from CPython.
26  *
27  * \note
28  * This module should only depend on CPython, however it currently uses
29  * BLI_string_utf8() for unicode conversion.
30  */
31
32 #include <Python.h>
33 #include <frameobject.h>
34
35 #include "BLI_utildefines.h"  /* for bool */
36
37 #include "py_capi_utils.h"
38
39 #include "python_utildefines.h"
40
41 #include "BLI_string.h"
42
43 #ifndef MATH_STANDALONE
44 /* only for BLI_strncpy_wchar_from_utf8, should replace with py funcs but too late in release now */
45 #include "BLI_string_utf8.h"
46 #endif
47
48 #ifdef _WIN32
49 #include "BLI_path_util.h"  /* BLI_setenv() */
50 #include "BLI_math_base.h"  /* isfinite() */
51 #endif
52
53 /* array utility function */
54 int PyC_AsArray_FAST(
55         void *array, PyObject *value_fast, const Py_ssize_t length,
56         const PyTypeObject *type, const bool is_double, const char *error_prefix)
57 {
58         const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
59         PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
60         Py_ssize_t i;
61
62         BLI_assert(PyList_Check(value_fast) || PyTuple_Check(value_fast));
63
64         if (value_len != length) {
65                 PyErr_Format(PyExc_TypeError,
66                              "%.200s: invalid sequence length. expected %d, got %d",
67                              error_prefix, length, value_len);
68                 return -1;
69         }
70
71         /* for each type */
72         if (type == &PyFloat_Type) {
73                 if (is_double) {
74                         double *array_double = array;
75                         for (i = 0; i < length; i++) {
76                                 array_double[i] = PyFloat_AsDouble(value_fast_items[i]);
77                         }
78                 }
79                 else {
80                         float *array_float = array;
81                         for (i = 0; i < length; i++) {
82                                 array_float[i] = PyFloat_AsDouble(value_fast_items[i]);
83                         }
84                 }
85         }
86         else if (type == &PyLong_Type) {
87                 /* could use is_double for 'long int' but no use now */
88                 int *array_int = array;
89                 for (i = 0; i < length; i++) {
90                         array_int[i] = PyC_Long_AsI32(value_fast_items[i]);
91                 }
92         }
93         else if (type == &PyBool_Type) {
94                 int *array_bool = array;
95                 for (i = 0; i < length; i++) {
96                         array_bool[i] = (PyLong_AsLong(value_fast_items[i]) != 0);
97                 }
98         }
99         else {
100                 PyErr_Format(PyExc_TypeError,
101                              "%s: internal error %s is invalid",
102                              error_prefix, type->tp_name);
103                 return -1;
104         }
105
106         if (PyErr_Occurred()) {
107                 PyErr_Format(PyExc_TypeError,
108                              "%s: one or more items could not be used as a %s",
109                              error_prefix, type->tp_name);
110                 return -1;
111         }
112
113         return 0;
114 }
115
116 int PyC_AsArray(
117         void *array, PyObject *value, const Py_ssize_t length,
118         const PyTypeObject *type, const bool is_double, const char *error_prefix)
119 {
120         PyObject *value_fast;
121         int ret;
122
123         if (!(value_fast = PySequence_Fast(value, error_prefix))) {
124                 return -1;
125         }
126
127         ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
128         Py_DECREF(value_fast);
129         return ret;
130 }
131
132 /* -------------------------------------------------------------------- */
133 /** \name Typed Tuple Packing
134  *
135  * \note See #PyC_Tuple_Pack_* macros that take multiple arguments.
136  *
137  * \{ */
138
139 /* array utility function */
140 PyObject *PyC_Tuple_PackArray_F32(const float *array, uint len)
141 {
142         PyObject *tuple = PyTuple_New(len);
143         for (uint i = 0; i < len; i++) {
144                 PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array[i]));
145         }
146         return tuple;
147 }
148
149 PyObject *PyC_Tuple_PackArray_I32(const int *array, uint len)
150 {
151         PyObject *tuple = PyTuple_New(len);
152         for (uint i = 0; i < len; i++) {
153                 PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array[i]));
154         }
155         return tuple;
156 }
157
158 PyObject *PyC_Tuple_PackArray_I32FromBool(const int *array, uint len)
159 {
160         PyObject *tuple = PyTuple_New(len);
161         for (uint i = 0; i < len; i++) {
162                 PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
163         }
164         return tuple;
165 }
166
167 PyObject *PyC_Tuple_PackArray_Bool(const bool *array, uint len)
168 {
169         PyObject *tuple = PyTuple_New(len);
170         for (uint i = 0; i < len; i++) {
171                 PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
172         }
173         return tuple;
174 }
175
176 /** \} */
177
178 /**
179  * Caller needs to ensure tuple is uninitialized.
180  * Handy for filling a tuple with None for eg.
181  */
182 void PyC_Tuple_Fill(PyObject *tuple, PyObject *value)
183 {
184         unsigned int tot = PyTuple_GET_SIZE(tuple);
185         unsigned int i;
186
187         for (i = 0; i < tot; i++) {
188                 PyTuple_SET_ITEM(tuple, i, value);
189                 Py_INCREF(value);
190         }
191 }
192
193 void PyC_List_Fill(PyObject *list, PyObject *value)
194 {
195         unsigned int tot = PyList_GET_SIZE(list);
196         unsigned int i;
197
198         for (i = 0; i < tot; i++) {
199                 PyList_SET_ITEM(list, i, value);
200                 Py_INCREF(value);
201         }
202 }
203
204 /**
205  * Use with PyArg_ParseTuple's "O&" formatting.
206  *
207  * \see #PyC_Long_AsBool for a similar function to use outside of argument parsing.
208  */
209 int PyC_ParseBool(PyObject *o, void *p)
210 {
211         bool *bool_p = p;
212         long value;
213         if (((value = PyLong_AsLong(o)) == -1) || !ELEM(value, 0, 1)) {
214                 PyErr_Format(PyExc_ValueError,
215                              "expected a bool or int (0/1), got %s",
216                              Py_TYPE(o)->tp_name);
217                 return 0;
218         }
219
220         *bool_p = value ? true : false;
221         return 1;
222 }
223
224
225 #ifndef MATH_STANDALONE
226
227 /* for debugging */
228 void PyC_ObSpit(const char *name, PyObject *var)
229 {
230         const char *null_str = "<null>";
231         fprintf(stderr, "<%s> : ", name);
232         if (var == NULL) {
233                 fprintf(stderr, "%s\n", null_str);
234         }
235         else {
236                 PyObject_Print(var, stderr, 0);
237                 const PyTypeObject *type = Py_TYPE(var);
238                 fprintf(stderr,
239                         " ref:%d, ptr:%p, type: %s\n",
240                         (int)var->ob_refcnt, (void *)var, type ? type->tp_name : null_str);
241         }
242 }
243
244 /**
245  * A version of #PyC_ObSpit that writes into a string (and doesn't take a name argument).
246  * Use for logging.
247  */
248 void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var)
249 {
250         /* No name, creator of string can manage that. */
251         const char *null_str = "<null>";
252         if (var == NULL) {
253                 BLI_snprintf(result, result_len, "%s", null_str);
254         }
255         else {
256                 const PyTypeObject *type = Py_TYPE(var);
257                 PyObject *var_str = PyObject_Repr(var);
258                 if (var_str == NULL) {
259                         /* We could print error here, but this may be used for generating errors - so don't for now. */
260                         PyErr_Clear();
261                 }
262                 BLI_snprintf(
263                         result, result_len,
264                         " ref=%d, ptr=%p, type=%s, value=%.200s",
265                         (int)var->ob_refcnt,
266                         (void *)var,
267                         type ? type->tp_name : null_str,
268                         var_str ? _PyUnicode_AsString(var_str) : "<error>");
269                 if (var_str != NULL) {
270                         Py_DECREF(var_str);
271                 }
272         }
273 }
274
275 void PyC_LineSpit(void)
276 {
277
278         const char *filename;
279         int lineno;
280
281         /* Note, allow calling from outside python (RNA) */
282         if (!PyC_IsInterpreterActive()) {
283                 fprintf(stderr, "python line lookup failed, interpreter inactive\n");
284                 return;
285         }
286
287         PyErr_Clear();
288         PyC_FileAndNum(&filename, &lineno);
289         
290         fprintf(stderr, "%s:%d\n", filename, lineno);
291 }
292
293 void PyC_StackSpit(void)
294 {
295         /* Note, allow calling from outside python (RNA) */
296         if (!PyC_IsInterpreterActive()) {
297                 fprintf(stderr, "python line lookup failed, interpreter inactive\n");
298                 return;
299         }
300         else {
301                 /* lame but handy */
302                 PyGILState_STATE gilstate = PyGILState_Ensure();
303                 PyRun_SimpleString("__import__('traceback').print_stack()");
304                 PyGILState_Release(gilstate);
305         }
306 }
307
308 void PyC_FileAndNum(const char **filename, int *lineno)
309 {
310         PyFrameObject *frame;
311         
312         if (filename) *filename = NULL;
313         if (lineno)   *lineno = -1;
314
315         if (!(frame = PyThreadState_GET()->frame)) {
316                 return;
317         }
318
319         /* when executing a script */
320         if (filename) {
321                 *filename = _PyUnicode_AsString(frame->f_code->co_filename);
322         }
323
324         /* when executing a module */
325         if (filename && *filename == NULL) {
326                 /* try an alternative method to get the filename - module based
327                  * references below are all borrowed (double checked) */
328                 PyObject *mod_name = PyDict_GetItemString(PyEval_GetGlobals(), "__name__");
329                 if (mod_name) {
330                         PyObject *mod = PyDict_GetItem(PyImport_GetModuleDict(), mod_name);
331                         if (mod) {
332                                 PyObject *mod_file = PyModule_GetFilenameObject(mod);
333                                 if (mod_file) {
334                                         *filename = _PyUnicode_AsString(mod_name);
335                                         Py_DECREF(mod_file);
336                                 }
337                                 else {
338                                         PyErr_Clear();
339                                 }
340                         }
341
342                         /* unlikely, fallback */
343                         if (*filename == NULL) {
344                                 *filename = _PyUnicode_AsString(mod_name);
345                         }
346                 }
347         }
348
349         if (lineno) {
350                 *lineno = PyFrame_GetLineNumber(frame);
351         }
352 }
353
354 void PyC_FileAndNum_Safe(const char **filename, int *lineno)
355 {
356         if (!PyC_IsInterpreterActive()) {
357                 return;
358         }
359
360         PyC_FileAndNum(filename, lineno);
361 }
362
363 /* Would be nice if python had this built in */
364 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
365 {
366         Py_ssize_t i;
367         PyObject *item = o;
368         const char *attr;
369         
370         va_list vargs;
371
372         va_start(vargs, n);
373         for (i = 0; i < n; i++) {
374                 attr = va_arg(vargs, char *);
375                 item = PyObject_GetAttrString(item, attr);
376                 
377                 if (item) 
378                         Py_DECREF(item);
379                 else /* python will set the error value here */
380                         break;
381                 
382         }
383         va_end(vargs);
384         
385         Py_XINCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
386         return item;
387 }
388
389 PyObject *PyC_FrozenSetFromStrings(const char **strings)
390 {
391         const char **str;
392         PyObject *ret;
393
394         ret = PyFrozenSet_New(NULL);
395
396         for (str = strings; *str; str++) {
397                 PyObject *py_str = PyUnicode_FromString(*str);
398                 PySet_Add(ret, py_str);
399                 Py_DECREF(py_str);
400         }
401
402         return ret;
403 }
404
405
406 /**
407  * Similar to #PyErr_Format(),
408  *
409  * Implementation - we cant actually prepend the existing exception,
410  * because it could have _any_ arguments given to it, so instead we get its
411  * ``__str__`` output and raise our own exception including it.
412  */
413 PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...)
414 {
415         PyObject *error_value_prefix;
416         va_list args;
417
418         va_start(args, format);
419         error_value_prefix = PyUnicode_FromFormatV(format, args); /* can fail and be NULL */
420         va_end(args);
421
422         if (PyErr_Occurred()) {
423                 PyObject *error_type, *error_value, *error_traceback;
424                 PyErr_Fetch(&error_type, &error_value, &error_traceback);
425                 PyErr_Format(exception_type_prefix,
426                              "%S, %.200s(%S)",
427                              error_value_prefix,
428                              Py_TYPE(error_value)->tp_name,
429                              error_value
430                              );
431         }
432         else {
433                 PyErr_SetObject(exception_type_prefix,
434                                 error_value_prefix
435                                 );
436         }
437
438         Py_XDECREF(error_value_prefix);
439
440         /* dumb to always return NULL but matches PyErr_Format */
441         return NULL;
442 }
443
444 /**
445  * Use for Python callbacks run directly from C,
446  * when we can't use normal methods of raising exceptions.
447  */
448 void PyC_Err_PrintWithFunc(PyObject *py_func)
449 {
450         /* since we return to C code we can't leave the error */
451         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
452         PyErr_Print();
453         PyErr_Clear();
454
455         /* use py style error */
456         fprintf(stderr, "File \"%s\", line %d, in %s\n",
457                 _PyUnicode_AsString(f_code->co_filename),
458                 f_code->co_firstlineno,
459                 _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name)
460                 );
461 }
462
463
464 /* returns the exception string as a new PyUnicode object, depends on external traceback module */
465 #if 0
466
467 /* this version uses traceback module but somehow fails on UI errors */
468
469 PyObject *PyC_ExceptionBuffer(void)
470 {
471         PyObject *traceback_mod = NULL;
472         PyObject *format_tb_func = NULL;
473         PyObject *ret = NULL;
474
475         if (!(traceback_mod = PyImport_ImportModule("traceback"))) {
476                 goto error_cleanup;
477         }
478         else if (!(format_tb_func = PyObject_GetAttrString(traceback_mod, "format_exc"))) {
479                 goto error_cleanup;
480         }
481
482         ret = PyObject_CallObject(format_tb_func, NULL);
483
484         if (ret == Py_None) {
485                 Py_DECREF(ret);
486                 ret = NULL;
487         }
488
489 error_cleanup:
490         /* could not import the module so print the error and close */
491         Py_XDECREF(traceback_mod);
492         Py_XDECREF(format_tb_func);
493
494         return ret;
495 }
496 #else /* verbose, non-threadsafe version */
497 PyObject *PyC_ExceptionBuffer(void)
498 {
499         PyObject *stdout_backup = PySys_GetObject("stdout"); /* borrowed */
500         PyObject *stderr_backup = PySys_GetObject("stderr"); /* borrowed */
501         PyObject *string_io = NULL;
502         PyObject *string_io_buf = NULL;
503         PyObject *string_io_mod = NULL;
504         PyObject *string_io_getvalue = NULL;
505
506         PyObject *error_type, *error_value, *error_traceback;
507
508         if (!PyErr_Occurred())
509                 return NULL;
510
511         PyErr_Fetch(&error_type, &error_value, &error_traceback);
512
513         PyErr_Clear();
514
515         /* import io
516          * string_io = io.StringIO()
517          */
518
519         if (!(string_io_mod = PyImport_ImportModule("io"))) {
520                 goto error_cleanup;
521         }
522         else if (!(string_io = PyObject_CallMethod(string_io_mod, "StringIO", NULL))) {
523                 goto error_cleanup;
524         }
525         else if (!(string_io_getvalue = PyObject_GetAttrString(string_io, "getvalue"))) {
526                 goto error_cleanup;
527         }
528
529         Py_INCREF(stdout_backup); // since these were borrowed we don't want them freed when replaced.
530         Py_INCREF(stderr_backup);
531
532         PySys_SetObject("stdout", string_io); // both of these are freed when restoring
533         PySys_SetObject("stderr", string_io);
534
535         PyErr_Restore(error_type, error_value, error_traceback);
536         PyErr_Print(); /* print the error */
537         PyErr_Clear();
538
539         string_io_buf = PyObject_CallObject(string_io_getvalue, NULL);
540
541         PySys_SetObject("stdout", stdout_backup);
542         PySys_SetObject("stderr", stderr_backup);
543
544         Py_DECREF(stdout_backup); /* now sys owns the ref again */
545         Py_DECREF(stderr_backup);
546
547         Py_DECREF(string_io_mod);
548         Py_DECREF(string_io_getvalue);
549         Py_DECREF(string_io); /* free the original reference */
550
551         PyErr_Clear();
552         return string_io_buf;
553
554
555 error_cleanup:
556         /* could not import the module so print the error and close */
557         Py_XDECREF(string_io_mod);
558         Py_XDECREF(string_io);
559
560         PyErr_Restore(error_type, error_value, error_traceback);
561         PyErr_Print(); /* print the error */
562         PyErr_Clear();
563
564         return NULL;
565 }
566 #endif
567
568 PyObject *PyC_ExceptionBuffer_Simple(void)
569 {
570         PyObject *string_io_buf;
571
572         PyObject *error_type, *error_value, *error_traceback;
573
574         if (!PyErr_Occurred())
575                 return NULL;
576
577         PyErr_Fetch(&error_type, &error_value, &error_traceback);
578
579         if (error_value == NULL) {
580                 return NULL;
581         }
582
583         string_io_buf = PyObject_Str(error_value);
584         /* Python does this too */
585         if (UNLIKELY(string_io_buf == NULL)) {
586                 string_io_buf = PyUnicode_FromFormat(
587                         "<unprintable %s object>", Py_TYPE(error_value)->tp_name);
588         }
589
590         PyErr_Restore(error_type, error_value, error_traceback);
591
592         PyErr_Print();
593         PyErr_Clear();
594         return string_io_buf;
595 }
596
597 /* string conversion, escape non-unicode chars, coerce must be set to NULL */
598 const char *PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce)
599 {
600         const char *result;
601
602         result = _PyUnicode_AsStringAndSize(py_str, size);
603
604         if (result) {
605                 /* 99% of the time this is enough but we better support non unicode
606                  * chars since blender doesnt limit this */
607                 return result;
608         }
609         else {
610                 PyErr_Clear();
611
612                 if (PyBytes_Check(py_str)) {
613                         *size = PyBytes_GET_SIZE(py_str);
614                         return PyBytes_AS_STRING(py_str);
615                 }
616                 else if ((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
617                         *size = PyBytes_GET_SIZE(*coerce);
618                         return PyBytes_AS_STRING(*coerce);
619                 }
620                 else {
621                         /* leave error raised from EncodeFS */
622                         return NULL;
623                 }
624         }
625 }
626
627 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
628 {
629         const char *result;
630
631         result = _PyUnicode_AsString(py_str);
632
633         if (result) {
634                 /* 99% of the time this is enough but we better support non unicode
635                  * chars since blender doesnt limit this */
636                 return result;
637         }
638         else {
639                 PyErr_Clear();
640
641                 if (PyBytes_Check(py_str)) {
642                         return PyBytes_AS_STRING(py_str);
643                 }
644                 else if ((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
645                         return PyBytes_AS_STRING(*coerce);
646                 }
647                 else {
648                         /* leave error raised from EncodeFS */
649                         return NULL;
650                 }
651         }
652 }
653
654 PyObject *PyC_UnicodeFromByteAndSize(const char *str, Py_ssize_t size)
655 {
656         PyObject *result = PyUnicode_FromStringAndSize(str, size);
657         if (result) {
658                 /* 99% of the time this is enough but we better support non unicode
659                  * chars since blender doesnt limit this */
660                 return result;
661         }
662         else {
663                 PyErr_Clear();
664                 /* this means paths will always be accessible once converted, on all OS's */
665                 result = PyUnicode_DecodeFSDefaultAndSize(str, size);
666                 return result;
667         }
668 }
669
670 PyObject *PyC_UnicodeFromByte(const char *str)
671 {
672         return PyC_UnicodeFromByteAndSize(str, strlen(str));
673 }
674
675 /*****************************************************************************
676  * Description: This function creates a new Python dictionary object.
677  * note: dict is owned by sys.modules["__main__"] module, reference is borrowed
678  * note: important we use the dict from __main__, this is what python expects
679  *  for 'pickle' to work as well as strings like this...
680  * >> foo = 10
681  * >> print(__import__("__main__").foo)
682  *
683  * note: this overwrites __main__ which gives problems with nested calls.
684  * be sure to run PyC_MainModule_Backup & PyC_MainModule_Restore if there is
685  * any chance that python is in the call stack.
686  ****************************************************************************/
687 PyObject *PyC_DefaultNameSpace(const char *filename)
688 {
689         PyInterpreterState *interp = PyThreadState_GET()->interp;
690         PyObject *mod_main = PyModule_New("__main__");
691         PyDict_SetItemString(interp->modules, "__main__", mod_main);
692         Py_DECREF(mod_main); /* sys.modules owns now */
693         PyModule_AddStringConstant(mod_main, "__name__", "__main__");
694         if (filename) {
695                 /* __file__ mainly for nice UI'ness
696                  * note: this wont map to a real file when executing text-blocks and buttons. */
697                 PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromByte(filename));
698         }
699         PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
700         Py_INCREF(interp->builtins); /* AddObject steals a reference */
701         return PyModule_GetDict(mod_main);
702 }
703
704 /* restore MUST be called after this */
705 void PyC_MainModule_Backup(PyObject **main_mod)
706 {
707         PyInterpreterState *interp = PyThreadState_GET()->interp;
708         *main_mod = PyDict_GetItemString(interp->modules, "__main__");
709         Py_XINCREF(*main_mod); /* don't free */
710 }
711
712 void PyC_MainModule_Restore(PyObject *main_mod)
713 {
714         PyInterpreterState *interp = PyThreadState_GET()->interp;
715         PyDict_SetItemString(interp->modules, "__main__", main_mod);
716         Py_XDECREF(main_mod);
717 }
718
719 /* must be called before Py_Initialize, expects output of BKE_appdir_folder_id(BLENDER_PYTHON, NULL) */
720 void PyC_SetHomePath(const char *py_path_bundle)
721 {
722         if (py_path_bundle == NULL) {
723                 /* Common enough to have bundled *nix python but complain on OSX/Win */
724 #if defined(__APPLE__) || defined(_WIN32)
725                 fprintf(stderr, "Warning! bundled python not found and is expected on this platform. "
726                         "(if you built with CMake: 'install' target may have not been built)\n");
727 #endif
728                 return;
729         }
730         /* set the environment path */
731         printf("found bundled python: %s\n", py_path_bundle);
732
733 #ifdef __APPLE__
734         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
735          * but current Python lib (release 3.1.1) doesn't handle these correctly */
736         if (strchr(py_path_bundle, ':'))
737                 printf("Warning : Blender application is located in a path containing : or / chars\
738                        \nThis may make python import function fail\n");
739 #endif
740
741
742 #if 0 /* disable for now [#31506] - campbell */
743 #ifdef _WIN32
744         /* cmake/MSVC debug build crashes without this, why only
745          * in this case is unknown.. */
746         {
747                 /*BLI_setenv("PYTHONPATH", py_path_bundle)*/;
748         }
749 #endif
750 #endif
751
752         {
753                 static wchar_t py_path_bundle_wchar[1024];
754
755                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
756                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
757
758                 BLI_strncpy_wchar_from_utf8(py_path_bundle_wchar, py_path_bundle, ARRAY_SIZE(py_path_bundle_wchar));
759
760                 Py_SetPythonHome(py_path_bundle_wchar);
761                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
762         }
763 }
764
765 bool PyC_IsInterpreterActive(void)
766 {
767         /* instead of PyThreadState_Get, which calls Py_FatalError */
768         return (PyThreadState_GetDict() != NULL);
769 }
770
771 /* Would be nice if python had this built in
772  * See: http://wiki.blender.org/index.php/Dev:Doc/Tools/Debugging/PyFromC
773  */
774 void PyC_RunQuicky(const char *filepath, int n, ...)
775 {
776         FILE *fp = fopen(filepath, "r");
777
778         if (fp) {
779                 PyGILState_STATE gilstate = PyGILState_Ensure();
780
781                 va_list vargs;
782
783                 int *sizes = PyMem_MALLOC(sizeof(int) * (n / 2));
784                 int i;
785
786                 PyObject *py_dict = PyC_DefaultNameSpace(filepath);
787                 PyObject *values = PyList_New(n / 2); /* namespace owns this, don't free */
788
789                 PyObject *py_result, *ret;
790
791                 PyObject *struct_mod = PyImport_ImportModule("struct");
792                 PyObject *calcsize = PyObject_GetAttrString(struct_mod, "calcsize"); /* struct.calcsize */
793                 PyObject *pack = PyObject_GetAttrString(struct_mod, "pack"); /* struct.pack */
794                 PyObject *unpack = PyObject_GetAttrString(struct_mod, "unpack"); /* struct.unpack */
795
796                 Py_DECREF(struct_mod);
797
798                 va_start(vargs, n);
799                 for (i = 0; i * 2 < n; i++) {
800                         const char *format = va_arg(vargs, char *);
801                         void *ptr = va_arg(vargs, void *);
802
803                         ret = PyObject_CallFunction(calcsize, "s", format);
804
805                         if (ret) {
806                                 sizes[i] = PyLong_AsLong(ret);
807                                 Py_DECREF(ret);
808                                 ret = PyObject_CallFunction(unpack, "sy#", format, (char *)ptr, sizes[i]);
809                         }
810
811                         if (ret == NULL) {
812                                 printf("%s error, line:%d\n", __func__, __LINE__);
813                                 PyErr_Print();
814                                 PyErr_Clear();
815
816                                 PyList_SET_ITEM(values, i, Py_INCREF_RET(Py_None)); /* hold user */
817
818                                 sizes[i] = 0;
819                         }
820                         else {
821                                 if (PyTuple_GET_SIZE(ret) == 1) {
822                                         /* convenience, convert single tuples into single values */
823                                         PyObject *tmp = PyTuple_GET_ITEM(ret, 0);
824                                         Py_INCREF(tmp);
825                                         Py_DECREF(ret);
826                                         ret = tmp;
827                                 }
828
829                                 PyList_SET_ITEM(values, i, ret); /* hold user */
830                         }
831                 }
832                 va_end(vargs);
833                 
834                 /* set the value so we can access it */
835                 PyDict_SetItemString(py_dict, "values", values);
836                 Py_DECREF(values);
837
838                 py_result = PyRun_File(fp, filepath, Py_file_input, py_dict, py_dict);
839
840                 fclose(fp);
841
842                 if (py_result) {
843
844                         /* we could skip this but then only slice assignment would work
845                          * better not be so strict */
846                         values = PyDict_GetItemString(py_dict, "values");
847
848                         if (values && PyList_Check(values)) {
849
850                                 /* don't use the result */
851                                 Py_DECREF(py_result);
852                                 py_result = NULL;
853
854                                 /* now get the values back */
855                                 va_start(vargs, n);
856                                 for (i = 0; i * 2 < n; i++) {
857                                         const char *format = va_arg(vargs, char *);
858                                         void *ptr = va_arg(vargs, void *);
859                                         
860                                         PyObject *item;
861                                         PyObject *item_new;
862                                         /* prepend the string formatting and remake the tuple */
863                                         item = PyList_GET_ITEM(values, i);
864                                         if (PyTuple_CheckExact(item)) {
865                                                 int ofs = PyTuple_GET_SIZE(item);
866                                                 item_new = PyTuple_New(ofs + 1);
867                                                 while (ofs--) {
868                                                         PyObject *member = PyTuple_GET_ITEM(item, ofs);
869                                                         PyTuple_SET_ITEM(item_new, ofs + 1, member);
870                                                         Py_INCREF(member);
871                                                 }
872
873                                                 PyTuple_SET_ITEM(item_new, 0, PyUnicode_FromString(format));
874                                         }
875                                         else {
876                                                 item_new = Py_BuildValue("sO", format, item);
877                                         }
878
879                                         ret = PyObject_Call(pack, item_new, NULL);
880
881                                         if (ret) {
882                                                 /* copy the bytes back into memory */
883                                                 memcpy(ptr, PyBytes_AS_STRING(ret), sizes[i]);
884                                                 Py_DECREF(ret);
885                                         }
886                                         else {
887                                                 printf("%s error on arg '%d', line:%d\n", __func__, i, __LINE__);
888                                                 PyC_ObSpit("failed converting:", item_new);
889                                                 PyErr_Print();
890                                                 PyErr_Clear();
891                                         }
892
893                                         Py_DECREF(item_new);
894                                 }
895                                 va_end(vargs);
896                         }
897                         else {
898                                 printf("%s error, 'values' not a list, line:%d\n", __func__, __LINE__);
899                         }
900                 }
901                 else {
902                         printf("%s error line:%d\n", __func__, __LINE__);
903                         PyErr_Print();
904                         PyErr_Clear();
905                 }
906
907                 Py_DECREF(calcsize);
908                 Py_DECREF(pack);
909                 Py_DECREF(unpack);
910
911                 PyMem_FREE(sizes);
912
913                 PyGILState_Release(gilstate);
914         }
915         else {
916                 fprintf(stderr, "%s: '%s' missing\n", __func__, filepath);
917         }
918 }
919
920 /* generic function to avoid depending on RNA */
921 void *PyC_RNA_AsPointer(PyObject *value, const char *type_name)
922 {
923         PyObject *as_pointer;
924         PyObject *pointer;
925
926         if (STREQ(Py_TYPE(value)->tp_name, type_name) &&
927             (as_pointer = PyObject_GetAttrString(value, "as_pointer")) != NULL &&
928             PyCallable_Check(as_pointer))
929         {
930                 void *result = NULL;
931
932                 /* must be a 'type_name' object */
933                 pointer = PyObject_CallObject(as_pointer, NULL);
934                 Py_DECREF(as_pointer);
935
936                 if (!pointer) {
937                         PyErr_SetString(PyExc_SystemError, "value.as_pointer() failed");
938                         return NULL;
939                 }
940                 result = PyLong_AsVoidPtr(pointer);
941                 Py_DECREF(pointer);
942                 if (!result) {
943                         PyErr_SetString(PyExc_SystemError, "value.as_pointer() failed");
944                 }
945
946                 return result;
947         }
948         else {
949                 PyErr_Format(PyExc_TypeError,
950                              "expected '%.200s' type found '%.200s' instead",
951                              type_name, Py_TYPE(value)->tp_name);
952                 return NULL;
953         }
954 }
955
956
957 /* PyC_FlagSet_* functions - so flags/sets can be interchanged in a generic way */
958 #include "BLI_dynstr.h"
959 #include "MEM_guardedalloc.h"
960
961 char *PyC_FlagSet_AsString(PyC_FlagSet *item)
962 {
963         DynStr *dynstr = BLI_dynstr_new();
964         PyC_FlagSet *e;
965         char *cstring;
966
967         for (e = item; item->identifier; item++) {
968                 BLI_dynstr_appendf(dynstr, (e == item) ? "'%s'" : ", '%s'", item->identifier);
969         }
970
971         cstring = BLI_dynstr_get_cstring(dynstr);
972         BLI_dynstr_free(dynstr);
973         return cstring;
974 }
975
976 int PyC_FlagSet_ValueFromID_int(PyC_FlagSet *item, const char *identifier, int *r_value)
977 {
978         for ( ; item->identifier; item++) {
979                 if (STREQ(item->identifier, identifier)) {
980                         *r_value = item->value;
981                         return 1;
982                 }
983         }
984
985         return 0;
986 }
987
988 int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int *r_value, const char *error_prefix)
989 {
990         if (PyC_FlagSet_ValueFromID_int(item, identifier, r_value) == 0) {
991                 const char *enum_str = PyC_FlagSet_AsString(item);
992                 PyErr_Format(PyExc_ValueError,
993                              "%s: '%.200s' not found in (%s)",
994                              error_prefix, identifier, enum_str);
995                 MEM_freeN((void *)enum_str);
996                 return -1;
997         }
998
999         return 0;
1000 }
1001
1002 int PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix)
1003 {
1004         /* set of enum items, concatenate all values with OR */
1005         int ret, flag = 0;
1006
1007         /* set looping */
1008         Py_ssize_t pos = 0;
1009         Py_ssize_t hash = 0;
1010         PyObject *key;
1011
1012         if (!PySet_Check(value)) {
1013                 PyErr_Format(PyExc_TypeError,
1014                              "%.200s expected a set, not %.200s",
1015                              error_prefix, Py_TYPE(value)->tp_name);
1016                 return -1;
1017         }
1018
1019         *r_value = 0;
1020
1021         while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1022                 const char *param = _PyUnicode_AsString(key);
1023
1024                 if (param == NULL) {
1025                         PyErr_Format(PyExc_TypeError,
1026                                      "%.200s set must contain strings, not %.200s",
1027                                      error_prefix, Py_TYPE(key)->tp_name);
1028                         return -1;
1029                 }
1030
1031                 if (PyC_FlagSet_ValueFromID(items, param, &ret, error_prefix) < 0) {
1032                         return -1;
1033                 }
1034
1035                 flag |= ret;
1036         }
1037
1038         *r_value = flag;
1039         return 0;
1040 }
1041
1042 PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag)
1043 {
1044         PyObject *ret = PySet_New(NULL);
1045         PyObject *pystr;
1046
1047         for ( ; items->identifier; items++) {
1048                 if (items->value & flag) {
1049                         pystr = PyUnicode_FromString(items->identifier);
1050                         PySet_Add(ret, pystr);
1051                         Py_DECREF(pystr);
1052                 }
1053         }
1054
1055         return ret;
1056 }
1057
1058
1059 /**
1060  * \return success
1061  *
1062  * \note it is caller's responsibility to acquire & release GIL!
1063  */
1064 bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value)
1065 {
1066         PyObject *py_dict, *mod, *retval;
1067         bool ok = true;
1068         PyObject *main_mod = NULL;
1069
1070         PyC_MainModule_Backup(&main_mod);
1071
1072         py_dict = PyC_DefaultNameSpace(filename);
1073
1074         mod = PyImport_ImportModule("math");
1075         if (mod) {
1076                 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
1077                 Py_DECREF(mod);
1078         }
1079         else { /* highly unlikely but possibly */
1080                 PyErr_Print();
1081                 PyErr_Clear();
1082         }
1083
1084         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
1085
1086         if (retval == NULL) {
1087                 ok = false;
1088         }
1089         else {
1090                 double val;
1091
1092                 if (PyTuple_Check(retval)) {
1093                         /* Users my have typed in 10km, 2m
1094                          * add up all values */
1095                         int i;
1096                         val = 0.0;
1097
1098                         for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
1099                                 const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
1100                                 if (val_item == -1 && PyErr_Occurred()) {
1101                                         val = -1;
1102                                         break;
1103                                 }
1104                                 val += val_item;
1105                         }
1106                 }
1107                 else {
1108                         val = PyFloat_AsDouble(retval);
1109                 }
1110                 Py_DECREF(retval);
1111
1112                 if (val == -1 && PyErr_Occurred()) {
1113                         ok = false;
1114                 }
1115                 else if (!isfinite(val)) {
1116                         *r_value = 0.0;
1117                 }
1118                 else {
1119                         *r_value = val;
1120                 }
1121         }
1122
1123         PyC_MainModule_Restore(main_mod);
1124
1125         return ok;
1126 }
1127
1128 bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value)
1129 {
1130         PyObject *py_dict, *retval;
1131         bool ok = true;
1132         PyObject *main_mod = NULL;
1133
1134         PyC_MainModule_Backup(&main_mod);
1135
1136         py_dict = PyC_DefaultNameSpace(filename);
1137
1138         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
1139
1140         if (retval == NULL) {
1141                 ok = false;
1142         }
1143         else {
1144                 const char *val;
1145                 Py_ssize_t val_len;
1146
1147                 val = _PyUnicode_AsStringAndSize(retval, &val_len);
1148                 if (val == NULL && PyErr_Occurred()) {
1149                         ok = false;
1150                 }
1151                 else {
1152                         char *val_alloc = MEM_mallocN(val_len + 1, __func__);
1153                         memcpy(val_alloc, val, val_len + 1);
1154                         *r_value = val_alloc;
1155                 }
1156
1157                 Py_DECREF(retval);
1158         }
1159
1160         PyC_MainModule_Restore(main_mod);
1161
1162         return ok;
1163 }
1164
1165 #endif  /* #ifndef MATH_STANDALONE */
1166
1167 /* -------------------------------------------------------------------- */
1168
1169 /** \name Int Conversion
1170  *
1171  * \note Python doesn't provide overflow checks for specific bit-widths.
1172  *
1173  * \{ */
1174
1175 /* Compiler optimizes out redundant checks. */
1176 #ifdef __GNUC__
1177 #  pragma warning(push)
1178 #  pragma GCC diagnostic ignored "-Wtype-limits"
1179 #endif
1180
1181 /**
1182  * Don't use `bool` return type, so -1 can be used as an error value.
1183  */
1184 int PyC_Long_AsBool(PyObject *value)
1185 {
1186         int test = _PyLong_AsInt(value);
1187         if (UNLIKELY((uint)test > 1)) {
1188                 PyErr_SetString(PyExc_TypeError,
1189                                 "Python number not a bool (0/1)");
1190                 return -1;
1191         }
1192         return test;
1193 }
1194
1195 int8_t PyC_Long_AsI8(PyObject *value)
1196 {
1197         int test = _PyLong_AsInt(value);
1198         if (UNLIKELY(test < INT8_MIN || test > INT8_MAX)) {
1199                 PyErr_SetString(PyExc_OverflowError,
1200                                 "Python int too large to convert to C int8");
1201                 return -1;
1202         }
1203         return (int8_t)test;
1204 }
1205
1206 int16_t PyC_Long_AsI16(PyObject *value)
1207 {
1208         int test = _PyLong_AsInt(value);
1209         if (UNLIKELY(test < INT16_MIN || test > INT16_MAX)) {
1210                 PyErr_SetString(PyExc_OverflowError,
1211                                 "Python int too large to convert to C int16");
1212                 return -1;
1213         }
1214         return (int16_t)test;
1215 }
1216
1217 /* Inlined in header:
1218  * PyC_Long_AsI32
1219  * PyC_Long_AsI64
1220  */
1221
1222 uint8_t PyC_Long_AsU8(PyObject *value)
1223 {
1224         ulong test = PyLong_AsUnsignedLong(value);
1225         if (UNLIKELY(test > UINT8_MAX)) {
1226                 PyErr_SetString(PyExc_OverflowError,
1227                                 "Python int too large to convert to C uint8");
1228                 return (uint8_t)-1;
1229         }
1230         return (uint8_t)test;
1231 }
1232
1233 uint16_t PyC_Long_AsU16(PyObject *value)
1234 {
1235         ulong test = PyLong_AsUnsignedLong(value);
1236         if (UNLIKELY(test > UINT16_MAX)) {
1237                 PyErr_SetString(PyExc_OverflowError,
1238                                 "Python int too large to convert to C uint16");
1239                 return (uint16_t)-1;
1240         }
1241         return (uint16_t)test;
1242 }
1243
1244 uint32_t PyC_Long_AsU32(PyObject *value)
1245 {
1246         ulong test = PyLong_AsUnsignedLong(value);
1247         if (UNLIKELY(test > UINT32_MAX)) {
1248                 PyErr_SetString(PyExc_OverflowError,
1249                                 "Python int too large to convert to C uint32");
1250                 return (uint32_t)-1;
1251         }
1252         return (uint32_t)test;
1253 }
1254
1255 /* Inlined in header:
1256  * PyC_Long_AsU64
1257  */
1258
1259 #ifdef __GNUC__
1260 #  pragma warning(pop)
1261 #endif
1262
1263 /** \} */