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