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