Blender  V2.93
py_capi_utils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
28 /* Future-proof, See https://docs.python.org/3/c-api/arg.html#strings-and-buffers */
29 #define PY_SSIZE_T_CLEAN
30 
31 #include <Python.h>
32 #include <frameobject.h>
33 
34 #include "BLI_utildefines.h" /* for bool */
35 
36 #include "py_capi_utils.h"
37 
38 #include "python_utildefines.h"
39 
40 #ifndef MATH_STANDALONE
41 # include "MEM_guardedalloc.h"
42 
43 # include "BLI_string.h"
44 
45 /* Only for BLI_strncpy_wchar_from_utf8,
46  * should replace with py funcs but too late in release now. */
47 # include "BLI_string_utf8.h"
48 #endif
49 
50 #ifdef _WIN32
51 # include "BLI_math_base.h" /* isfinite() */
52 #endif
53 
54 /* -------------------------------------------------------------------- */
58 /* array utility function */
60  PyObject *value_fast,
61  const Py_ssize_t length,
62  const PyTypeObject *type,
63  const bool is_double,
64  const char *error_prefix)
65 {
66  const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
67  PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
68  Py_ssize_t i;
69 
70  BLI_assert(PyList_Check(value_fast) || PyTuple_Check(value_fast));
71 
72  if (value_len != length) {
73  PyErr_Format(PyExc_TypeError,
74  "%.200s: invalid sequence length. expected %d, got %d",
75  error_prefix,
76  length,
77  value_len);
78  return -1;
79  }
80 
81  /* for each type */
82  if (type == &PyFloat_Type) {
83  if (is_double) {
84  double *array_double = array;
85  for (i = 0; i < length; i++) {
86  array_double[i] = PyFloat_AsDouble(value_fast_items[i]);
87  }
88  }
89  else {
90  float *array_float = array;
91  for (i = 0; i < length; i++) {
92  array_float[i] = PyFloat_AsDouble(value_fast_items[i]);
93  }
94  }
95  }
96  else if (type == &PyLong_Type) {
97  /* could use is_double for 'long int' but no use now */
98  int *array_int = array;
99  for (i = 0; i < length; i++) {
100  array_int[i] = PyC_Long_AsI32(value_fast_items[i]);
101  }
102  }
103  else if (type == &PyBool_Type) {
104  bool *array_bool = array;
105  for (i = 0; i < length; i++) {
106  array_bool[i] = (PyLong_AsLong(value_fast_items[i]) != 0);
107  }
108  }
109  else {
110  PyErr_Format(PyExc_TypeError, "%s: internal error %s is invalid", error_prefix, type->tp_name);
111  return -1;
112  }
113 
114  if (PyErr_Occurred()) {
115  PyErr_Format(PyExc_TypeError,
116  "%s: one or more items could not be used as a %s",
117  error_prefix,
118  type->tp_name);
119  return -1;
120  }
121 
122  return 0;
123 }
124 
125 int PyC_AsArray(void *array,
126  PyObject *value,
127  const Py_ssize_t length,
128  const PyTypeObject *type,
129  const bool is_double,
130  const char *error_prefix)
131 {
132  PyObject *value_fast;
133  int ret;
134 
135  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
136  return -1;
137  }
138 
139  ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
140  Py_DECREF(value_fast);
141  return ret;
142 }
143 
146 /* -------------------------------------------------------------------- */
152 /* array utility function */
153 PyObject *PyC_Tuple_PackArray_F32(const float *array, uint len)
154 {
155  PyObject *tuple = PyTuple_New(len);
156  for (uint i = 0; i < len; i++) {
157  PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array[i]));
158  }
159  return tuple;
160 }
161 
162 PyObject *PyC_Tuple_PackArray_F64(const double *array, uint len)
163 {
164  PyObject *tuple = PyTuple_New(len);
165  for (uint i = 0; i < len; i++) {
166  PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array[i]));
167  }
168  return tuple;
169 }
170 
171 PyObject *PyC_Tuple_PackArray_I32(const int *array, uint len)
172 {
173  PyObject *tuple = PyTuple_New(len);
174  for (uint i = 0; i < len; i++) {
175  PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array[i]));
176  }
177  return tuple;
178 }
179 
181 {
182  PyObject *tuple = PyTuple_New(len);
183  for (uint i = 0; i < len; i++) {
184  PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
185  }
186  return tuple;
187 }
188 
189 PyObject *PyC_Tuple_PackArray_Bool(const bool *array, uint len)
190 {
191  PyObject *tuple = PyTuple_New(len);
192  for (uint i = 0; i < len; i++) {
193  PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
194  }
195  return tuple;
196 }
197 
200 /* -------------------------------------------------------------------- */
208 void PyC_Tuple_Fill(PyObject *tuple, PyObject *value)
209 {
210  const uint tot = PyTuple_GET_SIZE(tuple);
211  uint i;
212 
213  for (i = 0; i < tot; i++) {
214  PyTuple_SET_ITEM(tuple, i, value);
215  Py_INCREF(value);
216  }
217 }
218 
219 void PyC_List_Fill(PyObject *list, PyObject *value)
220 {
221  const uint tot = PyList_GET_SIZE(list);
222  uint i;
223 
224  for (i = 0; i < tot; i++) {
225  PyList_SET_ITEM(list, i, value);
226  Py_INCREF(value);
227  }
228 }
229 
232 /* -------------------------------------------------------------------- */
241 int PyC_ParseBool(PyObject *o, void *p)
242 {
243  bool *bool_p = p;
244  long value;
245  if (((value = PyLong_AsLong(o)) == -1) || !ELEM(value, 0, 1)) {
246  PyErr_Format(PyExc_ValueError, "expected a bool or int (0/1), got %s", Py_TYPE(o)->tp_name);
247  return 0;
248  }
249 
250  *bool_p = value ? true : false;
251  return 1;
252 }
253 
257 int PyC_ParseStringEnum(PyObject *o, void *p)
258 {
259  struct PyC_StringEnum *e = p;
260  const char *value = PyUnicode_AsUTF8(o);
261  if (value == NULL) {
262  PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
263  return 0;
264  }
265  int i;
266  for (i = 0; e->items[i].id; i++) {
267  if (STREQ(e->items[i].id, value)) {
268  e->value_found = e->items[i].value;
269  return 1;
270  }
271  }
272 
273  /* Set as a precaution. */
274  e->value_found = -1;
275 
276  PyObject *enum_items = PyTuple_New(i);
277  for (i = 0; e->items[i].id; i++) {
278  PyTuple_SET_ITEM(enum_items, i, PyUnicode_FromString(e->items[i].id));
279  }
280  PyErr_Format(PyExc_ValueError, "expected a string in %S, got '%s'", enum_items, value);
281  Py_DECREF(enum_items);
282  return 0;
283 }
284 
286  const int value)
287 {
288  for (int i = 0; items[i].id; i++) {
289  if (items[i].value == value) {
290  return items[i].id;
291  }
292  }
293  return NULL;
294 }
295 
296 /* Silly function, we don't use arg. just check its compatible with `__deepcopy__`. */
297 int PyC_CheckArgs_DeepCopy(PyObject *args)
298 {
299  PyObject *dummy_pydict;
300  return PyArg_ParseTuple(args, "|O!:__deepcopy__", &PyDict_Type, &dummy_pydict) != 0;
301 }
302 
305 #ifndef MATH_STANDALONE
306 
307 /* -------------------------------------------------------------------- */
313 /* for debugging */
314 void PyC_ObSpit(const char *name, PyObject *var)
315 {
316  const char *null_str = "<null>";
317  fprintf(stderr, "<%s> : ", name);
318  if (var == NULL) {
319  fprintf(stderr, "%s\n", null_str);
320  }
321  else {
322  PyObject_Print(var, stderr, 0);
323  const PyTypeObject *type = Py_TYPE(var);
324  fprintf(stderr,
325  " ref:%d, ptr:%p, type: %s\n",
326  (int)var->ob_refcnt,
327  (void *)var,
328  type ? type->tp_name : null_str);
329  }
330 }
331 
336 void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var)
337 {
338  /* No name, creator of string can manage that. */
339  const char *null_str = "<null>";
340  if (var == NULL) {
341  BLI_snprintf(result, result_len, "%s", null_str);
342  }
343  else {
344  const PyTypeObject *type = Py_TYPE(var);
345  PyObject *var_str = PyObject_Repr(var);
346  if (var_str == NULL) {
347  /* We could print error here,
348  * but this may be used for generating errors - so don't for now. */
349  PyErr_Clear();
350  }
352  result_len,
353  " ref=%d, ptr=%p, type=%s, value=%.200s",
354  (int)var->ob_refcnt,
355  (void *)var,
356  type ? type->tp_name : null_str,
357  var_str ? PyUnicode_AsUTF8(var_str) : "<error>");
358  if (var_str != NULL) {
359  Py_DECREF(var_str);
360  }
361  }
362 }
363 
364 void PyC_LineSpit(void)
365 {
366 
367  const char *filename;
368  int lineno;
369 
370  /* Note, allow calling from outside python (RNA) */
371  if (!PyC_IsInterpreterActive()) {
372  fprintf(stderr, "python line lookup failed, interpreter inactive\n");
373  return;
374  }
375 
376  PyErr_Clear();
377  PyC_FileAndNum(&filename, &lineno);
378 
379  fprintf(stderr, "%s:%d\n", filename, lineno);
380 }
381 
382 void PyC_StackSpit(void)
383 {
384  /* Note, allow calling from outside python (RNA) */
385  if (!PyC_IsInterpreterActive()) {
386  fprintf(stderr, "python line lookup failed, interpreter inactive\n");
387  return;
388  }
389 
390  /* lame but handy */
391  const PyGILState_STATE gilstate = PyGILState_Ensure();
392  PyRun_SimpleString("__import__('traceback').print_stack()");
393  PyGILState_Release(gilstate);
394 }
395 
398 /* -------------------------------------------------------------------- */
402 void PyC_FileAndNum(const char **r_filename, int *r_lineno)
403 {
404  PyFrameObject *frame;
405 
406  if (r_filename) {
407  *r_filename = NULL;
408  }
409  if (r_lineno) {
410  *r_lineno = -1;
411  }
412 
413  if (!(frame = PyThreadState_GET()->frame)) {
414  return;
415  }
416 
417  /* when executing a script */
418  if (r_filename) {
419  *r_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
420  }
421 
422  /* when executing a module */
423  if (r_filename && *r_filename == NULL) {
424  /* try an alternative method to get the r_filename - module based
425  * references below are all borrowed (double checked) */
426  PyObject *mod_name = PyDict_GetItemString(PyEval_GetGlobals(), "__name__");
427  if (mod_name) {
428  PyObject *mod = PyDict_GetItem(PyImport_GetModuleDict(), mod_name);
429  if (mod) {
430  PyObject *mod_file = PyModule_GetFilenameObject(mod);
431  if (mod_file) {
432  *r_filename = PyUnicode_AsUTF8(mod_name);
433  Py_DECREF(mod_file);
434  }
435  else {
436  PyErr_Clear();
437  }
438  }
439 
440  /* unlikely, fallback */
441  if (*r_filename == NULL) {
442  *r_filename = PyUnicode_AsUTF8(mod_name);
443  }
444  }
445  }
446 
447  if (r_lineno) {
448  *r_lineno = PyFrame_GetLineNumber(frame);
449  }
450 }
451 
452 void PyC_FileAndNum_Safe(const char **r_filename, int *r_lineno)
453 {
454  if (!PyC_IsInterpreterActive()) {
455  return;
456  }
457 
458  PyC_FileAndNum(r_filename, r_lineno);
459 }
460 
463 /* -------------------------------------------------------------------- */
467 /* Would be nice if python had this built in */
468 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
469 {
470  Py_ssize_t i;
471  PyObject *item = o;
472  const char *attr;
473 
474  va_list vargs;
475 
476  va_start(vargs, n);
477  for (i = 0; i < n; i++) {
478  attr = va_arg(vargs, char *);
479  item = PyObject_GetAttrString(item, attr);
480 
481  if (item) {
482  Py_DECREF(item);
483  }
484  else {
485  /* python will set the error value here */
486  break;
487  }
488  }
489  va_end(vargs);
490 
491  Py_XINCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
492  return item;
493 }
494 
497 /* -------------------------------------------------------------------- */
501 PyObject *PyC_FrozenSetFromStrings(const char **strings)
502 {
503  const char **str;
504  PyObject *ret;
505 
506  ret = PyFrozenSet_New(NULL);
507 
508  for (str = strings; *str; str++) {
509  PyObject *py_str = PyUnicode_FromString(*str);
510  PySet_Add(ret, py_str);
511  Py_DECREF(py_str);
512  }
513 
514  return ret;
515 }
516 
519 /* -------------------------------------------------------------------- */
530 PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...)
531 {
532  PyObject *error_value_prefix;
533  va_list args;
534 
535  va_start(args, format);
536  error_value_prefix = PyUnicode_FromFormatV(format, args); /* can fail and be NULL */
537  va_end(args);
538 
539  if (PyErr_Occurred()) {
540  PyObject *error_type, *error_value, *error_traceback;
541  PyErr_Fetch(&error_type, &error_value, &error_traceback);
542 
543  if (PyUnicode_Check(error_value)) {
544  PyErr_Format(exception_type_prefix, "%S, %S", error_value_prefix, error_value);
545  }
546  else {
547  PyErr_Format(exception_type_prefix,
548  "%S, %.200s(%S)",
549  error_value_prefix,
550  Py_TYPE(error_value)->tp_name,
551  error_value);
552  }
553  }
554  else {
555  PyErr_SetObject(exception_type_prefix, error_value_prefix);
556  }
557 
558  Py_XDECREF(error_value_prefix);
559 
560  /* dumb to always return NULL but matches PyErr_Format */
561  return NULL;
562 }
563 
564 PyObject *PyC_Err_SetString_Prefix(PyObject *exception_type_prefix, const char *str)
565 {
566  return PyC_Err_Format_Prefix(exception_type_prefix, "%s", str);
567 }
568 
573 void PyC_Err_PrintWithFunc(PyObject *py_func)
574 {
575  /* since we return to C code we can't leave the error */
576  PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
577  PyErr_Print();
578  PyErr_Clear();
579 
580  /* use py style error */
581  fprintf(stderr,
582  "File \"%s\", line %d, in %s\n",
583  PyUnicode_AsUTF8(f_code->co_filename),
584  f_code->co_firstlineno,
585  PyUnicode_AsUTF8(((PyFunctionObject *)py_func)->func_name));
586 }
587 
590 /* -------------------------------------------------------------------- */
594 static void pyc_exception_buffer_handle_system_exit(PyObject *error_type,
595  PyObject *error_value,
596  PyObject *error_traceback)
597 {
598  if (!PyErr_GivenExceptionMatches(error_type, PyExc_SystemExit)) {
599  return;
600  }
601  /* Inspecting, follow Python's logic in #_Py_HandleSystemExit & treat as a regular exception. */
602  if (_Py_GetConfig()->inspect) {
603  return;
604  }
605 
606  /* NOTE(@campbellbarton): A `SystemExit` exception will exit immediately (unless inspecting).
607  * So print the error and exit now. This is necessary as the call to #PyErr_Print exits,
608  * the temporary `sys.stderr` assignment causes the output to be suppressed, failing silently.
609  * Instead, restore the error and print it. If Python changes it's behavior and doesn't exit in
610  * the future - continue to create the exception buffer, see: T99966.
611  *
612  * Arguably accessing a `SystemExit` exception as a buffer should be supported without exiting.
613  * (by temporarily enabling inspection for example) however - it's not obvious exactly when this
614  * should be enabled and complicates the Python API by introducing different kinds of execution.
615  * Since the rule of thumb is for Blender's embedded Python to match stand-alone Python,
616  * favor exiting when a `SystemExit` is raised.
617  * Especially since this exception more likely to be used for background/batch-processing
618  * utilities where exiting immediately makes sense, the possibility of this being called
619  * indirectly from python-drivers or modal-operators is less of a concern. */
620  PyErr_Restore(error_type, error_value, error_traceback);
621  PyErr_Print();
622 }
623 
624 /* returns the exception string as a new PyUnicode object, depends on external traceback module */
625 # if 0
626 
627 /* this version uses traceback module but somehow fails on UI errors */
628 
629 PyObject *PyC_ExceptionBuffer(void)
630 {
631  PyObject *traceback_mod = NULL;
632  PyObject *format_tb_func = NULL;
633  PyObject *ret = NULL;
634 
635  if (!(traceback_mod = PyImport_ImportModule("traceback"))) {
636  goto error_cleanup;
637  }
638  else if (!(format_tb_func = PyObject_GetAttrString(traceback_mod, "format_exc"))) {
639  goto error_cleanup;
640  }
641 
642  ret = PyObject_CallObject(format_tb_func, NULL);
643 
644  if (ret == Py_None) {
645  Py_DECREF(ret);
646  ret = NULL;
647  }
648 
649 error_cleanup:
650  /* could not import the module so print the error and close */
651  Py_XDECREF(traceback_mod);
652  Py_XDECREF(format_tb_func);
653 
654  return ret;
655 }
656 # else /* verbose, non-threadsafe version */
657 PyObject *PyC_ExceptionBuffer(void)
658 {
659  PyObject *stdout_backup = PySys_GetObject("stdout"); /* borrowed */
660  PyObject *stderr_backup = PySys_GetObject("stderr"); /* borrowed */
661  PyObject *string_io = NULL;
662  PyObject *string_io_buf = NULL;
663  PyObject *string_io_mod = NULL;
664  PyObject *string_io_getvalue = NULL;
665 
666  PyObject *error_type, *error_value, *error_traceback;
667 
668  if (!PyErr_Occurred()) {
669  return NULL;
670  }
671 
672  PyErr_Fetch(&error_type, &error_value, &error_traceback);
673 
674  pyc_exception_buffer_handle_system_exit(error_type, error_value, error_traceback);
675 
676  PyErr_Clear();
677 
678  /* import io
679  * string_io = io.StringIO()
680  */
681 
682  if (!(string_io_mod = PyImport_ImportModule("io"))) {
683  goto error_cleanup;
684  }
685  else if (!(string_io = PyObject_CallMethod(string_io_mod, "StringIO", NULL))) {
686  goto error_cleanup;
687  }
688  else if (!(string_io_getvalue = PyObject_GetAttrString(string_io, "getvalue"))) {
689  goto error_cleanup;
690  }
691 
692  /* Since these were borrowed we don't want them freed when replaced. */
693  Py_INCREF(stdout_backup);
694  Py_INCREF(stderr_backup);
695 
696  /* Both of these are freed when restoring. */
697  PySys_SetObject("stdout", string_io);
698  PySys_SetObject("stderr", string_io);
699 
700  PyErr_Restore(error_type, error_value, error_traceback);
701  PyErr_Print(); /* print the error */
702  PyErr_Clear();
703 
704  string_io_buf = PyObject_CallObject(string_io_getvalue, NULL);
705 
706  PySys_SetObject("stdout", stdout_backup);
707  PySys_SetObject("stderr", stderr_backup);
708 
709  Py_DECREF(stdout_backup); /* now sys owns the ref again */
710  Py_DECREF(stderr_backup);
711 
712  Py_DECREF(string_io_mod);
713  Py_DECREF(string_io_getvalue);
714  Py_DECREF(string_io); /* free the original reference */
715 
716  PyErr_Clear();
717  return string_io_buf;
718 
719 error_cleanup:
720  /* could not import the module so print the error and close */
721  Py_XDECREF(string_io_mod);
722  Py_XDECREF(string_io);
723 
724  PyErr_Restore(error_type, error_value, error_traceback);
725  PyErr_Print(); /* print the error */
726  PyErr_Clear();
727 
728  return NULL;
729 }
730 # endif
731 
733 {
734  PyObject *string_io_buf = NULL;
735 
736  PyObject *error_type, *error_value, *error_traceback;
737 
738  if (!PyErr_Occurred()) {
739  return NULL;
740  }
741 
742  PyErr_Fetch(&error_type, &error_value, &error_traceback);
743 
744  if (error_value == NULL) {
745  return NULL;
746  }
747 
748  /* Since #PyErr_Print is not called it's not essential that `SystemExit` exceptions are handled.
749  * Do this to match the behavior of #PyC_ExceptionBuffer since requesting a brief exception
750  * shouldn't result in completely different behavior. */
751  pyc_exception_buffer_handle_system_exit(error_type, error_value, error_traceback);
752 
753  if (PyErr_GivenExceptionMatches(error_type, PyExc_SyntaxError)) {
754  /* Special exception for syntax errors,
755  * in these cases the full error is verbose and not very useful,
756  * just use the initial text so we know what the error is. */
757  if (PyTuple_CheckExact(error_value) && PyTuple_GET_SIZE(error_value) >= 1) {
758  string_io_buf = PyObject_Str(PyTuple_GET_ITEM(error_value, 0));
759  }
760  }
761 
762  if (string_io_buf == NULL) {
763  string_io_buf = PyObject_Str(error_value);
764  }
765 
766  /* Python does this too */
767  if (UNLIKELY(string_io_buf == NULL)) {
768  string_io_buf = PyUnicode_FromFormat("<unprintable %s object>", Py_TYPE(error_value)->tp_name);
769  }
770 
771  PyErr_Restore(error_type, error_value, error_traceback);
772 
773  PyErr_Clear();
774  return string_io_buf;
775 }
776 
779 /* -------------------------------------------------------------------- */
785 /* string conversion, escape non-unicode chars, coerce must be set to NULL */
786 const char *PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce)
787 {
788  const char *result;
789 
790  result = PyUnicode_AsUTF8AndSize(py_str, size);
791 
792  if (result) {
793  /* 99% of the time this is enough but we better support non unicode
794  * chars since blender doesn't limit this */
795  return result;
796  }
797 
798  PyErr_Clear();
799 
800  if (PyBytes_Check(py_str)) {
801  *size = PyBytes_GET_SIZE(py_str);
802  return PyBytes_AS_STRING(py_str);
803  }
804  if ((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
805  *size = PyBytes_GET_SIZE(*coerce);
806  return PyBytes_AS_STRING(*coerce);
807  }
808 
809  /* leave error raised from EncodeFS */
810  return NULL;
811 }
812 
813 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
814 {
815  const char *result;
816 
817  result = PyUnicode_AsUTF8(py_str);
818 
819  if (result) {
820  /* 99% of the time this is enough but we better support non unicode
821  * chars since blender doesn't limit this. */
822  return result;
823  }
824 
825  PyErr_Clear();
826 
827  if (PyBytes_Check(py_str)) {
828  return PyBytes_AS_STRING(py_str);
829  }
830  if ((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
831  return PyBytes_AS_STRING(*coerce);
832  }
833 
834  /* leave error raised from EncodeFS */
835  return NULL;
836 }
837 
838 PyObject *PyC_UnicodeFromByteAndSize(const char *str, Py_ssize_t size)
839 {
840  PyObject *result = PyUnicode_FromStringAndSize(str, size);
841  if (result) {
842  /* 99% of the time this is enough but we better support non unicode
843  * chars since blender doesn't limit this */
844  return result;
845  }
846 
847  PyErr_Clear();
848  /* this means paths will always be accessible once converted, on all OS's */
849  result = PyUnicode_DecodeFSDefaultAndSize(str, size);
850  return result;
851 }
852 
853 PyObject *PyC_UnicodeFromByte(const char *str)
854 {
855  return PyC_UnicodeFromByteAndSize(str, strlen(str));
856 }
857 
860 /* -------------------------------------------------------------------- */
864 /*****************************************************************************
865  * Description: This function creates a new Python dictionary object.
866  * note: dict is owned by sys.modules["__main__"] module, reference is borrowed
867  * note: important we use the dict from __main__, this is what python expects
868  * for 'pickle' to work as well as strings like this...
869  * >> foo = 10
870  * >> print(__import__("__main__").foo)
871  *
872  * note: this overwrites __main__ which gives problems with nested calls.
873  * be sure to run PyC_MainModule_Backup & PyC_MainModule_Restore if there is
874  * any chance that python is in the call stack.
875  ****************************************************************************/
876 PyObject *PyC_DefaultNameSpace(const char *filename)
877 {
878  PyObject *modules = PyImport_GetModuleDict();
879  PyObject *builtins = PyEval_GetBuiltins();
880  PyObject *mod_main = PyModule_New("__main__");
881  PyDict_SetItemString(modules, "__main__", mod_main);
882  Py_DECREF(mod_main); /* sys.modules owns now */
883  PyModule_AddStringConstant(mod_main, "__name__", "__main__");
884  if (filename) {
885  /* __file__ mainly for nice UI'ness
886  * note: this wont map to a real file when executing text-blocks and buttons. */
887  PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromByte(filename));
888  }
889  PyModule_AddObject(mod_main, "__builtins__", builtins);
890  Py_INCREF(builtins); /* AddObject steals a reference */
891  return PyModule_GetDict(mod_main);
892 }
893 
894 bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
895 {
896  for (int i = 0; imports[i]; i++) {
897  PyObject *name = PyUnicode_FromString(imports[i]);
898  PyObject *mod = PyImport_ImportModuleLevelObject(name, NULL, NULL, 0, 0);
899  bool ok = false;
900  if (mod) {
901  PyDict_SetItem(py_dict, name, mod);
902  ok = true;
903  Py_DECREF(mod);
904  }
905  Py_DECREF(name);
906 
907  if (!ok) {
908  return false;
909  }
910  }
911  return true;
912 }
913 
914 /* restore MUST be called after this */
915 void PyC_MainModule_Backup(PyObject **r_main_mod)
916 {
917  PyObject *modules = PyImport_GetModuleDict();
918  *r_main_mod = PyDict_GetItemString(modules, "__main__");
919  Py_XINCREF(*r_main_mod); /* don't free */
920 }
921 
922 void PyC_MainModule_Restore(PyObject *main_mod)
923 {
924  PyObject *modules = PyImport_GetModuleDict();
925  PyDict_SetItemString(modules, "__main__", main_mod);
926  Py_XDECREF(main_mod);
927 }
928 
930 {
931  /* instead of PyThreadState_Get, which calls Py_FatalError */
932  return (PyThreadState_GetDict() != NULL);
933 }
934 
937 /* -------------------------------------------------------------------- */
941 /* Would be nice if python had this built in
942  * See: https://wiki.blender.org/wiki/Tools/Debugging/PyFromC
943  */
944 void PyC_RunQuicky(const char *filepath, int n, ...)
945 {
946  FILE *fp = fopen(filepath, "r");
947 
948  if (fp) {
949  const PyGILState_STATE gilstate = PyGILState_Ensure();
950 
951  va_list vargs;
952 
953  Py_ssize_t *sizes = PyMem_MALLOC(sizeof(*sizes) * (n / 2));
954  int i;
955 
956  PyObject *py_dict = PyC_DefaultNameSpace(filepath);
957  PyObject *values = PyList_New(n / 2); /* namespace owns this, don't free */
958 
959  PyObject *py_result, *ret;
960 
961  PyObject *struct_mod = PyImport_ImportModule("struct");
962  PyObject *calcsize = PyObject_GetAttrString(struct_mod, "calcsize"); /* struct.calcsize */
963  PyObject *pack = PyObject_GetAttrString(struct_mod, "pack"); /* struct.pack */
964  PyObject *unpack = PyObject_GetAttrString(struct_mod, "unpack"); /* struct.unpack */
965 
966  Py_DECREF(struct_mod);
967 
968  va_start(vargs, n);
969  for (i = 0; i * 2 < n; i++) {
970  const char *format = va_arg(vargs, char *);
971  void *ptr = va_arg(vargs, void *);
972 
973  ret = PyObject_CallFunction(calcsize, "s", format);
974 
975  if (ret) {
976  sizes[i] = PyLong_AsLong(ret);
977  Py_DECREF(ret);
978  ret = PyObject_CallFunction(unpack, "sy#", format, (char *)ptr, sizes[i]);
979  }
980 
981  if (ret == NULL) {
982  printf("%s error, line:%d\n", __func__, __LINE__);
983  PyErr_Print();
984  PyErr_Clear();
985 
986  PyList_SET_ITEM(values, i, Py_INCREF_RET(Py_None)); /* hold user */
987 
988  sizes[i] = 0;
989  }
990  else {
991  if (PyTuple_GET_SIZE(ret) == 1) {
992  /* convenience, convert single tuples into single values */
993  PyObject *tmp = PyTuple_GET_ITEM(ret, 0);
994  Py_INCREF(tmp);
995  Py_DECREF(ret);
996  ret = tmp;
997  }
998 
999  PyList_SET_ITEM(values, i, ret); /* hold user */
1000  }
1001  }
1002  va_end(vargs);
1003 
1004  /* set the value so we can access it */
1005  PyDict_SetItemString(py_dict, "values", values);
1006  Py_DECREF(values);
1007 
1008  py_result = PyRun_File(fp, filepath, Py_file_input, py_dict, py_dict);
1009 
1010  fclose(fp);
1011 
1012  if (py_result) {
1013 
1014  /* we could skip this but then only slice assignment would work
1015  * better not be so strict */
1016  values = PyDict_GetItemString(py_dict, "values");
1017 
1018  if (values && PyList_Check(values)) {
1019 
1020  /* don't use the result */
1021  Py_DECREF(py_result);
1022  py_result = NULL;
1023 
1024  /* now get the values back */
1025  va_start(vargs, n);
1026  for (i = 0; i * 2 < n; i++) {
1027  const char *format = va_arg(vargs, char *);
1028  void *ptr = va_arg(vargs, void *);
1029 
1030  PyObject *item;
1031  PyObject *item_new;
1032  /* prepend the string formatting and remake the tuple */
1033  item = PyList_GET_ITEM(values, i);
1034  if (PyTuple_CheckExact(item)) {
1035  int ofs = PyTuple_GET_SIZE(item);
1036  item_new = PyTuple_New(ofs + 1);
1037  while (ofs--) {
1038  PyObject *member = PyTuple_GET_ITEM(item, ofs);
1039  PyTuple_SET_ITEM(item_new, ofs + 1, member);
1040  Py_INCREF(member);
1041  }
1042 
1043  PyTuple_SET_ITEM(item_new, 0, PyUnicode_FromString(format));
1044  }
1045  else {
1046  item_new = Py_BuildValue("sO", format, item);
1047  }
1048 
1049  ret = PyObject_Call(pack, item_new, NULL);
1050 
1051  if (ret) {
1052  /* copy the bytes back into memory */
1053  memcpy(ptr, PyBytes_AS_STRING(ret), sizes[i]);
1054  Py_DECREF(ret);
1055  }
1056  else {
1057  printf("%s error on arg '%d', line:%d\n", __func__, i, __LINE__);
1058  PyC_ObSpit("failed converting:", item_new);
1059  PyErr_Print();
1060  PyErr_Clear();
1061  }
1062 
1063  Py_DECREF(item_new);
1064  }
1065  va_end(vargs);
1066  }
1067  else {
1068  printf("%s error, 'values' not a list, line:%d\n", __func__, __LINE__);
1069  }
1070  }
1071  else {
1072  printf("%s error line:%d\n", __func__, __LINE__);
1073  PyErr_Print();
1074  PyErr_Clear();
1075  }
1076 
1077  Py_DECREF(calcsize);
1078  Py_DECREF(pack);
1079  Py_DECREF(unpack);
1080 
1081  PyMem_FREE(sizes);
1082 
1083  PyGILState_Release(gilstate);
1084  }
1085  else {
1086  fprintf(stderr, "%s: '%s' missing\n", __func__, filepath);
1087  }
1088 }
1089 
1090 /* generic function to avoid depending on RNA */
1091 void *PyC_RNA_AsPointer(PyObject *value, const char *type_name)
1092 {
1093  PyObject *as_pointer;
1094  PyObject *pointer;
1095 
1096  if (STREQ(Py_TYPE(value)->tp_name, type_name) &&
1097  (as_pointer = PyObject_GetAttrString(value, "as_pointer")) != NULL &&
1098  PyCallable_Check(as_pointer)) {
1099  void *result = NULL;
1100 
1101  /* must be a 'type_name' object */
1102  pointer = PyObject_CallObject(as_pointer, NULL);
1103  Py_DECREF(as_pointer);
1104 
1105  if (!pointer) {
1106  PyErr_SetString(PyExc_SystemError, "value.as_pointer() failed");
1107  return NULL;
1108  }
1109  result = PyLong_AsVoidPtr(pointer);
1110  Py_DECREF(pointer);
1111  if (!result) {
1112  PyErr_SetString(PyExc_SystemError, "value.as_pointer() failed");
1113  }
1114 
1115  return result;
1116  }
1117 
1118  PyErr_Format(PyExc_TypeError,
1119  "expected '%.200s' type found '%.200s' instead",
1120  type_name,
1121  Py_TYPE(value)->tp_name);
1122  return NULL;
1123 }
1124 
1127 /* -------------------------------------------------------------------- */
1134 {
1135  PyObject *py_items = PyList_New(0);
1136  for (; item->identifier; item++) {
1137  PyList_APPEND(py_items, PyUnicode_FromString(item->identifier));
1138  }
1139  PyObject *py_string = PyObject_Repr(py_items);
1140  Py_DECREF(py_items);
1141  return py_string;
1142 }
1143 
1144 int PyC_FlagSet_ValueFromID_int(PyC_FlagSet *item, const char *identifier, int *r_value)
1145 {
1146  for (; item->identifier; item++) {
1147  if (STREQ(item->identifier, identifier)) {
1148  *r_value = item->value;
1149  return 1;
1150  }
1151  }
1152 
1153  return 0;
1154 }
1155 
1157  const char *identifier,
1158  int *r_value,
1159  const char *error_prefix)
1160 {
1161  if (PyC_FlagSet_ValueFromID_int(item, identifier, r_value) == 0) {
1162  PyObject *enum_str = PyC_FlagSet_AsString(item);
1163  PyErr_Format(
1164  PyExc_ValueError, "%s: '%.200s' not found in (%U)", error_prefix, identifier, enum_str);
1165  Py_DECREF(enum_str);
1166  return -1;
1167  }
1168 
1169  return 0;
1170 }
1171 
1173  PyObject *value,
1174  int *r_value,
1175  const char *error_prefix)
1176 {
1177  /* set of enum items, concatenate all values with OR */
1178  int ret, flag = 0;
1179 
1180  /* set looping */
1181  Py_ssize_t pos = 0;
1182  Py_ssize_t hash = 0;
1183  PyObject *key;
1184 
1185  if (!PySet_Check(value)) {
1186  PyErr_Format(PyExc_TypeError,
1187  "%.200s expected a set, not %.200s",
1188  error_prefix,
1189  Py_TYPE(value)->tp_name);
1190  return -1;
1191  }
1192 
1193  *r_value = 0;
1194 
1195  while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1196  const char *param = PyUnicode_AsUTF8(key);
1197 
1198  if (param == NULL) {
1199  PyErr_Format(PyExc_TypeError,
1200  "%.200s set must contain strings, not %.200s",
1201  error_prefix,
1202  Py_TYPE(key)->tp_name);
1203  return -1;
1204  }
1205 
1206  if (PyC_FlagSet_ValueFromID(items, param, &ret, error_prefix) < 0) {
1207  return -1;
1208  }
1209 
1210  flag |= ret;
1211  }
1212 
1213  *r_value = flag;
1214  return 0;
1215 }
1216 
1218 {
1219  PyObject *ret = PySet_New(NULL);
1220  PyObject *pystr;
1221 
1222  for (; items->identifier; items++) {
1223  if (items->value & flag) {
1224  pystr = PyUnicode_FromString(items->identifier);
1225  PySet_Add(ret, pystr);
1226  Py_DECREF(pystr);
1227  }
1228  }
1229 
1230  return ret;
1231 }
1232 
1235 /* -------------------------------------------------------------------- */
1244 bool PyC_RunString_AsNumber(const char *imports[],
1245  const char *expr,
1246  const char *filename,
1247  double *r_value)
1248 {
1249  PyObject *py_dict, *mod, *retval;
1250  bool ok = true;
1251  PyObject *main_mod = NULL;
1252 
1253  PyC_MainModule_Backup(&main_mod);
1254 
1255  py_dict = PyC_DefaultNameSpace(filename);
1256 
1257  mod = PyImport_ImportModule("math");
1258  if (mod) {
1259  PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
1260  Py_DECREF(mod);
1261  }
1262  else { /* highly unlikely but possibly */
1263  PyErr_Print();
1264  PyErr_Clear();
1265  }
1266 
1267  if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
1268  ok = false;
1269  }
1270  else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
1271  ok = false;
1272  }
1273  else {
1274  double val;
1275 
1276  if (PyTuple_Check(retval)) {
1277  /* Users my have typed in 10km, 2m
1278  * add up all values */
1279  int i;
1280  val = 0.0;
1281 
1282  for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
1283  const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
1284  if (val_item == -1 && PyErr_Occurred()) {
1285  val = -1;
1286  break;
1287  }
1288  val += val_item;
1289  }
1290  }
1291  else {
1292  val = PyFloat_AsDouble(retval);
1293  }
1294  Py_DECREF(retval);
1295 
1296  if (val == -1 && PyErr_Occurred()) {
1297  ok = false;
1298  }
1299  else if (!isfinite(val)) {
1300  *r_value = 0.0;
1301  }
1302  else {
1303  *r_value = val;
1304  }
1305  }
1306 
1307  PyC_MainModule_Restore(main_mod);
1308 
1309  return ok;
1310 }
1311 
1312 bool PyC_RunString_AsIntPtr(const char *imports[],
1313  const char *expr,
1314  const char *filename,
1315  intptr_t *r_value)
1316 {
1317  PyObject *py_dict, *retval;
1318  bool ok = true;
1319  PyObject *main_mod = NULL;
1320 
1321  PyC_MainModule_Backup(&main_mod);
1322 
1323  py_dict = PyC_DefaultNameSpace(filename);
1324 
1325  if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
1326  ok = false;
1327  }
1328  else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
1329  ok = false;
1330  }
1331  else {
1332  intptr_t val;
1333 
1334  val = (intptr_t)PyLong_AsVoidPtr(retval);
1335  if (val == 0 && PyErr_Occurred()) {
1336  ok = false;
1337  }
1338  else {
1339  *r_value = val;
1340  }
1341 
1342  Py_DECREF(retval);
1343  }
1344 
1345  PyC_MainModule_Restore(main_mod);
1346 
1347  return ok;
1348 }
1349 
1350 bool PyC_RunString_AsStringAndSize(const char *imports[],
1351  const char *expr,
1352  const char *filename,
1353  char **r_value,
1354  size_t *r_value_size)
1355 {
1356  PyObject *py_dict, *retval;
1357  bool ok = true;
1358  PyObject *main_mod = NULL;
1359 
1360  PyC_MainModule_Backup(&main_mod);
1361 
1362  py_dict = PyC_DefaultNameSpace(filename);
1363 
1364  if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
1365  ok = false;
1366  }
1367  else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
1368  ok = false;
1369  }
1370  else {
1371  const char *val;
1372  Py_ssize_t val_len;
1373 
1374  val = PyUnicode_AsUTF8AndSize(retval, &val_len);
1375  if (val == NULL && PyErr_Occurred()) {
1376  ok = false;
1377  }
1378  else {
1379  char *val_alloc = MEM_mallocN(val_len + 1, __func__);
1380  memcpy(val_alloc, val, val_len + 1);
1381  *r_value = val_alloc;
1382  *r_value_size = val_len;
1383  }
1384 
1385  Py_DECREF(retval);
1386  }
1387 
1388  PyC_MainModule_Restore(main_mod);
1389 
1390  return ok;
1391 }
1392 
1393 bool PyC_RunString_AsString(const char *imports[],
1394  const char *expr,
1395  const char *filename,
1396  char **r_value)
1397 {
1398  size_t value_size;
1399  return PyC_RunString_AsStringAndSize(imports, expr, filename, r_value, &value_size);
1400 }
1401 
1404 #endif /* #ifndef MATH_STANDALONE */
1405 
1406 /* -------------------------------------------------------------------- */
1413 /* Compiler optimizes out redundant checks. */
1414 #ifdef __GNUC__
1415 # pragma warning(push)
1416 # pragma GCC diagnostic ignored "-Wtype-limits"
1417 #endif
1418 
1422 int PyC_Long_AsBool(PyObject *value)
1423 {
1424  const int test = _PyLong_AsInt(value);
1425  if (UNLIKELY((uint)test > 1)) {
1426  PyErr_SetString(PyExc_TypeError, "Python number not a bool (0/1)");
1427  return -1;
1428  }
1429  return test;
1430 }
1431 
1432 int8_t PyC_Long_AsI8(PyObject *value)
1433 {
1434  const int test = _PyLong_AsInt(value);
1435  if (UNLIKELY(test < INT8_MIN || test > INT8_MAX)) {
1436  PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int8");
1437  return -1;
1438  }
1439  return (int8_t)test;
1440 }
1441 
1442 int16_t PyC_Long_AsI16(PyObject *value)
1443 {
1444  const int test = _PyLong_AsInt(value);
1445  if (UNLIKELY(test < INT16_MIN || test > INT16_MAX)) {
1446  PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int16");
1447  return -1;
1448  }
1449  return (int16_t)test;
1450 }
1451 
1452 /* Inlined in header:
1453  * PyC_Long_AsI32
1454  * PyC_Long_AsI64
1455  */
1456 
1457 uint8_t PyC_Long_AsU8(PyObject *value)
1458 {
1459  const ulong test = PyLong_AsUnsignedLong(value);
1460  if (UNLIKELY(test > UINT8_MAX)) {
1461  PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint8");
1462  return (uint8_t)-1;
1463  }
1464  return (uint8_t)test;
1465 }
1466 
1467 uint16_t PyC_Long_AsU16(PyObject *value)
1468 {
1469  const ulong test = PyLong_AsUnsignedLong(value);
1470  if (UNLIKELY(test > UINT16_MAX)) {
1471  PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint16");
1472  return (uint16_t)-1;
1473  }
1474  return (uint16_t)test;
1475 }
1476 
1477 uint32_t PyC_Long_AsU32(PyObject *value)
1478 {
1479  const ulong test = PyLong_AsUnsignedLong(value);
1480  if (UNLIKELY(test > UINT32_MAX)) {
1481  PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint32");
1482  return (uint32_t)-1;
1483  }
1484  return (uint32_t)test;
1485 }
1486 
1487 /* Inlined in header:
1488  * PyC_Long_AsU64
1489  */
1490 
1491 #ifdef __GNUC__
1492 # pragma warning(pop)
1493 #endif
1494 
1497 /* -------------------------------------------------------------------- */
1501 char PyC_StructFmt_type_from_str(const char *typestr)
1502 {
1503  switch (typestr[0]) {
1504  case '!':
1505  case '<':
1506  case '=':
1507  case '>':
1508  case '@':
1509  return typestr[1];
1510  default:
1511  return typestr[0];
1512  }
1513 }
1514 
1516 {
1517  switch (format) {
1518  case 'f':
1519  case 'd':
1520  case 'e':
1521  return true;
1522  default:
1523  return false;
1524  }
1525 }
1526 
1528 {
1529  switch (format) {
1530  case 'i':
1531  case 'I':
1532  case 'l':
1533  case 'L':
1534  case 'h':
1535  case 'H':
1536  case 'b':
1537  case 'B':
1538  case 'q':
1539  case 'Q':
1540  case 'n':
1541  case 'N':
1542  case 'P':
1543  return true;
1544  default:
1545  return false;
1546  }
1547 }
1548 
1550 {
1551  switch (format) {
1552  case 'c':
1553  case 's':
1554  case 'p':
1555  return true;
1556  default:
1557  return false;
1558  }
1559 }
1560 
1562 {
1563  switch (format) {
1564  case '?':
1565  return true;
1566  default:
1567  return false;
1568  }
1569 }
1570 
#define BLI_assert(a)
Definition: BLI_assert.h:58
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
unsigned long ulong
Definition: BLI_sys_types.h:85
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNLIKELY(x)
#define ELEM(...)
#define STREQ(a, b)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
#define str(s)
uint pos
format
Definition: logImageCore.h:47
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
bool isfinite(uchar)
Definition: image.cpp:44
#define hash
Definition: noise.c:169
int16_t PyC_Long_AsI16(PyObject *value)
char PyC_StructFmt_type_from_str(const char *typestr)
PyObject * PyC_DefaultNameSpace(const char *filename)
const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
PyObject * PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag)
int PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix)
bool PyC_StructFmt_type_is_bool(char format)
uint8_t PyC_Long_AsU8(PyObject *value)
void PyC_ObSpit(const char *name, PyObject *var)
int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int *r_value, const char *error_prefix)
int8_t PyC_Long_AsI8(PyObject *value)
int PyC_FlagSet_ValueFromID_int(PyC_FlagSet *item, const char *identifier, int *r_value)
void PyC_RunQuicky(const char *filepath, int n,...)
bool PyC_RunString_AsString(const char *imports[], const char *expr, const char *filename, char **r_value)
PyObject * PyC_Tuple_PackArray_Bool(const bool *array, uint len)
static void pyc_exception_buffer_handle_system_exit(PyObject *error_type, PyObject *error_value, PyObject *error_traceback)
int PyC_CheckArgs_DeepCopy(PyObject *args)
PyObject * PyC_Tuple_PackArray_I32FromBool(const int *array, uint len)
int PyC_ParseStringEnum(PyObject *o, void *p)
PyObject * PyC_UnicodeFromByteAndSize(const char *str, Py_ssize_t size)
PyObject * PyC_Err_SetString_Prefix(PyObject *exception_type_prefix, const char *str)
bool PyC_StructFmt_type_is_int_any(char format)
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
PyObject * PyC_ExceptionBuffer(void)
int PyC_Long_AsBool(PyObject *value)
PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format,...)
PyObject * PyC_Tuple_PackArray_F32(const float *array, uint len)
const char * PyC_StringEnum_FindIDFromValue(const struct PyC_StringEnumItems *items, const int value)
bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value)
bool PyC_StructFmt_type_is_byte(char format)
PyObject * PyC_ExceptionBuffer_Simple(void)
void PyC_StackSpit(void)
PyObject * PyC_Tuple_PackArray_I32(const int *array, uint len)
PyObject * PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n,...)
int PyC_AsArray_FAST(void *array, PyObject *value_fast, const Py_ssize_t length, const PyTypeObject *type, const bool is_double, const char *error_prefix)
Definition: py_capi_utils.c:59
void PyC_MainModule_Backup(PyObject **r_main_mod)
bool PyC_RunString_AsStringAndSize(const char *imports[], const char *expr, const char *filename, char **r_value, size_t *r_value_size)
const char * PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce)
void PyC_FileAndNum_Safe(const char **r_filename, int *r_lineno)
bool PyC_IsInterpreterActive(void)
uint32_t PyC_Long_AsU32(PyObject *value)
PyObject * PyC_FlagSet_AsString(PyC_FlagSet *item)
bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
void PyC_FileAndNum(const char **r_filename, int *r_lineno)
void PyC_List_Fill(PyObject *list, PyObject *value)
void PyC_LineSpit(void)
uint16_t PyC_Long_AsU16(PyObject *value)
void PyC_Err_PrintWithFunc(PyObject *py_func)
void PyC_MainModule_Restore(PyObject *main_mod)
PyObject * PyC_FrozenSetFromStrings(const char **strings)
PyObject * PyC_Tuple_PackArray_F64(const double *array, uint len)
PyObject * PyC_UnicodeFromByte(const char *str)
int PyC_ParseBool(PyObject *o, void *p)
void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var)
void PyC_Tuple_Fill(PyObject *tuple, PyObject *value)
bool PyC_StructFmt_type_is_float_any(char format)
int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const bool is_double, const char *error_prefix)
bool PyC_RunString_AsIntPtr(const char *imports[], const char *expr, const char *filename, intptr_t *r_value)
header-only utilities
return ret
signed short int16_t
Definition: stdint.h:79
unsigned short uint16_t
Definition: stdint.h:82
#define UINT16_MAX
Definition: stdint.h:144
unsigned int uint32_t
Definition: stdint.h:83
_W64 int intptr_t
Definition: stdint.h:121
#define INT8_MAX
Definition: stdint.h:136
#define UINT32_MAX
Definition: stdint.h:145
unsigned char uint8_t
Definition: stdint.h:81
#define INT16_MAX
Definition: stdint.h:138
#define UINT8_MAX
Definition: stdint.h:143
signed char int8_t
Definition: stdint.h:78
const char * identifier
Definition: py_capi_utils.h:98
const struct PyC_StringEnumItems * items
ccl_device_inline int mod(int x, int m)
Definition: util_math.h:405
uint len
PointerRNA * ptr
Definition: wm_files.c:3157