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