svn merge ^/trunk/blender -r42172:42182
[blender-staging.git] / source / blender / python / generic / idprop_py_api.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  * Contributor(s): Joseph Eagar, Campbell Barton
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/python/generic/idprop_py_api.c
25  *  \ingroup pygen
26  */
27
28
29 #include <Python.h>
30
31 #include "idprop_py_api.h"
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_string.h"
35 #include "BLI_utildefines.h"
36
37 #include "BKE_idprop.h"
38
39
40 #define USE_STRING_COERCE
41
42 #ifdef USE_STRING_COERCE
43 #include "py_capi_utils.h"
44 #endif
45
46 extern PyTypeObject BPy_IDArray_Type;
47 extern PyTypeObject BPy_IDGroup_Iter_Type;
48 extern PyTypeObject BPy_IDGroup_Type;
49
50 /*********************** ID Property Main Wrapper Stuff ***************/
51
52 /* ----------------------------------------------------------------------------
53  * static conversion functions to avoid duplicate code, no type checking.
54  */
55
56 static PyObject *idprop_py_from_idp_string(IDProperty *prop)
57 {
58         if (prop->subtype == IDP_STRING_SUB_BYTE) {
59                 return PyBytes_FromStringAndSize(IDP_Array(prop), prop->len);
60         }
61         else {
62 #ifdef USE_STRING_COERCE
63                 return PyC_UnicodeFromByteAndSize(IDP_Array(prop), prop->len - 1);
64 #else
65                 return PyUnicode_FromStringAndSize(IDP_Array(prop), prop->len - 1);
66 #endif
67         }
68 }
69
70 static PyObject *idprop_py_from_idp_int(IDProperty *prop)
71 {
72         return PyLong_FromLong((long)prop->data.val);
73 }
74
75 static PyObject *idprop_py_from_idp_float(IDProperty *prop)
76 {
77         return PyFloat_FromDouble((double)(*(float*)(&prop->data.val)));
78 }
79
80 static PyObject *idprop_py_from_idp_double(IDProperty *prop)
81 {
82         return PyFloat_FromDouble((*(double*)(&prop->data.val)));
83 }
84
85 static PyObject *idprop_py_from_idp_group(ID *id, IDProperty *prop, IDProperty *parent)
86 {
87         BPy_IDProperty *group= PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
88         group->id = id;
89         group->prop = prop;
90         group->parent = parent; /* can be NULL */
91         return (PyObject*) group;
92 }
93
94 static PyObject *idprop_py_from_idp_array(ID *id, IDProperty *prop)
95 {
96         BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &BPy_IDArray_Type);
97         array->id = id;
98         array->prop = prop;
99         return (PyObject*) array;
100 }
101
102 static PyObject *idprop_py_from_idp_idparray(ID *id, IDProperty *prop)
103 {
104         PyObject *seq = PyList_New(prop->len), *wrap;
105         IDProperty *array= IDP_IDPArray(prop);
106         int i;
107
108         if (!seq) {
109                 PyErr_Format(PyExc_RuntimeError,
110                              "%s: IDP_IDPARRAY: PyList_New(%d) failed",
111                              __func__, prop->len);
112                 return NULL;
113         }
114
115         for (i=0; i<prop->len; i++) {
116                 wrap= BPy_IDGroup_WrapData(id, array++, prop);
117
118                 if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
119                         return NULL;
120
121                 PyList_SET_ITEM(seq, i, wrap);
122         }
123
124         return seq;
125 }
126
127 /* -------------------------------------------------------------------------- */
128
129 /* use for both array and group */
130 static long BPy_IDGroup_hash(BPy_IDProperty *self)
131 {
132         return _Py_HashPointer(self->prop);
133 }
134
135 static PyObject *BPy_IDGroup_repr(BPy_IDProperty *self)
136 {
137         return PyUnicode_FromFormat( "<bpy id property from \"%s\">", self->id->name);
138 }
139
140 PyObject *BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
141 {
142         switch (prop->type) {
143         case IDP_STRING:
144                 return idprop_py_from_idp_string(prop);
145         case IDP_INT:
146                 return idprop_py_from_idp_int(prop);
147         case IDP_FLOAT:
148                 return idprop_py_from_idp_float(prop);
149         case IDP_DOUBLE:
150                 return idprop_py_from_idp_double(prop);
151         case IDP_GROUP:
152                 return idprop_py_from_idp_group(id, prop, parent);
153         case IDP_ARRAY:
154                 return idprop_py_from_idp_array(id, prop);
155         case IDP_IDPARRAY: /* this could be better a internal type */
156                 return idprop_py_from_idp_idparray(id, prop);
157         default:
158                 Py_RETURN_NONE;
159         }
160 }
161
162 #if 0 /* UNUSED, currenly assignment overwrites into new properties, rather than setting in-place */
163 static int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
164 {
165         switch (prop->type) {
166                 case IDP_STRING:
167                 {
168                         char *st;
169                         if (!PyUnicode_Check(value)) {
170                                 PyErr_SetString(PyExc_TypeError, "expected a string!");
171                                 return -1;
172                         }
173                         /* NOTE: if this code is enabled, bytes support needs to be added */
174 #ifdef USE_STRING_COERCE
175                         {
176                                 int alloc_len;
177                                 PyObject *value_coerce= NULL;
178
179                                 st= (char *)PyC_UnicodeAsByte(value, &value_coerce);
180                                 alloc_len= strlen(st) + 1;
181
182                                 st = _PyUnicode_AsString(value);
183                                 IDP_ResizeArray(prop, alloc_len);
184                                 memcpy(IDP_Array(prop), st, alloc_len);
185                                 Py_XDECREF(value_coerce);
186                         }
187 #else
188                         st = _PyUnicode_AsString(value);
189                         IDP_ResizeArray(prop, strlen(st)+1);
190                         strcpy(IDP_Array(prop), st);
191 #endif
192
193                         return 0;
194                 }
195
196                 case IDP_INT:
197                 {
198                         int ivalue= PyLong_AsSsize_t(value);
199                         if (ivalue==-1 && PyErr_Occurred()) {
200                                 PyErr_SetString(PyExc_TypeError, "expected an int type");
201                                 return -1;
202                         }
203                         prop->data.val = ivalue;
204                         break;
205                 }
206                 case IDP_FLOAT:
207                 {
208                         float fvalue= (float)PyFloat_AsDouble(value);
209                         if (fvalue==-1 && PyErr_Occurred()) {
210                                 PyErr_SetString(PyExc_TypeError, "expected a float");
211                                 return -1;
212                         }
213                         *(float*)&self->prop->data.val = fvalue;
214                         break;
215                 }
216                 case IDP_DOUBLE:
217                 {
218                         double dvalue= PyFloat_AsDouble(value);
219                         if (dvalue==-1 && PyErr_Occurred()) {
220                                 PyErr_SetString(PyExc_TypeError, "expected a float");
221                                 return -1;
222                         }
223                         *(double*)&self->prop->data.val = dvalue;
224                         break;
225                 }
226                 default:
227                         PyErr_SetString(PyExc_AttributeError, "attempt to set read-only attribute!");
228                         return -1;
229         }
230         return 0;
231 }
232 #endif
233
234 static PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *UNUSED(closure))
235 {
236         return PyUnicode_FromString(self->prop->name);
237 }
238
239 static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *UNUSED(closure))
240 {
241         const char *name;
242         Py_ssize_t name_size;
243
244         if (!PyUnicode_Check(value)) {
245                 PyErr_SetString(PyExc_TypeError, "expected a string!");
246                 return -1;
247         }
248
249         name = _PyUnicode_AsStringAndSize(value, &name_size);
250
251         if (name_size > MAX_IDPROP_NAME) {
252                 PyErr_SetString(PyExc_TypeError, "string length cannot exceed 31 characters!");
253                 return -1;
254         }
255
256         memcpy(self->prop->name, name, name_size);
257         return 0;
258 }
259
260 #if 0
261 static PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self)
262 {
263         return PyLong_FromSsize_t(self->prop->type);
264 }
265 #endif
266
267 static PyGetSetDef BPy_IDGroup_getseters[] = {
268         {(char *)"name", (getter)BPy_IDGroup_GetName, (setter)BPy_IDGroup_SetName, (char *)"The name of this Group.", NULL},
269          {NULL, NULL, NULL, NULL, NULL}
270 };
271
272 static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self)
273 {
274         if (self->prop->type != IDP_GROUP) {
275                 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
276                 return -1;
277         }
278
279         return self->prop->len;
280 }
281
282 static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
283 {
284         IDProperty *idprop;
285         const char *name;
286
287         if (self->prop->type  != IDP_GROUP) {
288                 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
289                 return NULL;
290         }
291
292         name= _PyUnicode_AsString(item);
293
294         if (name == NULL) {
295                 PyErr_SetString(PyExc_TypeError, "only strings are allowed as keys of ID properties");
296                 return NULL;
297         }
298
299         idprop= IDP_GetPropertyFromGroup(self->prop, name);
300
301         if (idprop==NULL) {
302                 PyErr_SetString(PyExc_KeyError, "key not in subgroup dict");
303                 return NULL;
304         }
305
306         return BPy_IDGroup_WrapData(self->id, idprop, self->prop);
307 }
308
309 /*returns NULL on success, error string on failure*/
310 static int idp_sequence_type(PyObject *seq)
311 {
312         PyObject *item;
313         int type= IDP_INT;
314
315         Py_ssize_t i, len = PySequence_Size(seq);
316         for (i=0; i < len; i++) {
317                 item = PySequence_GetItem(seq, i);
318                 if (PyFloat_Check(item)) {
319                         if (type == IDP_IDPARRAY) { /* mixed dict/int */
320                                 Py_DECREF(item);
321                                 return -1;
322                         }
323                         type= IDP_DOUBLE;
324                 }
325                 else if (PyLong_Check(item)) {
326                         if (type == IDP_IDPARRAY) { /* mixed dict/int */
327                                 Py_DECREF(item);
328                                 return -1;
329                         }
330                 }
331                 else if (PyMapping_Check(item)) {
332                         if (i != 0 && (type != IDP_IDPARRAY)) { /* mixed dict/int */
333                                 Py_DECREF(item);
334                                 return -1;
335                         }
336                         type= IDP_IDPARRAY;
337                 }
338                 else {
339                         Py_XDECREF(item);
340                         return -1;
341                 }
342
343                 Py_DECREF(item);
344         }
345
346         return type;
347 }
348
349 /* note: group can be a pointer array or a group.
350  * assume we already checked key is a string. */
351 const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *name_obj, IDProperty *group, PyObject *ob)
352 {
353         IDProperty *prop = NULL;
354         IDPropertyTemplate val = {0};
355
356         const char *name= "";
357
358         if (name_obj) {
359                 Py_ssize_t name_size;
360                 name = _PyUnicode_AsStringAndSize(name_obj, &name_size);
361                 if (name_size > MAX_IDPROP_NAME) {
362                         return "the length of IDProperty names is limited to 31 characters";
363                 }
364         }
365
366         if (PyFloat_Check(ob)) {
367                 val.d = PyFloat_AsDouble(ob);
368                 prop = IDP_New(IDP_DOUBLE, &val, name);
369         }
370         else if (PyLong_Check(ob)) {
371                 val.i = (int) PyLong_AsSsize_t(ob);
372                 prop = IDP_New(IDP_INT, &val, name);
373         }
374         else if (PyUnicode_Check(ob)) {
375 #ifdef USE_STRING_COERCE
376                 PyObject *value_coerce= NULL;
377                 val.string.str = (char *)PyC_UnicodeAsByte(ob, &value_coerce);
378                 val.string.subtype = IDP_STRING_SUB_UTF8;
379                 prop = IDP_New(IDP_STRING, &val, name);
380                 Py_XDECREF(value_coerce);
381 #else
382                 val.str = _PyUnicode_AsString(ob);
383                 prop = IDP_New(IDP_STRING, val, name);
384 #endif
385         }
386         else if (PyBytes_Check(ob)) {
387                 val.string.str= PyBytes_AS_STRING(ob);
388                 val.string.len= PyBytes_GET_SIZE(ob);
389                 val.string.subtype= IDP_STRING_SUB_BYTE;
390
391                 prop = IDP_New(IDP_STRING, &val, name);
392                 //prop = IDP_NewString(PyBytes_AS_STRING(ob), name, PyBytes_GET_SIZE(ob));
393                 //prop->subtype= IDP_STRING_SUB_BYTE;
394         }
395         else if (PySequence_Check(ob)) {
396                 PyObject *item;
397                 int i;
398
399                 if ((val.array.type= idp_sequence_type(ob)) == -1)
400                         return "only floats, ints and dicts are allowed in ID property arrays";
401
402                 /*validate sequence and derive type.
403                 we assume IDP_INT unless we hit a float
404                 number; then we assume it's */
405
406                 val.array.len = PySequence_Size(ob);
407
408                 switch(val.array.type) {
409                 case IDP_DOUBLE:
410                         prop = IDP_New(IDP_ARRAY, &val, name);
411                         for (i=0; i<val.array.len; i++) {
412                                 item = PySequence_GetItem(ob, i);
413                                 ((double*)IDP_Array(prop))[i] = (float)PyFloat_AsDouble(item);
414                                 Py_DECREF(item);
415                         }
416                         break;
417                 case IDP_INT:
418                         prop = IDP_New(IDP_ARRAY, &val, name);
419                         for (i=0; i<val.array.len; i++) {
420                                 item = PySequence_GetItem(ob, i);
421                                 ((int*)IDP_Array(prop))[i] = (int)PyLong_AsSsize_t(item);
422                                 Py_DECREF(item);
423                         }
424                         break;
425                 case IDP_IDPARRAY:
426                         prop= IDP_NewIDPArray(name);
427                         for (i=0; i<val.array.len; i++) {
428                                 const char *error;
429                                 item = PySequence_GetItem(ob, i);
430                                 error= BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
431                                 Py_DECREF(item);
432
433                                 if (error)
434                                         return error;
435                         }
436                         break;
437                 }
438         }
439         else if (PyMapping_Check(ob)) {
440                 PyObject *keys, *vals, *key, *pval;
441                 int i, len;
442                 /*yay! we get into recursive stuff now!*/
443                 keys = PyMapping_Keys(ob);
444                 vals = PyMapping_Values(ob);
445
446                 /*we allocate the group first; if we hit any invalid data,
447                   we can delete it easily enough.*/
448                 prop = IDP_New(IDP_GROUP, &val, name);
449                 len = PyMapping_Length(ob);
450                 for (i=0; i<len; i++) {
451                         key = PySequence_GetItem(keys, i);
452                         pval = PySequence_GetItem(vals, i);
453                         if (!PyUnicode_Check(key)) {
454                                 IDP_FreeProperty(prop);
455                                 MEM_freeN(prop);
456                                 Py_XDECREF(keys);
457                                 Py_XDECREF(vals);
458                                 Py_XDECREF(key);
459                                 Py_XDECREF(pval);
460                                 return "invalid element in subgroup dict template!";
461                         }
462                         if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval)) {
463                                 IDP_FreeProperty(prop);
464                                 MEM_freeN(prop);
465                                 Py_XDECREF(keys);
466                                 Py_XDECREF(vals);
467                                 Py_XDECREF(key);
468                                 Py_XDECREF(pval);
469                                 return "invalid element in subgroup dict template!";
470                         }
471                         Py_XDECREF(key);
472                         Py_XDECREF(pval);
473                 }
474                 Py_XDECREF(keys);
475                 Py_XDECREF(vals);
476         }
477         else return "invalid property value";
478
479         if (group->type==IDP_IDPARRAY) {
480                 IDP_AppendArray(group, prop);
481                 // IDP_FreeProperty(item); // IDP_AppendArray does a shallow copy (memcpy), only free memory
482                 MEM_freeN(prop);
483         }
484         else {
485                 IDP_ReplaceInGroup(group, prop);
486         }
487
488         return NULL;
489 }
490
491 int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
492 {
493         if (prop->type  != IDP_GROUP) {
494                 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
495                 return -1;
496         }
497
498         if (val == NULL) { /* del idprop[key] */
499                 IDProperty *pkey = IDP_GetPropertyFromGroup(prop, _PyUnicode_AsString(key));
500                 if (pkey) {
501                         IDP_RemFromGroup(prop, pkey);
502                         IDP_FreeProperty(pkey);
503                         MEM_freeN(pkey);
504                         return 0;
505                 }
506                 else {
507                         PyErr_SetString(PyExc_KeyError, "property not found in group");
508                         return -1;
509                 }
510         }
511         else {
512                 const char *err;
513
514                 if (!PyUnicode_Check(key)) {
515                         PyErr_SetString(PyExc_TypeError, "only strings are allowed as subgroup keys");
516                         return -1;
517                 }
518
519                 err = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
520                 if (err) {
521                         PyErr_SetString(PyExc_KeyError, err );
522                         return -1;
523                 }
524
525                 return 0;
526         }
527 }
528
529 static int BPy_IDGroup_Map_SetItem(BPy_IDProperty *self, PyObject *key, PyObject *val)
530 {
531         return BPy_Wrap_SetMapItem(self->prop, key, val);
532 }
533
534 static PyObject *BPy_IDGroup_iter(BPy_IDProperty *self)
535 {
536         BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &BPy_IDGroup_Iter_Type);
537         iter->group = self;
538         iter->mode = IDPROP_ITER_KEYS;
539         iter->cur = self->prop->data.group.first;
540         Py_XINCREF(iter);
541         return (PyObject*) iter;
542 }
543
544 /* for simple, non nested types this is the same as BPy_IDGroup_WrapData */
545 static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
546 {
547         switch (prop->type) {
548         case IDP_STRING:
549                 return idprop_py_from_idp_string(prop);
550         case IDP_INT:
551                 return idprop_py_from_idp_int(prop);
552         case IDP_FLOAT:
553                 return idprop_py_from_idp_float(prop);
554         case IDP_DOUBLE:
555                 return idprop_py_from_idp_double(prop);
556         case IDP_ARRAY:
557         {
558                 PyObject *seq = PyList_New(prop->len);
559                 int i;
560
561                 if (!seq) {
562                         PyErr_Format(PyExc_RuntimeError,
563                                      "%s: IDP_ARRAY: PyList_New(%d) failed",
564                                      __func__, prop->len);
565                         return NULL;
566                 }
567
568                 switch(prop->subtype) {
569                 case IDP_FLOAT:
570                 {
571                         float *array= (float*)IDP_Array(prop);
572                         for (i=0; i<prop->len; i++) {
573                                 PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
574                         }
575                         break;
576                 }
577                 case IDP_DOUBLE:
578                 {
579                         double *array= (double*)IDP_Array(prop);
580                         for (i=0; i<prop->len; i++) {
581                                 PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
582                         }
583                         break;
584                 }
585                 case IDP_INT:
586                 {
587                         int *array= (int*)IDP_Array(prop);
588                         for (i=0; i<prop->len; i++) {
589                                 PyList_SET_ITEM(seq, i, PyLong_FromLong(array[i]));
590                         }
591                         break;
592                 }
593                 default:
594                         PyErr_Format(PyExc_RuntimeError,
595                                      "%s: invalid/corrupt array type '%d'!",
596                                      __func__, prop->subtype);
597                         Py_DECREF(seq);
598                         return NULL;
599                 }
600
601                 return seq;
602         }
603         case IDP_IDPARRAY:
604         {
605                 PyObject *seq = PyList_New(prop->len), *wrap;
606                 IDProperty *array= IDP_IDPArray(prop);
607                 int i;
608
609                 if (!seq) {
610                         PyErr_Format(PyExc_RuntimeError,
611                                      "%s: IDP_IDPARRAY: PyList_New(%d) failed",
612                                      __func__, prop->len);
613                         return NULL;
614                 }
615
616                 for (i=0; i<prop->len; i++) {
617                         wrap= BPy_IDGroup_MapDataToPy(array++);
618
619                         if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
620                                 return NULL;
621
622                         PyList_SET_ITEM(seq, i, wrap);
623                 }
624                 return seq;
625         }
626         case IDP_GROUP:
627         {
628                 PyObject *dict = PyDict_New(), *wrap;
629                 IDProperty *loop;
630
631                 for (loop=prop->data.group.first; loop; loop=loop->next) {
632                         wrap = BPy_IDGroup_MapDataToPy(loop);
633
634                         if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
635                                 return NULL;
636
637                         PyDict_SetItemString(dict, loop->name, wrap);
638                         Py_DECREF(wrap);
639                 }
640                 return dict;
641         }
642         }
643
644         PyErr_Format(PyExc_RuntimeError,
645                      "%s ERROR: '%s' property exists with a bad type code '%d'!",
646                      __func__, prop->name, prop->type);
647         return NULL;
648 }
649
650 static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value)
651 {
652         IDProperty *idprop;
653         PyObject *pyform;
654         const char *name = _PyUnicode_AsString(value);
655
656         if (!name) {
657                 PyErr_Format(PyExc_TypeError,
658                              "pop expected at least a string argument, not %.200s",
659                              Py_TYPE(value)->tp_name);
660                 return NULL;
661         }
662
663         idprop= IDP_GetPropertyFromGroup(self->prop, name);
664
665         if (idprop) {
666                 pyform = BPy_IDGroup_MapDataToPy(idprop);
667
668                 if (!pyform) {
669                         /*ok something bad happened with the pyobject,
670                           so don't remove the prop from the group.  if pyform is
671                           NULL, then it already should have raised an exception.*/
672                         return NULL;
673                 }
674
675                 IDP_RemFromGroup(self->prop, idprop);
676                 return pyform;
677         }
678
679         PyErr_SetString(PyExc_KeyError, "item not in group");
680         return NULL;
681 }
682
683 static PyObject *BPy_IDGroup_IterItems(BPy_IDProperty *self)
684 {
685         BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &BPy_IDGroup_Iter_Type);
686         iter->group = self;
687         iter->mode = IDPROP_ITER_ITEMS;
688         iter->cur = self->prop->data.group.first;
689         Py_XINCREF(iter);
690         return (PyObject*) iter;
691 }
692
693 /* utility function */
694 static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len, const char *func)
695 {
696         int j;
697
698         printf("%s: ID Property Error found and corrected!\n", func);
699
700         /*fill rest of list with valid references to None*/
701         for (j=len; j<prop->len; j++) {
702                 Py_INCREF(Py_None);
703                 PyList_SET_ITEM(seq, j, Py_None);
704         }
705
706         /*set correct group length*/
707         prop->len = len;
708 }
709
710 PyObject *BPy_Wrap_GetKeys(IDProperty *prop)
711 {
712         PyObject *list = PyList_New(prop->len);
713         IDProperty *loop;
714         int i;
715
716         for (i=0, loop=prop->data.group.first; loop && (i < prop->len); loop=loop->next, i++)
717                 PyList_SET_ITEM(list, i, PyUnicode_FromString(loop->name));
718
719         /* if the id prop is corrupt, count the remaining */
720         for (; loop; loop=loop->next, i++) {}
721
722         if (i != prop->len) { /* if the loop didnt finish, we know the length is wrong */
723                 BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
724                 Py_DECREF(list); /*free the list*/
725                 /*call self again*/
726                 return BPy_Wrap_GetKeys(prop);
727         }
728
729         return list;
730 }
731
732 PyObject *BPy_Wrap_GetValues(ID *id, IDProperty *prop)
733 {
734         PyObject *list = PyList_New(prop->len);
735         IDProperty *loop;
736         int i;
737
738         for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) {
739                 PyList_SET_ITEM(list, i, BPy_IDGroup_WrapData(id, loop, prop));
740         }
741
742         if (i != prop->len) {
743                 BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
744                 Py_DECREF(list); /*free the list*/
745                 /*call self again*/
746                 return BPy_Wrap_GetValues(id, prop);
747         }
748
749         return list;
750 }
751
752 PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
753 {
754         PyObject *seq = PyList_New(prop->len);
755         IDProperty *loop;
756         int i;
757
758         for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) {
759                 PyObject *item= PyTuple_New(2);
760                 PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name));
761                 PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop, prop));
762                 PyList_SET_ITEM(seq, i, item);
763         }
764
765         if (i != prop->len) {
766                 BPy_IDGroup_CorrectListLen(prop, seq, i, __func__);
767                 Py_DECREF(seq); /*free the list*/
768                 /*call self again*/
769                 return BPy_Wrap_GetItems(id, prop);
770         }
771
772         return seq;
773 }
774
775
776 static PyObject *BPy_IDGroup_GetKeys(BPy_IDProperty *self)
777 {
778         return BPy_Wrap_GetKeys(self->prop);
779 }
780
781 static PyObject *BPy_IDGroup_GetValues(BPy_IDProperty *self)
782 {
783         return BPy_Wrap_GetValues(self->id, self->prop);
784 }
785
786 static PyObject *BPy_IDGroup_GetItems(BPy_IDProperty *self)
787 {
788         return BPy_Wrap_GetItems(self->id, self->prop);
789 }
790
791 static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value)
792 {
793         const char *name = _PyUnicode_AsString(value);
794
795         if (!name) {
796                 PyErr_Format(PyExc_TypeError,
797                              "expected a string, not a %.200s",
798                              Py_TYPE(value)->tp_name);
799                 return -1;
800         }
801
802         return IDP_GetPropertyFromGroup(self->prop, name) ? 1:0;
803 }
804
805 static PyObject *BPy_IDGroup_Update(BPy_IDProperty *self, PyObject *value)
806 {
807         PyObject *pkey, *pval;
808         Py_ssize_t i=0;
809
810         if (!PyDict_Check(value)) {
811                 PyErr_Format(PyExc_TypeError,
812                              "expected a dict not a %.200s",
813                              Py_TYPE(value)->tp_name);
814                 return NULL;
815         }
816
817         while (PyDict_Next(value, &i, &pkey, &pval)) {
818                 BPy_IDGroup_Map_SetItem(self, pkey, pval);
819                 if (PyErr_Occurred()) return NULL;
820         }
821
822         Py_RETURN_NONE;
823 }
824
825 static PyObject *BPy_IDGroup_to_dict(BPy_IDProperty *self)
826 {
827         return BPy_IDGroup_MapDataToPy(self->prop);
828 }
829
830
831 /* Matches python dict.get(key, [default]) */
832 static PyObject* BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args)
833 {
834         IDProperty *idprop;
835         char *key;
836         PyObject* def = Py_None;
837
838         if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
839                 return NULL;
840
841         idprop= IDP_GetPropertyFromGroup(self->prop, key);
842         if (idprop) {
843                 PyObject* pyobj = BPy_IDGroup_WrapData(self->id, idprop, self->prop);
844                 if (pyobj)
845                         return pyobj;
846         }
847
848         Py_INCREF(def);
849         return def;
850 }
851
852 static struct PyMethodDef BPy_IDGroup_methods[] = {
853         {"pop", (PyCFunction)BPy_IDGroup_Pop, METH_O,
854                 "pop an item from the group; raises KeyError if the item doesn't exist"},
855         {"iteritems", (PyCFunction)BPy_IDGroup_IterItems, METH_NOARGS,
856                 "iterate through the items in the dict; behaves like dictionary method iteritems"},
857         {"keys", (PyCFunction)BPy_IDGroup_GetKeys, METH_NOARGS,
858                 "get the keys associated with this group as a list of strings"},
859         {"values", (PyCFunction)BPy_IDGroup_GetValues, METH_NOARGS,
860                 "get the values associated with this group"},
861         {"items", (PyCFunction)BPy_IDGroup_GetItems, METH_NOARGS,
862                 "get the items associated with this group"},
863         {"update", (PyCFunction)BPy_IDGroup_Update, METH_O,
864                 "updates the values in the group with the values of another or a dict"},
865         {"get", (PyCFunction)BPy_IDGroup_Get, METH_VARARGS,
866                 "idprop.get(k[,d]) -> idprop[k] if k in idprop, else d.  d defaults to None"},
867         {"to_dict", (PyCFunction)BPy_IDGroup_to_dict, METH_NOARGS,
868                 "return a purely python version of the group"},
869         {NULL, NULL, 0, NULL}
870 };
871
872 static PySequenceMethods BPy_IDGroup_Seq = {
873         (lenfunc) BPy_IDGroup_Map_Len,          /* lenfunc sq_length */
874         NULL,                                                           /* binaryfunc sq_concat */
875         NULL,                                                           /* ssizeargfunc sq_repeat */
876         NULL,                                                           /* ssizeargfunc sq_item */ /* TODO - setting this will allow PySequence_Check to return True */
877         NULL,                                                           /* intintargfunc ***was_sq_slice*** */
878         NULL,                                                           /* intobjargproc sq_ass_item */
879         NULL,                                                           /* ssizeobjargproc ***was_sq_ass_slice*** */
880         (objobjproc) BPy_IDGroup_Contains,      /* objobjproc sq_contains */
881         NULL,                                                           /* binaryfunc sq_inplace_concat */
882         NULL,                                                           /* ssizeargfunc sq_inplace_repeat */
883 };
884
885 static PyMappingMethods BPy_IDGroup_Mapping = {
886         (lenfunc)BPy_IDGroup_Map_Len,           /*inquiry mp_length */
887         (binaryfunc)BPy_IDGroup_Map_GetItem,/*binaryfunc mp_subscript */
888         (objobjargproc)BPy_IDGroup_Map_SetItem, /*objobjargproc mp_ass_subscript */
889 };
890
891 PyTypeObject BPy_IDGroup_Type = {
892         PyVarObject_HEAD_INIT(NULL, 0)
893         /*  For printing, in format "<module>.<name>" */
894         "Blender IDProperty",           /* char *tp_name; */
895         sizeof(BPy_IDProperty),         /* int tp_basicsize; */
896         0,                          /* tp_itemsize;  For allocation */
897
898         /* Methods to implement standard operations */
899
900         NULL,                                           /* destructor tp_dealloc; */
901         NULL,                       /* printfunc tp_print; */
902         NULL,     /* getattrfunc tp_getattr; */
903         NULL,     /* setattrfunc tp_setattr; */
904         NULL,                       /* cmpfunc tp_compare; */
905         (reprfunc)BPy_IDGroup_repr,     /* reprfunc tp_repr; */
906
907         /* Method suites for standard classes */
908
909         NULL,                       /* PyNumberMethods *tp_as_number; */
910         &BPy_IDGroup_Seq,                       /* PySequenceMethods *tp_as_sequence; */
911         &BPy_IDGroup_Mapping,           /* PyMappingMethods *tp_as_mapping; */
912
913         /* More standard operations (here for binary compatibility) */
914
915         (hashfunc)BPy_IDGroup_hash, /* hashfunc tp_hash; */
916         NULL,                       /* ternaryfunc tp_call; */
917         NULL,                       /* reprfunc tp_str; */
918         NULL,                       /* getattrofunc tp_getattro; */
919         NULL,                       /* setattrofunc tp_setattro; */
920
921         /* Functions to access object as input/output buffer */
922         NULL,                       /* PyBufferProcs *tp_as_buffer; */
923
924   /*** Flags to define presence of optional/expanded features ***/
925         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
926
927         NULL,                       /*  char *tp_doc;  Documentation string */
928   /*** Assigned meaning in release 2.0 ***/
929         /* call function for all accessible objects */
930         NULL,                       /* traverseproc tp_traverse; */
931
932         /* delete references to contained objects */
933         NULL,                       /* inquiry tp_clear; */
934
935   /***  Assigned meaning in release 2.1 ***/
936   /*** rich comparisons ***/
937         NULL,                       /* richcmpfunc tp_richcompare; */
938
939   /***  weak reference enabler ***/
940         0,                          /* long tp_weaklistoffset; */
941
942   /*** Added in release 2.2 ***/
943         /*   Iterators */
944         (getiterfunc)BPy_IDGroup_iter, /* getiterfunc tp_iter; */
945         NULL,                       /* iternextfunc tp_iternext; */
946   /*** Attribute descriptor and subclassing stuff ***/
947         BPy_IDGroup_methods,        /* struct PyMethodDef *tp_methods; */
948         NULL,                       /* struct PyMemberDef *tp_members; */
949         BPy_IDGroup_getseters,       /* struct PyGetSetDef *tp_getset; */
950 };
951
952 /********Array Wrapper********/
953
954 static PyTypeObject *idp_array_py_type(BPy_IDArray *self, short *is_double)
955 {
956         switch (self->prop->subtype) {
957                 case IDP_FLOAT:
958                         *is_double= 0;
959                         return &PyFloat_Type;
960                 case IDP_DOUBLE:
961                         *is_double= 1;
962                         return &PyFloat_Type;
963                 case IDP_INT:
964                         *is_double= 0;
965                         return &PyLong_Type;
966         }
967
968         *is_double= 0;
969         return NULL;
970 }
971
972 static PyObject *BPy_IDArray_repr(BPy_IDArray *self)
973 {
974         return PyUnicode_FromFormat("<bpy id property array [%d]>", self->prop->len);
975 }
976
977 static PyObject *BPy_IDArray_GetType(BPy_IDArray *self)
978 {
979         switch(self->prop->subtype) {
980         case IDP_FLOAT:
981                 return PyUnicode_FromString("f");
982         case IDP_DOUBLE:
983                 return PyUnicode_FromString("d");
984         case IDP_INT:
985                 return PyUnicode_FromString("i");
986         }
987
988         PyErr_Format(PyExc_RuntimeError,
989                      "%s: invalid/corrupt array type '%d'!",
990                      __func__, self->prop->subtype);
991
992         return NULL;
993 }
994
995 static PyGetSetDef BPy_IDArray_getseters[] = {
996     /* matches pythons array.typecode */
997         {(char *)"typecode", (getter)BPy_IDArray_GetType, (setter)NULL, (char *)"The type of the data in the array, is an int.", NULL},
998         {NULL, NULL, NULL, NULL, NULL},
999 };
1000
1001 static PyObject *BPy_IDArray_to_list(BPy_IDArray *self)
1002 {
1003         return BPy_IDGroup_MapDataToPy(self->prop);
1004 }
1005
1006 static PyMethodDef BPy_IDArray_methods[] = {
1007         {"to_list", (PyCFunction)BPy_IDArray_to_list, METH_NOARGS,
1008                 "return the array as a list"},
1009         {NULL, NULL, 0, NULL}
1010 };
1011
1012 static int BPy_IDArray_Len(BPy_IDArray *self)
1013 {
1014         return self->prop->len;
1015 }
1016
1017 static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index)
1018 {
1019         if (index < 0 || index >= self->prop->len) {
1020                 PyErr_SetString(PyExc_IndexError, "index out of range!");
1021                 return NULL;
1022         }
1023
1024         switch (self->prop->subtype) {
1025                 case IDP_FLOAT:
1026                         return PyFloat_FromDouble(((float*)IDP_Array(self->prop))[index]);
1027                 case IDP_DOUBLE:
1028                         return PyFloat_FromDouble(((double*)IDP_Array(self->prop))[index]);
1029                 case IDP_INT:
1030                         return PyLong_FromLong((long)((int*)IDP_Array(self->prop))[index]);
1031         }
1032
1033         PyErr_Format(PyExc_RuntimeError,
1034                      "%s: invalid/corrupt array type '%d'!",
1035                      __func__, self->prop->subtype);
1036
1037         return NULL;
1038 }
1039
1040 static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value)
1041 {
1042         int i;
1043         float f;
1044         double d;
1045
1046         if (index < 0 || index >= self->prop->len) {
1047                 PyErr_SetString(PyExc_RuntimeError, "index out of range!");
1048                 return -1;
1049         }
1050
1051         switch (self->prop->subtype) {
1052                 case IDP_FLOAT:
1053                         f= (float)PyFloat_AsDouble(value);
1054                         if (f==-1 && PyErr_Occurred()) {
1055                                 PyErr_SetString(PyExc_TypeError, "expected a float");
1056                                 return -1;
1057                         }
1058                         ((float*)IDP_Array(self->prop))[index] = f;
1059                         break;
1060                 case IDP_DOUBLE:
1061                         d= PyFloat_AsDouble(value);
1062                         if (d==-1 && PyErr_Occurred()) {
1063                                 PyErr_SetString(PyExc_TypeError, "expected a float");
1064                                 return -1;
1065                         }
1066                         ((double*)IDP_Array(self->prop))[index] = d;
1067                         break;
1068                 case IDP_INT:
1069                         i= PyLong_AsSsize_t(value);
1070                         if (i==-1 && PyErr_Occurred()) {
1071                                 PyErr_SetString(PyExc_TypeError, "expected an int type");
1072                                 return -1;
1073                         }
1074
1075                         ((int*)IDP_Array(self->prop))[index] = i;
1076                         break;
1077         }
1078         return 0;
1079 }
1080
1081 static PySequenceMethods BPy_IDArray_Seq = {
1082         (lenfunc) BPy_IDArray_Len,                      /* inquiry sq_length */
1083         NULL,                                                           /* binaryfunc sq_concat */
1084         NULL,                                                           /* intargfunc sq_repeat */
1085         (ssizeargfunc)BPy_IDArray_GetItem,      /* intargfunc sq_item */
1086         NULL,                                                           /* intintargfunc sq_slice */
1087         (ssizeobjargproc)BPy_IDArray_SetItem,/* intobjargproc sq_ass_item */
1088         NULL,                                                           /* intintobjargproc sq_ass_slice */
1089         NULL,                                                           /* objobjproc sq_contains */
1090                                 /* Added in release 2.0 */
1091         NULL,                                                           /* binaryfunc sq_inplace_concat */
1092         NULL,                                                           /* intargfunc sq_inplace_repeat */
1093 };
1094
1095
1096
1097 /* sequence slice (get): idparr[a:b] */
1098 static PyObject *BPy_IDArray_slice(BPy_IDArray *self, int begin, int end)
1099 {
1100         IDProperty *prop= self->prop;
1101         PyObject *tuple;
1102         int count;
1103
1104         CLAMP(begin, 0, prop->len);
1105         if (end<0) end= prop->len+end+1;
1106         CLAMP(end, 0, prop->len);
1107         begin= MIN2(begin, end);
1108
1109         tuple= PyTuple_New(end - begin);
1110
1111         switch (prop->subtype) {
1112                 case IDP_FLOAT:
1113                 {
1114                         float *array= (float*)IDP_Array(prop);
1115                         for (count = begin; count < end; count++) {
1116                                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(array[count]));
1117                         }
1118                         break;
1119                 }
1120                 case IDP_DOUBLE:
1121                 {
1122                         double *array= (double*)IDP_Array(prop);
1123                         for (count = begin; count < end; count++) {
1124                                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(array[count]));
1125                         }
1126                         break;
1127                 }
1128                 case IDP_INT:
1129                 {
1130                         int *array= (int*)IDP_Array(prop);
1131                         for (count = begin; count < end; count++) {
1132                                 PyTuple_SET_ITEM(tuple, count - begin, PyLong_FromLong(array[count]));
1133                         }
1134                         break;
1135                 }
1136         }
1137
1138         return tuple;
1139 }
1140 /* sequence slice (set): idparr[a:b] = value */
1141 static int BPy_IDArray_ass_slice(BPy_IDArray *self, int begin, int end, PyObject *seq)
1142 {
1143         IDProperty *prop= self->prop;
1144         short is_double= 0;
1145         const PyTypeObject *py_type= idp_array_py_type(self, &is_double);
1146         const size_t elem_size= is_double ? sizeof(double) : sizeof(float);
1147         size_t alloc_len;
1148         size_t size;
1149         void *vec;
1150
1151         CLAMP(begin, 0, prop->len);
1152         CLAMP(end, 0, prop->len);
1153         begin = MIN2(begin, end);
1154
1155         size = (end - begin);
1156         alloc_len= size * elem_size;
1157
1158         vec= MEM_mallocN(alloc_len, "array assignment"); /* NOTE: we count on int/float being the same size here */
1159         if (PyC_AsArray(vec, seq, size, py_type, is_double, "slice assignment: ") == -1) {
1160                 MEM_freeN(vec);
1161                 return -1;
1162         }
1163
1164         memcpy((void *)(((char *)IDP_Array(prop)) + (begin * elem_size)), vec, alloc_len);
1165
1166         MEM_freeN(vec);
1167         return 0;
1168 }
1169
1170 static PyObject *BPy_IDArray_subscript(BPy_IDArray* self, PyObject* item)
1171 {
1172         if (PyIndex_Check(item)) {
1173                 Py_ssize_t i;
1174                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1175                 if (i == -1 && PyErr_Occurred())
1176                         return NULL;
1177                 if (i < 0)
1178                         i += self->prop->len;
1179                 return BPy_IDArray_GetItem(self, i);
1180         }
1181         else if (PySlice_Check(item)) {
1182                 Py_ssize_t start, stop, step, slicelength;
1183
1184                 if (PySlice_GetIndicesEx((void *)item, self->prop->len, &start, &stop, &step, &slicelength) < 0)
1185                         return NULL;
1186
1187                 if (slicelength <= 0) {
1188                         return PyTuple_New(0);
1189                 }
1190                 else if (step == 1) {
1191                         return BPy_IDArray_slice(self, start, stop);
1192                 }
1193                 else {
1194                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with vectors");
1195                         return NULL;
1196                 }
1197         }
1198         else {
1199                 PyErr_Format(PyExc_TypeError,
1200                              "vector indices must be integers, not %.200s",
1201                              __func__, Py_TYPE(item)->tp_name);
1202                 return NULL;
1203         }
1204 }
1205
1206 static int BPy_IDArray_ass_subscript(BPy_IDArray* self, PyObject* item, PyObject* value)
1207 {
1208         if (PyIndex_Check(item)) {
1209                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1210                 if (i == -1 && PyErr_Occurred())
1211                         return -1;
1212                 if (i < 0)
1213                         i += self->prop->len;
1214                 return BPy_IDArray_SetItem(self, i, value);
1215         }
1216         else if (PySlice_Check(item)) {
1217                 Py_ssize_t start, stop, step, slicelength;
1218
1219                 if (PySlice_GetIndicesEx((void *)item, self->prop->len, &start, &stop, &step, &slicelength) < 0)
1220                         return -1;
1221
1222                 if (step == 1)
1223                         return BPy_IDArray_ass_slice(self, start, stop, value);
1224                 else {
1225                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with vectors");
1226                         return -1;
1227                 }
1228         }
1229         else {
1230                 PyErr_Format(PyExc_TypeError,
1231                              "vector indices must be integers, not %.200s",
1232                              Py_TYPE(item)->tp_name);
1233                 return -1;
1234         }
1235 }
1236
1237 static PyMappingMethods BPy_IDArray_AsMapping = {
1238         (lenfunc)BPy_IDArray_Len,
1239         (binaryfunc)BPy_IDArray_subscript,
1240         (objobjargproc)BPy_IDArray_ass_subscript
1241 };
1242
1243
1244 PyTypeObject BPy_IDArray_Type = {
1245         PyVarObject_HEAD_INIT(NULL, 0)
1246         /*  For printing, in format "<module>.<name>" */
1247         "Blender IDArray",           /* char *tp_name; */
1248         sizeof(BPy_IDArray),       /* int tp_basicsize; */
1249         0,                          /* tp_itemsize;  For allocation */
1250
1251         /* Methods to implement standard operations */
1252
1253         NULL,                                           /* destructor tp_dealloc; */
1254         NULL,                       /* printfunc tp_print; */
1255         NULL,     /* getattrfunc tp_getattr; */
1256         NULL,     /* setattrfunc tp_setattr; */
1257         NULL,                       /* cmpfunc tp_compare; */
1258         (reprfunc)BPy_IDArray_repr,     /* reprfunc tp_repr; */
1259
1260         /* Method suites for standard classes */
1261
1262         NULL,                       /* PyNumberMethods *tp_as_number; */
1263         &BPy_IDArray_Seq,               /* PySequenceMethods *tp_as_sequence; */
1264         &BPy_IDArray_AsMapping,     /* PyMappingMethods *tp_as_mapping; */
1265
1266         /* More standard operations (here for binary compatibility) */
1267
1268         NULL,                                           /* hashfunc tp_hash; */
1269         NULL,                       /* ternaryfunc tp_call; */
1270         NULL,                       /* reprfunc tp_str; */
1271         NULL,                       /* getattrofunc tp_getattro; */
1272         NULL,                       /* setattrofunc tp_setattro; */
1273
1274         /* Functions to access object as input/output buffer */
1275         NULL,                       /* PyBufferProcs *tp_as_buffer; */
1276
1277   /*** Flags to define presence of optional/expanded features ***/
1278         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
1279
1280         NULL,                       /*  char *tp_doc;  Documentation string */
1281   /*** Assigned meaning in release 2.0 ***/
1282         /* call function for all accessible objects */
1283         NULL,                       /* traverseproc tp_traverse; */
1284
1285         /* delete references to contained objects */
1286         NULL,                       /* inquiry tp_clear; */
1287
1288   /***  Assigned meaning in release 2.1 ***/
1289   /*** rich comparisons ***/
1290         NULL,                       /* richcmpfunc tp_richcompare; */
1291
1292   /***  weak reference enabler ***/
1293         0,                          /* long tp_weaklistoffset; */
1294
1295   /*** Added in release 2.2 ***/
1296         /*   Iterators */
1297         NULL,                       /* getiterfunc tp_iter; */
1298         NULL,                       /* iternextfunc tp_iternext; */
1299
1300   /*** Attribute descriptor and subclassing stuff ***/
1301         BPy_IDArray_methods,            /* struct PyMethodDef *tp_methods; */
1302         NULL,                       /* struct PyMemberDef *tp_members; */
1303         BPy_IDArray_getseters,       /* struct PyGetSetDef *tp_getset; */
1304         NULL,                       /* struct _typeobject *tp_base; */
1305         NULL,                       /* PyObject *tp_dict; */
1306         NULL,                       /* descrgetfunc tp_descr_get; */
1307         NULL,                       /* descrsetfunc tp_descr_set; */
1308         0,                          /* long tp_dictoffset; */
1309         NULL,                       /* initproc tp_init; */
1310         NULL,                       /* allocfunc tp_alloc; */
1311         NULL,                       /* newfunc tp_new; */
1312         /*  Low-level free-memory routine */
1313         NULL,                       /* freefunc tp_free;  */
1314         /* For PyObject_IS_GC */
1315         NULL,                       /* inquiry tp_is_gc;  */
1316         NULL,                       /* PyObject *tp_bases; */
1317         /* method resolution order */
1318         NULL,                       /* PyObject *tp_mro;  */
1319         NULL,                       /* PyObject *tp_cache; */
1320         NULL,                       /* PyObject *tp_subclasses; */
1321         NULL,                       /* PyObject *tp_weaklist; */
1322         NULL
1323 };
1324
1325 /*********** ID Property Group iterator ********/
1326
1327 static PyObject *IDGroup_Iter_iterself(PyObject *self)
1328 {
1329         Py_XINCREF(self);
1330         return self;
1331 }
1332
1333 static PyObject *IDGroup_Iter_repr(BPy_IDGroup_Iter *self)
1334 {
1335         return PyUnicode_FromFormat("(ID Property Group Iter \"%s\")", self->group->prop->name);
1336 }
1337
1338 static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self)
1339 {
1340         IDProperty *cur=NULL;
1341         PyObject *ret;
1342
1343         if (self->cur) {
1344                 cur = self->cur;
1345                 self->cur = self->cur->next;
1346                 if (self->mode == IDPROP_ITER_ITEMS) {
1347                         ret = PyTuple_New(2);
1348                         PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name));
1349                         PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
1350                         return ret;
1351                 }
1352                 else {
1353                         return PyUnicode_FromString(cur->name);
1354                 }
1355         }
1356         else {
1357                 PyErr_SetString(PyExc_StopIteration, "iterator at end");
1358                 return NULL;
1359         }
1360 }
1361
1362 PyTypeObject BPy_IDGroup_Iter_Type = {
1363         PyVarObject_HEAD_INIT(NULL, 0)
1364         /*  For printing, in format "<module>.<name>" */
1365         "Blender IDGroup_Iter",           /* char *tp_name; */
1366         sizeof( BPy_IDGroup_Iter ),       /* int tp_basicsize; */
1367         0,                          /* tp_itemsize;  For allocation */
1368
1369         /* Methods to implement standard operations */
1370
1371         NULL,                                           /* destructor tp_dealloc; */
1372         NULL,                       /* printfunc tp_print; */
1373         NULL,     /* getattrfunc tp_getattr; */
1374         NULL,     /* setattrfunc tp_setattr; */
1375         NULL,                       /* cmpfunc tp_compare; */
1376         ( reprfunc ) IDGroup_Iter_repr,     /* reprfunc tp_repr; */
1377
1378         /* Method suites for standard classes */
1379
1380         NULL,                       /* PyNumberMethods *tp_as_number; */
1381         NULL,                                           /* PySequenceMethods *tp_as_sequence; */
1382         NULL,                       /* PyMappingMethods *tp_as_mapping; */
1383
1384         /* More standard operations (here for binary compatibility) */
1385
1386         NULL,                       /* hashfunc tp_hash; */
1387         NULL,                       /* ternaryfunc tp_call; */
1388         NULL,                       /* reprfunc tp_str; */
1389         NULL,                       /* getattrofunc tp_getattro; */
1390         NULL,                       /* setattrofunc tp_setattro; */
1391
1392         /* Functions to access object as input/output buffer */
1393         NULL,                       /* PyBufferProcs *tp_as_buffer; */
1394
1395   /*** Flags to define presence of optional/expanded features ***/
1396         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
1397
1398         NULL,                       /*  char *tp_doc;  Documentation string */
1399   /*** Assigned meaning in release 2.0 ***/
1400         /* call function for all accessible objects */
1401         NULL,                       /* traverseproc tp_traverse; */
1402
1403         /* delete references to contained objects */
1404         NULL,                       /* inquiry tp_clear; */
1405
1406   /***  Assigned meaning in release 2.1 ***/
1407   /*** rich comparisons ***/
1408         NULL,                       /* richcmpfunc tp_richcompare; */
1409
1410   /***  weak reference enabler ***/
1411         0,                          /* long tp_weaklistoffset; */
1412
1413   /*** Added in release 2.2 ***/
1414         /*   Iterators */
1415         IDGroup_Iter_iterself,              /* getiterfunc tp_iter; */
1416         (iternextfunc) BPy_Group_Iter_Next, /* iternextfunc tp_iternext; */
1417 };
1418
1419 void IDProp_Init_Types(void)
1420 {
1421         PyType_Ready(&BPy_IDGroup_Type);
1422         PyType_Ready(&BPy_IDGroup_Iter_Type);
1423         PyType_Ready(&BPy_IDArray_Type);
1424 }