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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
|
/**
* $Id$
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*------------------------------
* PyObjectPlus cpp
*
* C++ library routines for Crawl 3.2
*
* Derived from work by
* David Redish
* graduate student
* Computer Science Department
* Carnegie Mellon University (CMU)
* Center for the Neural Basis of Cognition (CNBC)
* http://www.python.org/doc/PyCPP.html
*
------------------------------*/
#include <MT_assert.h>
#include "stdlib.h"
#include "PyObjectPlus.h"
#include "STR_String.h"
#include "MT_Vector3.h"
#include "MEM_guardedalloc.h"
PyObjectPlus::~PyObjectPlus()
{
#ifndef DISABLE_PYTHON
if(m_proxy) {
BGE_PROXY_REF(m_proxy)= NULL;
Py_DECREF(m_proxy); /* Remove own reference, python may still have 1 */
}
// assert(ob_refcnt==0);
#endif
}
PyObjectPlus::PyObjectPlus() : SG_QList() // constructor
{
#ifndef DISABLE_PYTHON
m_proxy= NULL;
#endif
};
void PyObjectPlus::ProcessReplica()
{
#ifndef DISABLE_PYTHON
/* Clear the proxy, will be created again if needed with GetProxy()
* otherwise the PyObject will point to the wrong reference */
m_proxy= NULL;
#endif
}
/* Sometimes we might want to manually invalidate a BGE type even if
* it hasnt been released by the BGE, say for example when an object
* is removed from a scene, accessing it may cause problems.
*
* In this case the current proxy is made invalid, disowned,
* and will raise an error on access. However if python can get access
* to this class again it will make a new proxy and work as expected.
*/
void PyObjectPlus::InvalidateProxy() // check typename of each parent
{
#ifndef DISABLE_PYTHON
if(m_proxy) {
BGE_PROXY_REF(m_proxy)=NULL;
Py_DECREF(m_proxy);
m_proxy= NULL;
}
#endif
}
#ifndef DISABLE_PYTHON
/*------------------------------
* PyObjectPlus Type -- Every class, even the abstract one should have a Type
------------------------------*/
PyTypeObject PyObjectPlus::Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"PyObjectPlus", /*tp_name*/
sizeof(PyObjectPlus_Proxy), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,0,0,0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
0,0,0,0,0,0,0,
Methods,
0,
0,
NULL // no subtype
};
PyObject *PyObjectPlus::py_base_repr(PyObject *self) // This should be the entry in Type.
{
PyObjectPlus *self_plus= BGE_PROXY_REF(self);
if(self_plus==NULL) {
PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
return NULL;
}
return self_plus->py_repr();
}
PyObject * PyObjectPlus::py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyTypeObject *base_type;
PyObjectPlus_Proxy *base = NULL;
if (!PyArg_ParseTuple(args, "O:Base PyObjectPlus", &base))
return NULL;
/* the 'base' PyObject may be subclassed (multiple times even)
* we need to find the first C++ defined class to check 'type'
* is a subclass of the base arguments type.
*
* This way we can share one tp_new function for every PyObjectPlus
*
* eg.
*
* # CustomOb is called 'type' in this C code
* class CustomOb(GameTypes.KX_GameObject):
* pass
*
* # this calls py_base_new(...), the type of 'CustomOb' is checked to be a subclass of the 'cont.owner' type
* ob = CustomOb(cont.owner)
*
* */
base_type= Py_TYPE(base);
while(base_type && !BGE_PROXY_CHECK_TYPE(base_type))
base_type= base_type->tp_base;
if(base_type==NULL || !BGE_PROXY_CHECK_TYPE(base_type)) {
PyErr_SetString(PyExc_TypeError, "can't subclass from a blender game type because the argument given is not a game class or subclass");
return NULL;
}
/* use base_type rather then Py_TYPE(base) because we could alredy be subtyped */
if(!PyType_IsSubtype(type, base_type)) {
PyErr_Format(PyExc_TypeError, "can't subclass blender game type <%s> from <%s> because it is not a subclass", base_type->tp_name, type->tp_name);
return NULL;
}
/* invalidate the existing base and return a new subclassed one,
* this is a bit dodgy in that it also attaches its self to the existing object
* which is not really 'correct' python OO but for our use its OK. */
PyObjectPlus_Proxy *ret = (PyObjectPlus_Proxy *) type->tp_alloc(type, 0); /* starts with 1 ref, used for the return ref' */
ret->ref= base->ref;
ret->ptr= base->ptr;
ret->py_owns= base->py_owns;
ret->py_ref = base->py_ref;
if (ret->py_ref) {
base->ref= NULL; /* invalidate! disallow further access */
base->ptr = NULL;
if (ret->ref)
ret->ref->m_proxy= NULL;
/* 'base' may be free'd after this func finished but not necessarily
* there is no reference to the BGE data now so it will throw an error on access */
Py_DECREF(base);
if (ret->ref) {
ret->ref->m_proxy= (PyObject *)ret; /* no need to add a ref because one is added when creating. */
Py_INCREF(ret); /* we return a new ref but m_proxy holds a ref so we need to add one */
}
} else {
// generic structures don't hold a reference to this proxy, so don't increment ref count
if (ret->py_owns)
// but if the proxy owns the structure, there can be only one owner
base->ptr= NULL;
}
return (PyObject *)ret;
}
void PyObjectPlus::py_base_dealloc(PyObject *self) // python wrapper
{
if (BGE_PROXY_PYREF(self)) {
PyObjectPlus *self_plus= BGE_PROXY_REF(self);
if(self_plus) {
if(BGE_PROXY_PYOWNS(self)) { /* Does python own this?, then delete it */
self_plus->m_proxy = NULL; /* Need this to stop ~PyObjectPlus from decrefing m_proxy otherwise its decref'd twice and py-debug crashes */
delete self_plus;
}
BGE_PROXY_REF(self)= NULL; // not really needed
}
// the generic pointer is not deleted directly, only through self_plus
BGE_PROXY_PTR(self)= NULL; // not really needed
} else {
void *ptr= BGE_PROXY_PTR(self);
if(ptr) {
if(BGE_PROXY_PYOWNS(self)) { /* Does python own this?, then delete it */
// generic structure owned by python MUST be created though MEM_alloc
MEM_freeN(ptr);
}
BGE_PROXY_PTR(self)= NULL; // not really needed
}
}
#if 0
/* is ok normally but not for subtyping, use tp_free instead. */
PyObject_DEL( self );
#else
Py_TYPE(self)->tp_free(self);
#endif
};
/*------------------------------
* PyObjectPlus Methods -- Every class, even the abstract one should have a Methods
------------------------------*/
PyMethodDef PyObjectPlus::Methods[] = {
{NULL, NULL} /* Sentinel */
};
#define attr_invalid (&(PyObjectPlus::Attributes[0]))
PyAttributeDef PyObjectPlus::Attributes[] = {
KX_PYATTRIBUTE_RO_FUNCTION("invalid", PyObjectPlus, pyattr_get_invalid),
{NULL} //Sentinel
};
PyObject* PyObjectPlus::pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
return PyBool_FromLong(self_v ? 0:1);
}
/* note, this is called as a python 'getset, where the PyAttributeDef is the closure */
PyObject *PyObjectPlus::py_get_attrdef(PyObject *self_py, const PyAttributeDef *attrdef)
{
PyObjectPlus *ref= (BGE_PROXY_REF(self_py));
char* ptr = (attrdef->m_usePtr) ? (char*)BGE_PROXY_PTR(self_py) : (char*)ref;
if(ptr == NULL || (BGE_PROXY_PYREF(self_py) && (ref==NULL || !ref->py_is_valid()))) {
if(attrdef == attr_invalid)
Py_RETURN_TRUE; // dont bother running the function
PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
return NULL;
}
if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
{
// fake attribute, ignore
return NULL;
}
if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
{
// the attribute has no field correspondance, handover processing to function.
if (attrdef->m_getFunction == NULL)
return NULL;
return (*attrdef->m_getFunction)(ref, attrdef);
}
ptr += attrdef->m_offset;
if (attrdef->m_length > 1)
{
PyObject* resultlist = PyList_New(attrdef->m_length);
for (unsigned int i=0; i<attrdef->m_length; i++)
{
switch (attrdef->m_type) {
case KX_PYATTRIBUTE_TYPE_BOOL:
{
bool *val = reinterpret_cast<bool*>(ptr);
ptr += sizeof(bool);
PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *val = reinterpret_cast<short int*>(ptr);
ptr += sizeof(short int);
PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
break;
}
case KX_PYATTRIBUTE_TYPE_ENUM:
// enum are like int, just make sure the field size is the same
if (sizeof(int) != attrdef->m_size)
{
Py_DECREF(resultlist);
return NULL;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *val = reinterpret_cast<int*>(ptr);
ptr += sizeof(int);
PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
break;
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *val = reinterpret_cast<float*>(ptr);
ptr += sizeof(float);
PyList_SET_ITEM(resultlist,i,PyFloat_FromDouble(*val));
break;
}
default:
// no support for array of complex data
Py_DECREF(resultlist);
return NULL;
}
}
return resultlist;
}
else
{
switch (attrdef->m_type) {
case KX_PYATTRIBUTE_TYPE_FLAG:
{
bool bval;
switch (attrdef->m_size) {
case 1:
{
unsigned char *val = reinterpret_cast<unsigned char*>(ptr);
bval = (*val & attrdef->m_imin);
break;
}
case 2:
{
unsigned short *val = reinterpret_cast<unsigned short*>(ptr);
bval = (*val & attrdef->m_imin);
break;
}
case 4:
{
unsigned int *val = reinterpret_cast<unsigned int*>(ptr);
bval = (*val & attrdef->m_imin);
break;
}
default:
return NULL;
}
if (attrdef->m_imax)
bval = !bval;
return PyLong_FromSsize_t(bval);
}
case KX_PYATTRIBUTE_TYPE_BOOL:
{
bool *val = reinterpret_cast<bool*>(ptr);
return PyLong_FromSsize_t(*val);
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *val = reinterpret_cast<short int*>(ptr);
return PyLong_FromSsize_t(*val);
}
case KX_PYATTRIBUTE_TYPE_ENUM:
// enum are like int, just make sure the field size is the same
if (sizeof(int) != attrdef->m_size)
{
return NULL;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *val = reinterpret_cast<int*>(ptr);
return PyLong_FromSsize_t(*val);
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *val = reinterpret_cast<float*>(ptr);
if (attrdef->m_imin == 0) {
if (attrdef->m_imax == 0) {
return PyFloat_FromDouble(*val);
} else {
// vector, verify size
if (attrdef->m_size != attrdef->m_imax*sizeof(float))
{
return NULL;
}
#ifdef USE_MATHUTILS
return newVectorObject(val, attrdef->m_imax, Py_NEW, NULL);
#else
PyObject* resultlist = PyList_New(attrdef->m_imax);
for (unsigned int i=0; i<attrdef->m_imax; i++)
{
PyList_SET_ITEM(resultlist,i,PyFloat_FromDouble(val[i]));
}
return resultlist;
#endif
}
} else {
// matrix case
if (attrdef->m_size != attrdef->m_imax*attrdef->m_imin*sizeof(float))
{
return NULL;
}
#ifdef USE_MATHUTILS
return newMatrixObject(val, attrdef->m_imin, attrdef->m_imax, Py_WRAP, NULL);
#else
PyObject* rowlist = PyList_New(attrdef->m_imin);
for (unsigned int i=0; i<attrdef->m_imin; i++)
{
PyObject* collist = PyList_New(attrdef->m_imax);
for (unsigned int j=0; j<attrdef->m_imax; j++)
{
PyList_SET_ITEM(collist,j,PyFloat_FromDouble(val[j]));
}
PyList_SET_ITEM(rowlist,i,collist);
val += attrdef->m_imax;
}
return rowlist;
#endif
}
}
case KX_PYATTRIBUTE_TYPE_VECTOR:
{
MT_Vector3 *val = reinterpret_cast<MT_Vector3*>(ptr);
#ifdef USE_MATHUTILS
float fval[3]= {(*val)[0], (*val)[1], (*val)[2]};
return newVectorObject(fval, 3, Py_NEW, NULL);
#else
PyObject* resultlist = PyList_New(3);
for (unsigned int i=0; i<3; i++)
{
PyList_SET_ITEM(resultlist,i,PyFloat_FromDouble((*val)[i]));
}
return resultlist;
#endif
}
case KX_PYATTRIBUTE_TYPE_STRING:
{
STR_String *val = reinterpret_cast<STR_String*>(ptr);
return PyUnicode_FromString(*val);
}
case KX_PYATTRIBUTE_TYPE_CHAR:
{
return PyUnicode_FromString(ptr);
}
default:
return NULL;
}
}
}
static bool py_check_attr_float(float *var, PyObject *value, const PyAttributeDef *attrdef)
{
double val = PyFloat_AsDouble(value);
if (val == -1.0 && PyErr_Occurred())
{
PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
return false;
}
if (attrdef->m_clamp)
{
if (val < attrdef->m_fmin)
val = attrdef->m_fmin;
else if (val > attrdef->m_fmax)
val = attrdef->m_fmax;
}
else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
{
PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
return false;
}
*var = (float)val;
return true;
}
/* note, this is called as a python getset */
int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef)
{
PyObjectPlus *ref= (BGE_PROXY_REF(self_py));
char* ptr = (attrdef->m_usePtr) ? (char*)BGE_PROXY_PTR(self_py) : (char*)ref;
if(ref==NULL || !ref->py_is_valid() || ptr==NULL) {
PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
return PY_SET_ATTR_FAIL;
}
void *undoBuffer = NULL;
void *sourceBuffer = NULL;
size_t bufferSize = 0;
PyObject *item = NULL; // to store object that must be dereferenced in case of error
PyObject *list = NULL; // to store object that must be dereferenced in case of error
ptr += attrdef->m_offset;
if (attrdef->m_length > 1)
{
if (!PySequence_Check(value))
{
PyErr_Format(PyExc_TypeError, "expected a sequence for attribute \"%s\"", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
if (PySequence_Size(value) != attrdef->m_length)
{
PyErr_Format(PyExc_TypeError, "incorrect number of elements in sequence for attribute \"%s\"", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_FUNCTION:
if (attrdef->m_setFunction == NULL)
{
PyErr_Format(PyExc_AttributeError, "function attribute without function for attribute \"%s\", report to blender.org", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
return (*attrdef->m_setFunction)(ref, attrdef, value);
case KX_PYATTRIBUTE_TYPE_BOOL:
bufferSize = sizeof(bool);
break;
case KX_PYATTRIBUTE_TYPE_SHORT:
bufferSize = sizeof(short int);
break;
case KX_PYATTRIBUTE_TYPE_ENUM:
case KX_PYATTRIBUTE_TYPE_INT:
bufferSize = sizeof(int);
break;
case KX_PYATTRIBUTE_TYPE_FLOAT:
bufferSize = sizeof(float);
break;
default:
// should not happen
PyErr_Format(PyExc_AttributeError, "Unsupported attribute type for attribute \"%s\", report to blender.org", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
// let's implement a smart undo method
bufferSize *= attrdef->m_length;
undoBuffer = malloc(bufferSize);
sourceBuffer = ptr;
if (undoBuffer)
{
memcpy(undoBuffer, sourceBuffer, bufferSize);
}
for (int i=0; i<attrdef->m_length; i++)
{
item = PySequence_GetItem(value, i); /* new ref */
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_BOOL:
{
bool *var = reinterpret_cast<bool*>(ptr);
ptr += sizeof(bool);
if (PyLong_Check(item))
{
*var = (PyLong_AsSsize_t(item) != 0);
}
else if (PyBool_Check(item))
{
*var = (item == Py_True);
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer or a bool for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *var = reinterpret_cast<short int*>(ptr);
ptr += sizeof(short int);
if (PyLong_Check(item))
{
long val = PyLong_AsSsize_t(item);
if (attrdef->m_clamp)
{
if (val < attrdef->m_imin)
val = attrdef->m_imin;
else if (val > attrdef->m_imax)
val = attrdef->m_imax;
}
else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
*var = (short int)val;
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_ENUM:
// enum are equivalent to int, just make sure that the field size matches:
if (sizeof(int) != attrdef->m_size)
{
PyErr_Format(PyExc_AttributeError, "Size check error for attribute, \"%s\", report to blender.org", attrdef->m_name);
goto UNDO_AND_ERROR;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *var = reinterpret_cast<int*>(ptr);
ptr += sizeof(int);
if (PyLong_Check(item))
{
long val = PyLong_AsSsize_t(item);
if (attrdef->m_clamp)
{
if (val < attrdef->m_imin)
val = attrdef->m_imin;
else if (val > attrdef->m_imax)
val = attrdef->m_imax;
}
else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
*var = (int)val;
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *var = reinterpret_cast<float*>(ptr);
ptr += sizeof(float);
double val = PyFloat_AsDouble(item);
if (val == -1.0 && PyErr_Occurred())
{
PyErr_Format(PyExc_TypeError, "expected a float for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
else if (attrdef->m_clamp)
{
if (val < attrdef->m_fmin)
val = attrdef->m_fmin;
else if (val > attrdef->m_fmax)
val = attrdef->m_fmax;
}
else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
{
PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
goto UNDO_AND_ERROR;
}
*var = (float)val;
break;
}
default:
// should not happen
PyErr_Format(PyExc_AttributeError, "type check error for attribute \"%s\", report to blender.org", attrdef->m_name);
goto UNDO_AND_ERROR;
}
// finished using item, release
Py_DECREF(item);
item = NULL;
}
// no error, call check function if any
if (attrdef->m_checkFunction != NULL)
{
if ((*attrdef->m_checkFunction)(ref, attrdef) != 0)
{
// if the checing function didnt set an error then set a generic one here so we dont set an error with no exception
if (PyErr_Occurred()==0)
PyErr_Format(PyExc_AttributeError, "type check error for attribute \"%s\", reasion unknown", attrdef->m_name);
// post check returned an error, restore values
UNDO_AND_ERROR:
if (undoBuffer)
{
memcpy(sourceBuffer, undoBuffer, bufferSize);
free(undoBuffer);
}
if (item)
Py_DECREF(item);
return PY_SET_ATTR_FAIL;
}
}
if (undoBuffer)
free(undoBuffer);
return PY_SET_ATTR_SUCCESS;
}
else // simple attribute value
{
if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
{
if (attrdef->m_setFunction == NULL)
{
PyErr_Format(PyExc_AttributeError, "function attribute without function \"%s\", report to blender.org", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
return (*attrdef->m_setFunction)(ref, attrdef, value);
}
if (attrdef->m_checkFunction != NULL || attrdef->m_type == KX_PYATTRIBUTE_TYPE_VECTOR)
{
// post check function is provided, prepare undo buffer
sourceBuffer = ptr;
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_BOOL:
bufferSize = sizeof(bool);
break;
case KX_PYATTRIBUTE_TYPE_SHORT:
bufferSize = sizeof(short);
break;
case KX_PYATTRIBUTE_TYPE_ENUM:
case KX_PYATTRIBUTE_TYPE_FLAG:
case KX_PYATTRIBUTE_TYPE_CHAR:
bufferSize = attrdef->m_size;
break;
case KX_PYATTRIBUTE_TYPE_INT:
bufferSize = sizeof(int);
break;
case KX_PYATTRIBUTE_TYPE_FLOAT:
bufferSize = sizeof(float);
if (attrdef->m_imax)
bufferSize *= attrdef->m_imax;
if (attrdef->m_imin)
bufferSize *= attrdef->m_imin;
break;
case KX_PYATTRIBUTE_TYPE_STRING:
sourceBuffer = reinterpret_cast<STR_String*>(ptr)->Ptr();
if (sourceBuffer)
bufferSize = strlen(reinterpret_cast<char*>(sourceBuffer))+1;
break;
case KX_PYATTRIBUTE_TYPE_VECTOR:
bufferSize = sizeof(MT_Vector3);
break;
default:
PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
return PY_SET_ATTR_FAIL;
}
if (bufferSize)
{
undoBuffer = malloc(bufferSize);
if (undoBuffer)
{
memcpy(undoBuffer, sourceBuffer, bufferSize);
}
}
}
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_BOOL:
{
bool *var = reinterpret_cast<bool*>(ptr);
if (PyLong_Check(value))
{
*var = (PyLong_AsSsize_t(value) != 0);
}
else if (PyBool_Check(value))
{
*var = (value == Py_True);
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer or a bool for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_FLAG:
{
bool bval;
if (PyLong_Check(value))
{
bval = (PyLong_AsSsize_t(value) != 0);
}
else if (PyBool_Check(value))
{
bval = (value == Py_True);
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer or a bool for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
if (attrdef->m_imax)
bval = !bval;
switch (attrdef->m_size) {
case 1:
{
unsigned char *val = reinterpret_cast<unsigned char*>(ptr);
*val = (*val & ~attrdef->m_imin) | ((bval)?attrdef->m_imin:0);
break;
}
case 2:
{
unsigned short *val = reinterpret_cast<unsigned short*>(ptr);
*val = (*val & ~attrdef->m_imin) | ((bval)?attrdef->m_imin:0);
break;
}
case 4:
{
unsigned int *val = reinterpret_cast<unsigned int*>(ptr);
*val = (*val & ~attrdef->m_imin) | ((bval)?attrdef->m_imin:0);
break;
}
default:
PyErr_Format(PyExc_TypeError, "internal error: unsupported flag field \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *var = reinterpret_cast<short int*>(ptr);
if (PyLong_Check(value))
{
long val = PyLong_AsSsize_t(value);
if (attrdef->m_clamp)
{
if (val < attrdef->m_imin)
val = attrdef->m_imin;
else if (val > attrdef->m_imax)
val = attrdef->m_imax;
}
else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
*var = (short int)val;
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_ENUM:
// enum are equivalent to int, just make sure that the field size matches:
if (sizeof(int) != attrdef->m_size)
{
PyErr_Format(PyExc_AttributeError, "attribute size check error for attribute \"%s\", report to blender.org", attrdef->m_name);
goto FREE_AND_ERROR;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *var = reinterpret_cast<int*>(ptr);
if (PyLong_Check(value))
{
long val = PyLong_AsSsize_t(value);
if (attrdef->m_clamp)
{
if (val < attrdef->m_imin)
val = attrdef->m_imin;
else if (val > attrdef->m_imax)
val = attrdef->m_imax;
}
else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
*var = (int)val;
}
else
{
PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *var = reinterpret_cast<float*>(ptr);
if (attrdef->m_imin != 0)
{
if (attrdef->m_size != attrdef->m_imin*attrdef->m_imax*sizeof(float))
{
PyErr_Format(PyExc_TypeError, "internal error: incorrect field size for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
if (!PySequence_Check(value) || PySequence_Size(value) != attrdef->m_imin)
{
PyErr_Format(PyExc_TypeError, "expected a sequence of [%d][%d] floats for attribute \"%s\"", attrdef->m_imin, attrdef->m_imax, attrdef->m_name);
goto FREE_AND_ERROR;
}
for (int i=0; i<attrdef->m_imin; i++)
{
PyObject *list = PySequence_GetItem(value, i); /* new ref */
if (!PySequence_Check(list) || PySequence_Size(list) != attrdef->m_imax)
{
PyErr_Format(PyExc_TypeError, "expected a sequence of [%d][%d] floats for attribute \"%s\"", attrdef->m_imin, attrdef->m_imax, attrdef->m_name);
goto RESTORE_AND_ERROR;
}
for (int j=0; j<attrdef->m_imax; j++)
{
item = PySequence_GetItem(list, j); /* new ref */
if (!py_check_attr_float(var, item, attrdef))
{
PyErr_Format(PyExc_TypeError, "expected a sequence of [%d][%d] floats for attribute \"%s\"", attrdef->m_imin, attrdef->m_imax, attrdef->m_name);
goto RESTORE_AND_ERROR;
}
Py_DECREF(item);
item = NULL;
++var;
}
Py_DECREF(list);
list = NULL;
}
}
else if (attrdef->m_imax != 0)
{
if (attrdef->m_size != attrdef->m_imax*sizeof(float))
{
PyErr_Format(PyExc_TypeError, "internal error: incorrect field size for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
if (!PySequence_Check(value) || PySequence_Size(value) != attrdef->m_imax)
{
PyErr_Format(PyExc_TypeError, "expected a sequence of [%d] floats for attribute \"%s\"", attrdef->m_imax, attrdef->m_name);
goto FREE_AND_ERROR;
}
for (int i=0; i<attrdef->m_imax; i++)
{
item = PySequence_GetItem(value, i); /* new ref */
if (!py_check_attr_float(var, item, attrdef))
{
goto RESTORE_AND_ERROR;
}
Py_DECREF(item);
item = NULL;
++var;
}
}
else
{
if (!py_check_attr_float(var, value, attrdef))
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_VECTOR:
{
if (!PySequence_Check(value) || PySequence_Size(value) != 3)
{
PyErr_Format(PyExc_TypeError, "expected a sequence of 3 floats for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
MT_Vector3 *var = reinterpret_cast<MT_Vector3*>(ptr);
for (int i=0; i<3; i++)
{
item = PySequence_GetItem(value, i); /* new ref */
double val = PyFloat_AsDouble(item);
Py_DECREF(item);
item = NULL;
if (val == -1.0 && PyErr_Occurred())
{
PyErr_Format(PyExc_TypeError, "expected a sequence of 3 floats for attribute \"%s\"", attrdef->m_name);
goto RESTORE_AND_ERROR;
}
else if (attrdef->m_clamp)
{
if (val < attrdef->m_fmin)
val = attrdef->m_fmin;
else if (val > attrdef->m_fmax)
val = attrdef->m_fmax;
}
else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
{
PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
goto RESTORE_AND_ERROR;
}
(*var)[i] = (MT_Scalar)val;
}
break;
}
case KX_PYATTRIBUTE_TYPE_CHAR:
{
if (PyUnicode_Check(value))
{
Py_ssize_t val_len;
char *val = _PyUnicode_AsStringAndSize(value, &val_len);
strncpy(ptr, val, attrdef->m_size);
ptr[attrdef->m_size-1] = 0;
}
else
{
PyErr_Format(PyExc_TypeError, "expected a string for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_STRING:
{
STR_String *var = reinterpret_cast<STR_String*>(ptr);
if (PyUnicode_Check(value))
{
Py_ssize_t val_len;
char *val = _PyUnicode_AsStringAndSize(value, &val_len);
if (attrdef->m_clamp)
{
if (val_len < attrdef->m_imin)
{
// can't increase the length of the string
PyErr_Format(PyExc_ValueError, "string length too short for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
else if (val_len > attrdef->m_imax)
{
// trim the string
char c = val[attrdef->m_imax];
val[attrdef->m_imax] = 0;
*var = val;
val[attrdef->m_imax] = c;
break;
}
} else if (val_len < attrdef->m_imin || val_len > attrdef->m_imax)
{
PyErr_Format(PyExc_ValueError, "string length out of range for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
*var = val;
}
else
{
PyErr_Format(PyExc_TypeError, "expected a string for attribute \"%s\"", attrdef->m_name);
goto FREE_AND_ERROR;
}
break;
}
default:
// should not happen
PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
goto FREE_AND_ERROR;
}
}
// check if post processing is needed
if (attrdef->m_checkFunction != NULL)
{
if ((*attrdef->m_checkFunction)(ref, attrdef) != 0)
{
// restore value
RESTORE_AND_ERROR:
if (undoBuffer)
{
if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_STRING)
{
// special case for STR_String: restore the string
STR_String *var = reinterpret_cast<STR_String*>(ptr);
*var = reinterpret_cast<char*>(undoBuffer);
}
else
{
// other field type have direct values
memcpy(ptr, undoBuffer, bufferSize);
}
}
FREE_AND_ERROR:
if (undoBuffer)
free(undoBuffer);
if (list)
Py_DECREF(list);
if (item)
Py_DECREF(item);
return 1;
}
}
if (undoBuffer)
free(undoBuffer);
return 0;
}
/*------------------------------
* PyObjectPlus repr -- representations
------------------------------*/
PyObject *PyObjectPlus::py_repr(void)
{
PyErr_SetString(PyExc_SystemError, "Representation not overridden by object.");
return NULL;
}
PyObject *PyObjectPlus::GetProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr)
{
if (self->m_proxy==NULL)
{
self->m_proxy = reinterpret_cast<PyObject *>PyObject_NEW( PyObjectPlus_Proxy, tp);
BGE_PROXY_PYOWNS(self->m_proxy) = false;
BGE_PROXY_PYREF(self->m_proxy) = true;
}
//PyObject_Print(self->m_proxy, stdout, 0);
//printf("ref %d\n", self->m_proxy->ob_refcnt);
BGE_PROXY_REF(self->m_proxy) = self; /* Its possible this was set to NULL, so set it back here */
BGE_PROXY_PTR(self->m_proxy) = ptr;
Py_INCREF(self->m_proxy); /* we own one, thos ones fore the return */
return self->m_proxy;
}
PyObject *PyObjectPlus::NewProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr, bool py_owns)
{
if (!self)
{
// in case of proxy without reference to game object
PyObject* proxy = reinterpret_cast<PyObject *>PyObject_NEW( PyObjectPlus_Proxy, tp);
BGE_PROXY_PYREF(proxy) = false;
BGE_PROXY_PYOWNS(proxy) = py_owns;
BGE_PROXY_REF(proxy) = NULL;
BGE_PROXY_PTR(proxy) = ptr;
return proxy;
}
if (self->m_proxy)
{
if(py_owns)
{ /* Free */
BGE_PROXY_REF(self->m_proxy) = NULL;
Py_DECREF(self->m_proxy);
self->m_proxy= NULL;
}
else {
Py_INCREF(self->m_proxy);
return self->m_proxy;
}
}
GetProxyPlus_Ext(self, tp, ptr);
if(py_owns) {
BGE_PROXY_PYOWNS(self->m_proxy) = py_owns;
Py_DECREF(self->m_proxy); /* could avoid thrashing here but for now its ok */
}
return self->m_proxy;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
/* deprecation warning management */
bool PyObjectPlus::m_ignore_deprecation_warnings(false);
void PyObjectPlus::SetDeprecationWarnings(bool ignoreDeprecationWarnings)
{
m_ignore_deprecation_warnings = ignoreDeprecationWarnings;
}
void PyDebugLine()
{
// import sys; print '\t%s:%d' % (sys._getframe(0).f_code.co_filename, sys._getframe(0).f_lineno)
PyObject *getframe, *frame;
PyObject *f_lineno, *f_code, *co_filename;
getframe = PySys_GetObject((char *)"_getframe"); // borrowed
if (getframe) {
frame = PyObject_CallObject(getframe, NULL);
if (frame) {
f_lineno= PyObject_GetAttrString(frame, "f_lineno");
f_code= PyObject_GetAttrString(frame, "f_code");
if (f_lineno && f_code) {
co_filename= PyObject_GetAttrString(f_code, "co_filename");
if (co_filename) {
printf("\t%s:%d\n", _PyUnicode_AsString(co_filename), (int)PyLong_AsSsize_t(f_lineno));
Py_DECREF(f_lineno);
Py_DECREF(f_code);
Py_DECREF(co_filename);
Py_DECREF(frame);
return;
}
}
Py_XDECREF(f_lineno);
Py_XDECREF(f_code);
Py_DECREF(frame);
}
}
PyErr_Clear();
printf("\tERROR - Could not access sys._getframe(0).f_lineno or sys._getframe().f_code.co_filename\n");
}
void PyObjectPlus::ShowDeprecationWarning_func(const char* old_way,const char* new_way)
{
printf("Method %s is deprecated, please use %s instead.\n", old_way, new_way);
PyDebugLine();
}
void PyObjectPlus::ClearDeprecationWarning()
{
WarnLink *wlink_next;
WarnLink *wlink = GetDeprecationWarningLinkFirst();
while(wlink)
{
wlink->warn_done= false; /* no need to NULL the link, its cleared before adding to the list next time round */
wlink_next= reinterpret_cast<WarnLink *>(wlink->link);
wlink->link= NULL;
wlink= wlink_next;
}
NullDeprecationWarning();
}
WarnLink* m_base_wlink_first= NULL;
WarnLink* m_base_wlink_last= NULL;
WarnLink* PyObjectPlus::GetDeprecationWarningLinkFirst(void) {return m_base_wlink_first;}
WarnLink* PyObjectPlus::GetDeprecationWarningLinkLast(void) {return m_base_wlink_last;}
void PyObjectPlus::SetDeprecationWarningFirst(WarnLink* wlink) {m_base_wlink_first= wlink;}
void PyObjectPlus::SetDeprecationWarningLinkLast(WarnLink* wlink) {m_base_wlink_last= wlink;}
void PyObjectPlus::NullDeprecationWarning() {m_base_wlink_first= m_base_wlink_last= NULL;}
#endif // DISABLE_PYTHON
|