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