Welcome to mirror list, hosted at ThFree Co, Russian Federation.

py_capi_rna.c « generic « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6a556a3fff0dad654e1de2315d3174b5690455d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup pygen
 *
 * Python/RNA utilities.
 *
 * RNA functions that aren't part of the `bpy_rna.c` API.
 */

/* Future-proof, See https://docs.python.org/3/c-api/arg.html#strings-and-buffers */
#define PY_SSIZE_T_CLEAN

#include <Python.h>
#include <stdbool.h>

#include "py_capi_rna.h"

#include "BLI_bitmap.h"
#include "BLI_dynstr.h"

#include "RNA_access.h"

#include "MEM_guardedalloc.h"

/**
 * Convert all items into a single comma separated string.
 * Use for creating useful error messages.
 */
char *pyrna_enum_repr(const EnumPropertyItem *item)
{
  DynStr *dynstr = BLI_dynstr_new();

  /* We can't compare with the first element in the array
   * since it may be a category (without an identifier). */
  for (bool is_first = true; item->identifier; item++) {
    if (item->identifier[0]) {
      BLI_dynstr_appendf(dynstr, is_first ? "'%s'" : ", '%s'", item->identifier);
      is_first = false;
    }
  }

  char *cstring = BLI_dynstr_get_cstring(dynstr);
  BLI_dynstr_free(dynstr);
  return cstring;
}

/**
 * Same as #RNA_enum_value_from_id, but raises an exception.
 */
int pyrna_enum_value_from_id(const EnumPropertyItem *item,
                             const char *identifier,
                             int *r_value,
                             const char *error_prefix)
{
  if (RNA_enum_value_from_id(item, identifier, r_value) == 0) {
    const char *enum_str = pyrna_enum_repr(item);
    PyErr_Format(
        PyExc_ValueError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str);
    MEM_freeN((void *)enum_str);
    return -1;
  }

  return 0;
}

/**
 * Use with #PyArg_ParseTuple's `O&` formatting.
 */
int pyrna_enum_value_parse_string(PyObject *o, void *p)
{
  const char *identifier = PyUnicode_AsUTF8(o);
  if (identifier == NULL) {
    PyErr_Format(PyExc_TypeError, "expected a string enum, not %.200s", Py_TYPE(o)->tp_name);
    return 0;
  }
  struct BPy_EnumProperty_Parse *parse_data = p;
  if (pyrna_enum_value_from_id(
          parse_data->items, identifier, &parse_data->value, "enum identifier") == -1) {
    return 0;
  }

  parse_data->value_orig = o;
  parse_data->is_set = true;
  return 1;
}

/**
 * Use with #PyArg_ParseTuple's `O&` formatting.
 */
int pyrna_enum_bitfield_parse_set(PyObject *o, void *p)
{
  if (!PySet_Check(o)) {
    PyErr_Format(PyExc_TypeError, "expected a set, not %.200s", Py_TYPE(o)->tp_name);
    return 0;
  }

  struct BPy_EnumProperty_Parse *parse_data = p;
  if (pyrna_enum_bitfield_from_set(
          parse_data->items, o, &parse_data->value, "enum identifier set") == -1) {
    return 0;
  }
  parse_data->value_orig = o;
  parse_data->is_set = true;
  return 1;
}

/**
 * Takes a set of strings and map it to and array of booleans.
 *
 * Useful when the values aren't flags.
 *
 * \param type_convert_sign: Maps signed to unsigned range,
 * needed when we want to use the full range of a signed short/char.
 */
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)
{
  /* Set looping. */
  Py_ssize_t pos = 0;
  Py_ssize_t hash = 0;
  PyObject *key;

  BLI_bitmap *bitmap = BLI_BITMAP_NEW(bitmap_size, __func__);

  while (_PySet_NextEntry(value, &pos, &key, &hash)) {
    const char *param = PyUnicode_AsUTF8(key);
    if (param == NULL) {
      PyErr_Format(PyExc_TypeError,
                   "%.200s expected a string, not %.200s",
                   error_prefix,
                   Py_TYPE(key)->tp_name);
      goto error;
    }

    int ret;
    if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
      goto error;
    }

    int index = ret;

    if (type_convert_sign) {
      if (type_size == 2) {
        union {
          signed short as_signed;
          ushort as_unsigned;
        } ret_convert;
        ret_convert.as_signed = (signed short)ret;
        index = (int)ret_convert.as_unsigned;
      }
      else if (type_size == 1) {
        union {
          signed char as_signed;
          uchar as_unsigned;
        } ret_convert;
        ret_convert.as_signed = (signed char)ret;
        index = (int)ret_convert.as_unsigned;
      }
      else {
        BLI_assert_unreachable();
      }
    }
    BLI_assert(index < bitmap_size);
    BLI_BITMAP_ENABLE(bitmap, index);
  }

  return bitmap;

error:
  MEM_freeN(bitmap);
  return NULL;
}

/**
 * 'value' _must_ be a set type, error check before calling.
 */
int pyrna_enum_bitfield_from_set(const EnumPropertyItem *items,
                                 PyObject *value,
                                 int *r_value,
                                 const char *error_prefix)
{
  /* Set of enum items, concatenate all values with OR. */
  int ret, flag = 0;

  /* Set looping. */
  Py_ssize_t pos = 0;
  Py_ssize_t hash = 0;
  PyObject *key;

  *r_value = 0;

  while (_PySet_NextEntry(value, &pos, &key, &hash)) {
    const char *param = PyUnicode_AsUTF8(key);

    if (param == NULL) {
      PyErr_Format(PyExc_TypeError,
                   "%.200s expected a string, not %.200s",
                   error_prefix,
                   Py_TYPE(key)->tp_name);
      return -1;
    }

    if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
      return -1;
    }

    flag |= ret;
  }

  *r_value = flag;
  return 0;
}

PyObject *pyrna_enum_bitfield_as_set(const EnumPropertyItem *items, int value)
{
  PyObject *ret = PySet_New(NULL);
  const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];

  if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
    PyObject *item;
    int index;
    for (index = 0; identifier[index]; index++) {
      item = PyUnicode_FromString(identifier[index]);
      PySet_Add(ret, item);
      Py_DECREF(item);
    }
  }

  return ret;
}