Blender  V2.93
py_capi_utils.h
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 
21 /* Use a define instead of `#pragma once` because of `bmesh_py_types.h` */
22 #ifndef __PY_CAPI_UTILS_H__
23 #define __PY_CAPI_UTILS_H__
24 
25 #include "BLI_sys_types.h"
27 
28 void PyC_ObSpit(const char *name, PyObject *var);
29 void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var);
30 void PyC_LineSpit(void);
31 void PyC_StackSpit(void);
32 PyObject *PyC_ExceptionBuffer(void);
33 PyObject *PyC_ExceptionBuffer_Simple(void);
34 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...);
35 PyObject *PyC_FrozenSetFromStrings(const char **strings);
36 
37 PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...);
38 PyObject *PyC_Err_SetString_Prefix(PyObject *exception_type_prefix, const char *str);
39 
40 void PyC_Err_PrintWithFunc(PyObject *py_func);
41 
42 void PyC_FileAndNum(const char **r_filename, int *r_lineno);
43 void PyC_FileAndNum_Safe(const char **r_filename, int *r_lineno); /* checks python is running */
44 int PyC_AsArray_FAST(void *array,
45  PyObject *value_fast,
46  const Py_ssize_t length,
47  const PyTypeObject *type,
48  const bool is_double,
49  const char *error_prefix);
50 int PyC_AsArray(void *array,
51  PyObject *value,
52  const Py_ssize_t length,
53  const PyTypeObject *type,
54  const bool is_double,
55  const char *error_prefix);
56 
57 PyObject *PyC_Tuple_PackArray_F32(const float *array, uint len);
58 PyObject *PyC_Tuple_PackArray_F64(const double *array, uint len);
59 PyObject *PyC_Tuple_PackArray_I32(const int *array, uint len);
60 PyObject *PyC_Tuple_PackArray_I32FromBool(const int *array, uint len);
61 PyObject *PyC_Tuple_PackArray_Bool(const bool *array, uint len);
62 
63 #define PyC_Tuple_Pack_F32(...) \
64  PyC_Tuple_PackArray_F32(((const float[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
65 #define PyC_Tuple_Pack_F64(...) \
66  PyC_Tuple_PackArray_F64(((const double[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
67 #define PyC_Tuple_Pack_I32(...) \
68  PyC_Tuple_PackArray_I32(((const int[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
69 #define PyC_Tuple_Pack_I32FromBool(...) \
70  PyC_Tuple_PackArray_I32FromBool(((const int[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
71 #define PyC_Tuple_Pack_Bool(...) \
72  PyC_Tuple_PackArray_Bool(((const bool[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
73 
74 void PyC_Tuple_Fill(PyObject *tuple, PyObject *value);
75 void PyC_List_Fill(PyObject *list, PyObject *value);
76 
77 /* follow http://www.python.org/dev/peps/pep-0383/ */
78 PyObject *PyC_UnicodeFromByte(const char *str);
79 PyObject *PyC_UnicodeFromByteAndSize(const char *str, Py_ssize_t size);
80 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce must be NULL */
81 const char *PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce);
82 
83 /* name namespace function for bpy */
84 PyObject *PyC_DefaultNameSpace(const char *filename);
85 void PyC_RunQuicky(const char *filepath, int n, ...);
86 bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[]);
87 
88 void PyC_MainModule_Backup(PyObject **r_main_mod);
89 void PyC_MainModule_Restore(PyObject *main_mod);
90 
91 bool PyC_IsInterpreterActive(void);
92 
93 void *PyC_RNA_AsPointer(PyObject *value, const char *type_name);
94 
95 /* flag / set --- interchange */
96 typedef struct PyC_FlagSet {
97  int value;
98  const char *identifier;
100 
101 PyObject *PyC_FlagSet_AsString(PyC_FlagSet *item);
102 int PyC_FlagSet_ValueFromID_int(PyC_FlagSet *item, const char *identifier, int *r_value);
104  const char *identifier,
105  int *r_value,
106  const char *error_prefix);
108  PyObject *value,
109  int *r_value,
110  const char *error_prefix);
111 PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
112 
113 bool PyC_RunString_AsNumber(const char **imports,
114  const char *expr,
115  const char *filename,
116  double *r_value);
117 bool PyC_RunString_AsIntPtr(const char **imports,
118  const char *expr,
119  const char *filename,
120  intptr_t *r_value);
121 bool PyC_RunString_AsStringAndSize(const char **imports,
122  const char *expr,
123  const char *filename,
124  char **r_value,
125  size_t *r_value_size);
126 bool PyC_RunString_AsString(const char **imports,
127  const char *expr,
128  const char *filename,
129  char **r_value);
130 
131 int PyC_ParseBool(PyObject *o, void *p);
132 
134  int value;
135  const char *id;
136 };
138  const struct PyC_StringEnumItems *items;
140 };
141 
142 int PyC_ParseStringEnum(PyObject *o, void *p);
143 const char *PyC_StringEnum_FindIDFromValue(const struct PyC_StringEnumItems *items,
144  const int value);
145 
146 int PyC_CheckArgs_DeepCopy(PyObject *args);
147 
148 /* Integer parsing (with overflow checks), -1 on error. */
149 int PyC_Long_AsBool(PyObject *value);
150 int8_t PyC_Long_AsI8(PyObject *value);
151 int16_t PyC_Long_AsI16(PyObject *value);
152 #if 0 /* inline */
153 int32_t PyC_Long_AsI32(PyObject *value);
154 int64_t PyC_Long_AsI64(PyObject *value);
155 #endif
156 
157 uint8_t PyC_Long_AsU8(PyObject *value);
158 uint16_t PyC_Long_AsU16(PyObject *value);
159 uint32_t PyC_Long_AsU32(PyObject *value);
160 #if 0 /* inline */
161 uint64_t PyC_Long_AsU64(PyObject *value);
162 #endif
163 
164 /* inline so type signatures match as expected */
165 Py_LOCAL_INLINE(int32_t) PyC_Long_AsI32(PyObject *value)
166 {
167  return (int32_t)_PyLong_AsInt(value);
168 }
169 Py_LOCAL_INLINE(int64_t) PyC_Long_AsI64(PyObject *value)
170 {
171  return (int64_t)PyLong_AsLongLong(value);
172 }
173 Py_LOCAL_INLINE(uint64_t) PyC_Long_AsU64(PyObject *value)
174 {
175  return (uint64_t)PyLong_AsUnsignedLongLong(value);
176 }
177 
178 /* utils for format string in `struct` module style syntax */
179 char PyC_StructFmt_type_from_str(const char *typestr);
184 
185 #endif /* __PY_CAPI_UTILS_H__ */
unsigned int uint
Definition: BLI_sys_types.h:83
_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
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)
format
Definition: logImageCore.h:47
int16_t PyC_Long_AsI16(PyObject *value)
bool PyC_RunString_AsString(const char **imports, const char *expr, const char *filename, char **r_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)
bool PyC_RunString_AsStringAndSize(const char **imports, const char *expr, const char *filename, char **r_value, size_t *r_value_size)
uint8_t PyC_Long_AsU8(PyObject *value)
void PyC_ObSpit(const char *name, PyObject *var)
Py_LOCAL_INLINE(int32_t) PyC_Long_AsI32(PyObject *value)
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,...)
PyObject * PyC_Tuple_PackArray_Bool(const bool *array, uint len)
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)
bool PyC_RunString_AsNumber(const char **imports, const char *expr, const char *filename, double *r_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_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)
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_RunString_AsIntPtr(const char **imports, const char *expr, const char *filename, intptr_t *r_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)
struct PyC_FlagSet PyC_FlagSet
signed short int16_t
Definition: stdint.h:79
unsigned short uint16_t
Definition: stdint.h:82
unsigned int uint32_t
Definition: stdint.h:83
__int64 int64_t
Definition: stdint.h:92
_W64 int intptr_t
Definition: stdint.h:121
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
unsigned __int64 uint64_t
Definition: stdint.h:93
signed char int8_t
Definition: stdint.h:78
const char * identifier
Definition: py_capi_utils.h:98
const struct PyC_StringEnumItems * items
uint len