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