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 
17 /** \file
18  * \ingroup pythonintern
19  */
20 
21 #pragma once
22 
23 /* --- bpy build options --- */
24 #ifdef WITH_PYTHON_SAFETY
25 
26 /**
27  * Play it safe and keep optional for now,
28  * need to test further now this affects looping on 10000's of verts for eg.
29  */
30 #  define USE_WEAKREFS
31 
32 /* method to invalidate removed py data, XXX, slow to remove objects, otherwise no overhead */
33 /* #define USE_PYRNA_INVALIDATE_GC */
34 
35 /* different method */
36 #  define USE_PYRNA_INVALIDATE_WEAKREF
37 
38 /* support for inter references, currently only needed for corner case */
39 #  define USE_PYRNA_STRUCT_REFERENCE
40 
41 #else /* WITH_PYTHON_SAFETY */
42 
43 /* default, no defines! */
44 
45 #endif /* !WITH_PYTHON_SAFETY */
46 
47 /* sanity checks on above defs */
48 #if defined(USE_PYRNA_INVALIDATE_WEAKREF) && !defined(USE_WEAKREFS)
49 #  define USE_WEAKREFS
50 #endif
51 
52 #if defined(USE_PYRNA_INVALIDATE_GC) && defined(USE_PYRNA_INVALIDATE_WEAKREF)
53 #  error "Only 1 reference check method at a time!"
54 #endif
55 
56 /* only used by operator introspection get_rna(), this is only used for doc gen
57  * so prefer the leak to the memory bloat for now. */
58 // #define PYRNA_FREE_SUPPORT
59 
60 /* use real collection iterators rather than faking with a list
61  * this is needed so enums can be iterated over without crashing,
62  * since finishing the iteration frees temp allocated enums */
63 #define USE_PYRNA_ITER
64 
65 /* --- end bpy build options --- */
66 
67 struct ID;
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 extern PyTypeObject pyrna_struct_meta_idprop_Type;
74 extern PyTypeObject pyrna_struct_Type;
75 extern PyTypeObject pyrna_prop_Type;
76 extern PyTypeObject pyrna_prop_array_Type;
77 extern PyTypeObject pyrna_prop_collection_Type;
78 extern PyTypeObject pyrna_func_Type;
79 
80 #define BPy_StructRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_struct_Type))
81 #define BPy_StructRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_struct_Type)
82 #define BPy_PropertyRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_prop_Type))
83 #define BPy_PropertyRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_prop_Type)
84 
85 #define PYRNA_STRUCT_CHECK_OBJ(obj) \
86   if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
87     return NULL; \
88   } \
89   (void)0
90 #define PYRNA_STRUCT_CHECK_INT(obj) \
91   if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
92     return -1; \
93   } \
94   (void)0
95 
96 #define PYRNA_PROP_CHECK_OBJ(obj) \
97   if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
98     return NULL; \
99   } \
100   (void)0
101 #define PYRNA_PROP_CHECK_INT(obj) \
102   if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
103     return -1; \
104   } \
105   (void)0
106 
107 #define PYRNA_STRUCT_IS_VALID(pysrna) (LIKELY(((BPy_StructRNA *)(pysrna))->ptr.type != NULL))
108 #define PYRNA_PROP_IS_VALID(pysrna) (LIKELY(((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL))
109 
110 /* 'in_weakreflist' MUST be aligned */
111 
112 typedef struct {
113   PyObject_HEAD /* required python macro   */
114 #ifdef USE_WEAKREFS
115       PyObject *in_weakreflist;
116 #endif
117   PointerRNA ptr;
118 } BPy_DummyPointerRNA;
119 
120 typedef struct {
121   PyObject_HEAD /* required python macro   */
122 #ifdef USE_WEAKREFS
123       PyObject *in_weakreflist;
124 #endif
125   PointerRNA ptr;
126 #ifdef USE_PYRNA_STRUCT_REFERENCE
127   /* generic PyObject we hold a reference to, example use:
128    * hold onto the collection iterator to prevent it from freeing allocated data we may use */
129   PyObject *reference;
130 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
131 
132 #ifdef PYRNA_FREE_SUPPORT
133   bool freeptr; /* needed in some cases if ptr.data is created on the fly, free when deallocing */
134 #endif          /* PYRNA_FREE_SUPPORT */
135 } BPy_StructRNA;
136 
137 typedef struct {
138   PyObject_HEAD /* required python macro   */
139 #ifdef USE_WEAKREFS
140       PyObject *in_weakreflist;
141 #endif
142   PointerRNA ptr;
143   PropertyRNA *prop;
144 } BPy_PropertyRNA;
145 
146 typedef struct {
147   PyObject_HEAD /* required python macro   */
148 #ifdef USE_WEAKREFS
149       PyObject *in_weakreflist;
150 #endif
151   PointerRNA ptr;
152   PropertyRNA *prop;
153 
154   /* Arystan: this is a hack to allow sub-item r/w access like: face.uv[n][m] */
155   /** Array dimension, e.g: 0 for face.uv, 2 for face.uv[n][m], etc. */
156   int arraydim;
157   /** Array first item offset, e.g. if face.uv is [4][2], arrayoffset for face.uv[n] is 2n. */
158   int arrayoffset;
159 } BPy_PropertyArrayRNA;
160 
161 typedef struct {
162   PyObject_HEAD /* required python macro   */
163 #ifdef USE_WEAKREFS
164       PyObject *in_weakreflist;
165 #endif
166 
167   /* collection iterator specific parts */
168   CollectionPropertyIterator iter;
169 } BPy_PropertyCollectionIterRNA;
170 
171 typedef struct {
172   PyObject_HEAD /* required python macro   */
173 #ifdef USE_WEAKREFS
174       PyObject *in_weakreflist;
175 #endif
176   PointerRNA ptr;
177   FunctionRNA *func;
178 } BPy_FunctionRNA;
179 
180 /* cheap trick */
181 #define BPy_BaseTypeRNA BPy_PropertyRNA
182 
183 StructRNA *srna_from_self(PyObject *self, const char *error_prefix);
184 StructRNA *pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix);
185 
186 void BPY_rna_init(void);
187 PyObject *BPY_rna_module(void);
188 void BPY_update_rna_module(void);
189 /*PyObject *BPY_rna_doc(void);*/
190 PyObject *BPY_rna_types(void);
191 
192 PyObject *pyrna_struct_CreatePyObject(PointerRNA *ptr);
193 PyObject *pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop);
194 
195 /* extern'd by other modules which don't deal closely with RNA */
196 PyObject *pyrna_id_CreatePyObject(struct ID *id);
197 bool pyrna_id_FromPyObject(PyObject *obj, struct ID **id);
198 bool pyrna_id_CheckPyObject(PyObject *obj);
199 
200 /* operators also need this to set args */
201 int pyrna_pydict_to_props(PointerRNA *ptr,
202                           PyObject *kw,
203                           const bool all_args,
204                           const char *error_prefix);
205 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
206 
207 uint *pyrna_set_to_enum_bitmap(const struct EnumPropertyItem *items,
208                                PyObject *value,
209                                int type_size,
210                                bool type_convert_sign,
211                                int bitmap_size,
212                                const char *error_prefix);
213 PyObject *pyrna_enum_bitfield_to_py(const struct EnumPropertyItem *items, int value);
214 int pyrna_set_to_enum_bitfield(const struct EnumPropertyItem *items,
215                                PyObject *value,
216                                int *r_value,
217                                const char *error_prefix);
218 
219 int pyrna_enum_value_from_id(const EnumPropertyItem *item,
220                              const char *identifier,
221                              int *value,
222                              const char *error_prefix);
223 
224 int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class);
225 
226 void pyrna_struct_type_extend_capi(struct StructRNA *srna,
227                                    struct PyMethodDef *py_method,
228                                    struct PyGetSetDef *py_getset);
229 
230 /* called before stopping python */
231 void pyrna_alloc_types(void);
232 void pyrna_free_types(void);
233 
234 /* primitive type conversion */
235 int pyrna_py_to_array(
236     PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix);
237 int pyrna_py_to_array_index(PointerRNA *ptr,
238                             PropertyRNA *prop,
239                             int arraydim,
240                             int arrayoffset,
241                             int index,
242                             PyObject *py,
243                             const char *error_prefix);
244 PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
245 
246 PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop);
247 PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self,
248                                     PointerRNA *ptr,
249                                     PropertyRNA *prop,
250                                     int index);
251 PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop);
252 int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value);
253 
254 bool pyrna_write_check(void);
255 void pyrna_write_set(bool val);
256 
257 void pyrna_invalidate(BPy_DummyPointerRNA *self);
258 int pyrna_struct_validity_check(BPy_StructRNA *pysrna);
259 int pyrna_prop_validity_check(BPy_PropertyRNA *self);
260 
261 /* bpy.utils.(un)register_class */
262 extern PyMethodDef meth_bpy_register_class;
263 extern PyMethodDef meth_bpy_unregister_class;
264 
265 /* bpy.utils._bl_owner_(get/set) */
266 extern PyMethodDef meth_bpy_owner_id_set;
267 extern PyMethodDef meth_bpy_owner_id_get;
268 
269 extern BPy_StructRNA *bpy_context_module;
270 
271 #ifdef __cplusplus
272 }
273 #endif
274