Blender V5.3
py_capi_rna.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13/* Future-proof, See https://docs.python.org/3/c-api/arg.html#strings-and-buffers */
14#define PY_SSIZE_T_CLEAN
15
16#include <Python.h>
17
18#include "py_capi_rna.hh"
19
20#include "BLI_bitmap.hh"
21#include "BLI_dynstr.hh"
22
23#include "RNA_access.hh"
24
25#include "MEM_guardedalloc.h"
26
27namespace blender {
28
29/* -------------------------------------------------------------------- */
32
34{
35 DynStr *dynstr = BLI_dynstr_new();
36
37 /* We can't compare with the first element in the array
38 * since it may be a category (without an identifier). */
39 for (bool is_first = true; item->identifier; item++) {
40 if (item->identifier[0]) {
41 BLI_dynstr_appendf(dynstr, is_first ? "'%s'" : ", '%s'", item->identifier);
42 is_first = false;
43 }
44 }
45
46 char *cstring = BLI_dynstr_get_cstring(dynstr);
47 BLI_dynstr_free(dynstr);
48 return cstring;
49}
50
52
53/* -------------------------------------------------------------------- */
56
58 const char *identifier,
59 int *r_value,
60 const char *error_prefix)
61{
62 if (RNA_enum_value_from_id(item, identifier, r_value) == 0) {
63 const char *enum_str = pyrna_enum_repr(item);
64 PyErr_Format(
65 PyExc_ValueError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str);
66 MEM_delete(enum_str);
67 return -1;
68 }
69
70 return 0;
71}
72
74 PyObject *value,
75 int type_size,
76 bool type_convert_sign,
77 int bitmap_size,
78 const char *error_prefix)
79{
80 BLI_assert(PySet_Check(value));
81 BLI_bitmap *bitmap = BLI_BITMAP_NEW(bitmap_size, __func__);
82
83 if (PySet_GET_SIZE(value) > 0) {
84 /* Set looping. */
85 PyObject *it = PyObject_GetIter(value);
86 PyObject *key;
87 while ((key = PyIter_Next(it))) {
88 /* Borrow from the set. */
89 Py_DECREF(key);
90
91 const char *param = PyUnicode_AsUTF8(key);
92 if (param == nullptr) {
93 PyErr_Format(PyExc_TypeError,
94 "%.200s expected a string, not %.200s",
95 error_prefix,
96 Py_TYPE(key)->tp_name);
97 break;
98 }
99
100 int ret;
101 if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
102 break;
103 }
104
105 int index = ret;
106
107 if (type_convert_sign) {
108 if (type_size == 2) {
109 union {
110 signed short as_signed;
111 ushort as_unsigned;
112 } ret_convert;
113 ret_convert.as_signed = short(ret);
114 index = int(ret_convert.as_unsigned);
115 }
116 else if (type_size == 1) {
117 union {
118 signed char as_signed;
119 uchar as_unsigned;
120 } ret_convert;
121 ret_convert.as_signed = static_cast<signed char>(ret);
122 index = int(ret_convert.as_unsigned);
123 }
124 else {
126 }
127 }
128 BLI_assert(index < bitmap_size);
129 BLI_BITMAP_ENABLE(bitmap, index);
130 }
131 Py_DECREF(it);
132
133 if (key) {
134 MEM_delete(bitmap);
135 bitmap = nullptr;
136 }
137 }
138
139 return bitmap;
140}
141
143 PyObject *value,
144 int *r_value,
145 const char *error_prefix)
146{
147 BLI_assert(PySet_Check(value));
148 /* Set of enum items, concatenate all values with OR. */
149 int flag = 0;
150
151 *r_value = 0;
152
153 PyObject *key = nullptr;
154 if (PySet_GET_SIZE(value) > 0) {
155 /* Set looping. */
156 PyObject *it = PyObject_GetIter(value);
157 while ((key = PyIter_Next(it))) {
158 /* Borrow from the set. */
159 Py_DECREF(key);
160
161 const char *param = PyUnicode_AsUTF8(key);
162 if (param == nullptr) {
163 PyErr_Format(PyExc_TypeError,
164 "%.200s expected a string, not %.200s",
165 error_prefix,
166 Py_TYPE(key)->tp_name);
167 break;
168 }
169
170 int ret;
171 if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
172 break;
173 }
174
175 flag |= ret;
176 }
177 Py_DECREF(it);
178 if (key) {
179 return -1;
180 }
181 }
182
183 *r_value = flag;
184 return 0;
185}
186
187PyObject *pyrna_enum_bitfield_as_set(const EnumPropertyItem *items, int value)
188{
189 PyObject *ret = PySet_New(nullptr);
190 const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
191
192 if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
193 PyObject *item;
194 int index;
195 for (index = 0; identifier[index]; index++) {
196 item = PyUnicode_FromString(identifier[index]);
197 PySet_Add(ret, item);
198 Py_DECREF(item);
199 }
200 }
201
202 return ret;
203}
204
206
207/* -------------------------------------------------------------------- */
210
211int pyrna_enum_value_parse_string(PyObject *o, void *p)
212{
213 const char *identifier = PyUnicode_AsUTF8(o);
214 if (identifier == nullptr) {
215 PyErr_Format(PyExc_TypeError, "expected a string enum, not %.200s", Py_TYPE(o)->tp_name);
216 return 0;
217 }
218 BPy_EnumProperty_Parse *parse_data = static_cast<BPy_EnumProperty_Parse *>(p);
220 parse_data->items, identifier, &parse_data->value, "enum identifier") == -1)
221 {
222 return 0;
223 }
224
225 parse_data->value_orig = o;
226 parse_data->is_set = true;
227 return 1;
228}
229
230int pyrna_enum_bitfield_parse_set(PyObject *o, void *p)
231{
232 if (!PySet_Check(o)) {
233 PyErr_Format(PyExc_TypeError, "expected a set, not %.200s", Py_TYPE(o)->tp_name);
234 return 0;
235 }
236
237 BPy_EnumProperty_Parse *parse_data = static_cast<BPy_EnumProperty_Parse *>(p);
239 parse_data->items, o, &parse_data->value, "enum identifier set") == -1)
240 {
241 return 0;
242 }
243 parse_data->value_orig = o;
244 parse_data->is_set = true;
245 return 1;
246}
247
249
250} // namespace blender
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
A dynamically sized string ADT.
Read Blender memory allocation and freeing.
#define RNA_ENUM_BITFLAG_SIZE
Definition RNA_types.hh:270
#define BLI_assert_unreachable()
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
int pyrna_enum_bitfield_from_set(const EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
int RNA_enum_bitflag_identifiers(const EnumPropertyItem *item, const int value, const char **r_identifier)
unsigned short ushort
Definition BKE_text.h:56
BLI_bitmap * pyrna_enum_bitmap_from_set(const EnumPropertyItem *items, PyObject *value, int type_size, bool type_convert_sign, int bitmap_size, const char *error_prefix)
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
unsigned char uchar
Definition BKE_text.h:58
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int pyrna_enum_bitfield_parse_set(PyObject *o, void *p)
PyObject * pyrna_enum_bitfield_as_set(const EnumPropertyItem *items, int value)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
int pyrna_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value, const char *error_prefix)
char * pyrna_enum_repr(const EnumPropertyItem *item)
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
int pyrna_enum_value_parse_string(PyObject *o, void *p)
const EnumPropertyItem * items