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