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
|
/**
* $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
#ifndef NO_EXP_PYTHON_EMBEDDING
/*------------------------------
* 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"
/*------------------------------
* PyObjectPlus Type -- Every class, even the abstract one should have a Type
------------------------------*/
PyTypeObject PyObjectPlus::Type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"PyObjectPlus", /*tp_name*/
sizeof(PyObjectPlus), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
PyDestructor,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,
py_base_getattro,
py_base_setattro,
0,0,0,0,0,0,0,0,0,
Methods
};
PyObjectPlus::~PyObjectPlus()
{
if (ob_refcnt)
{
_Py_ForgetReference(this);
}
// assert(ob_refcnt==0);
}
PyObjectPlus::PyObjectPlus(PyTypeObject *T) // constructor
{
MT_assert(T != NULL);
this->ob_type = T;
_Py_NewReference(this);
};
/*------------------------------
* PyObjectPlus Methods -- Every class, even the abstract one should have a Methods
------------------------------*/
PyMethodDef PyObjectPlus::Methods[] = {
{"isA", (PyCFunction) sPy_isA, METH_O},
{NULL, NULL} /* Sentinel */
};
PyAttributeDef PyObjectPlus::Attributes[] = {
{NULL} //Sentinel
};
/*------------------------------
* PyObjectPlus Parents -- Every class, even the abstract one should have parents
------------------------------*/
PyParentObject PyObjectPlus::Parents[] = {&PyObjectPlus::Type, NULL};
/*------------------------------
* PyObjectPlus attributes -- attributes
------------------------------*/
PyObject *PyObjectPlus::py_getattro(PyObject* attr)
{
PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \
if (descr == NULL) {
if (strcmp(PyString_AsString(attr), "__dict__")==0) {
return py_getattr_dict(NULL, Type.tp_dict); /* no Attributes yet */
}
PyErr_SetString(PyExc_AttributeError, "attribute not found");
return NULL;
} else {
return PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, (PyObject *)this); \
}
//if (streq(attr, "type"))
// return Py_BuildValue("s", (*(GetParents()))->tp_name);
}
int PyObjectPlus::py_delattro(PyObject* attr)
{
PyErr_SetString(PyExc_AttributeError, "attribute cant be deleted");
return 1;
}
int PyObjectPlus::py_setattro(PyObject *attr, PyObject* value)
{
//return PyObject::py_setattro(attr,value);
//cerr << "Unknown attribute" << endl;
PyErr_SetString(PyExc_AttributeError, "attribute cant be set");
return 1;
}
PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef)
{
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)(self, attrdef);
}
char *ptr = reinterpret_cast<char*>(self)+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_SetItem(resultlist,i,PyInt_FromLong(*val));
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *val = reinterpret_cast<short int*>(ptr);
ptr += sizeof(short int);
PyList_SetItem(resultlist,i,PyInt_FromLong(*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_SetItem(resultlist,i,PyInt_FromLong(*val));
break;
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *val = reinterpret_cast<float*>(ptr);
ptr += sizeof(float);
PyList_SetItem(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_BOOL:
{
bool *val = reinterpret_cast<bool*>(ptr);
return PyInt_FromLong(*val);
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *val = reinterpret_cast<short int*>(ptr);
return PyInt_FromLong(*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 PyInt_FromLong(*val);
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *val = reinterpret_cast<float*>(ptr);
return PyFloat_FromDouble(*val);
}
case KX_PYATTRIBUTE_TYPE_STRING:
{
STR_String *val = reinterpret_cast<STR_String*>(ptr);
return PyString_FromString(*val);
}
default:
return NULL;
}
}
}
#if 0
PyObject *PyObjectPlus::py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr)
{
char *attr_str= PyString_AsString(attr);
const PyAttributeDef *attrdef;
for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
if (!strcmp(attr_str, attrdef->m_name))
return py_get_attrdef(self, attrdef);
return NULL;
}
#endif
int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value)
{
void *undoBuffer = NULL;
void *sourceBuffer = NULL;
size_t bufferSize = 0;
char *ptr = reinterpret_cast<char*>(self)+attrdef->m_offset;
if (attrdef->m_length > 1)
{
if (!PySequence_Check(value))
{
PyErr_SetString(PyExc_TypeError, "expected a sequence");
return 1;
}
if (PySequence_Size(value) != attrdef->m_length)
{
PyErr_SetString(PyExc_TypeError, "incorrect number of elements in sequence");
return 1;
}
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_FUNCTION:
if (attrdef->m_setFunction == NULL)
{
PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
return 1;
}
return (*attrdef->m_setFunction)(self, 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_SetString(PyExc_AttributeError, "Unsupported attribute type, report to blender.org");
return 1;
}
// 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++)
{
PyObject *item = PySequence_GetItem(value, i); /* new ref */
// we can decrement the reference immediately, the reference count
// is at least 1 because the item is part of an array
Py_DECREF(item);
switch (attrdef->m_type)
{
case KX_PYATTRIBUTE_TYPE_BOOL:
{
bool *var = reinterpret_cast<bool*>(ptr);
ptr += sizeof(bool);
if (PyInt_Check(item))
{
*var = (PyInt_AsLong(item) != 0);
}
else if (PyBool_Check(item))
{
*var = (item == Py_True);
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
goto UNDO_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *var = reinterpret_cast<short int*>(ptr);
ptr += sizeof(short int);
if (PyInt_Check(item))
{
long val = PyInt_AsLong(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_SetString(PyExc_ValueError, "item value out of range");
goto UNDO_AND_ERROR;
}
*var = (short int)val;
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer");
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_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
goto UNDO_AND_ERROR;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *var = reinterpret_cast<int*>(ptr);
ptr += sizeof(int);
if (PyInt_Check(item))
{
long val = PyInt_AsLong(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_SetString(PyExc_ValueError, "item value out of range");
goto UNDO_AND_ERROR;
}
*var = (int)val;
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer");
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_SetString(PyExc_TypeError, "expected a float");
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_SetString(PyExc_ValueError, "item value out of range");
goto UNDO_AND_ERROR;
}
*var = (float)val;
break;
}
default:
// should not happen
PyErr_SetString(PyExc_AttributeError, "attribute type check error, report to blender.org");
goto UNDO_AND_ERROR;
}
}
// no error, call check function if any
if (attrdef->m_checkFunction != NULL)
{
if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
{
// post check returned an error, restore values
UNDO_AND_ERROR:
if (undoBuffer)
{
memcpy(sourceBuffer, undoBuffer, bufferSize);
free(undoBuffer);
}
return 1;
}
}
if (undoBuffer)
free(undoBuffer);
return 0;
}
else // simple attribute value
{
if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
{
if (attrdef->m_setFunction == NULL)
{
PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
return 1;
}
return (*attrdef->m_setFunction)(self, attrdef, value);
}
if (attrdef->m_checkFunction != NULL)
{
// 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_INT:
bufferSize = sizeof(int);
break;
case KX_PYATTRIBUTE_TYPE_FLOAT:
bufferSize = sizeof(float);
break;
case KX_PYATTRIBUTE_TYPE_STRING:
sourceBuffer = reinterpret_cast<STR_String*>(ptr)->Ptr();
if (sourceBuffer)
bufferSize = strlen(reinterpret_cast<char*>(sourceBuffer))+1;
break;
default:
PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
return 1;
}
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 (PyInt_Check(value))
{
*var = (PyInt_AsLong(value) != 0);
}
else if (PyBool_Check(value))
{
*var = (value == Py_True);
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_SHORT:
{
short int *var = reinterpret_cast<short int*>(ptr);
if (PyInt_Check(value))
{
long val = PyInt_AsLong(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_SetString(PyExc_ValueError, "value out of range");
goto FREE_AND_ERROR;
}
*var = (short int)val;
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer");
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_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
goto FREE_AND_ERROR;
}
// walkthrough
case KX_PYATTRIBUTE_TYPE_INT:
{
int *var = reinterpret_cast<int*>(ptr);
if (PyInt_Check(value))
{
long val = PyInt_AsLong(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_SetString(PyExc_ValueError, "value out of range");
goto FREE_AND_ERROR;
}
*var = (int)val;
}
else
{
PyErr_SetString(PyExc_TypeError, "expected an integer");
goto FREE_AND_ERROR;
}
break;
}
case KX_PYATTRIBUTE_TYPE_FLOAT:
{
float *var = reinterpret_cast<float*>(ptr);
double val = PyFloat_AsDouble(value);
if (val == -1.0 && PyErr_Occurred())
{
PyErr_SetString(PyExc_TypeError, "expected a float");
goto FREE_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_SetString(PyExc_ValueError, "value out of range");
goto FREE_AND_ERROR;
}
*var = (float)val;
break;
}
case KX_PYATTRIBUTE_TYPE_STRING:
{
STR_String *var = reinterpret_cast<STR_String*>(ptr);
if (PyString_Check(value))
{
char *val = PyString_AsString(value);
if (attrdef->m_clamp)
{
if (strlen(val) < attrdef->m_imin)
{
// can't increase the length of the string
PyErr_SetString(PyExc_ValueError, "string length too short");
goto FREE_AND_ERROR;
}
else if (strlen(val) > 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 (strlen(val) < attrdef->m_imin || strlen(val) > attrdef->m_imax)
{
PyErr_SetString(PyExc_ValueError, "string length out of range");
goto FREE_AND_ERROR;
}
*var = val;
}
else
{
PyErr_SetString(PyExc_TypeError, "expected a string");
goto FREE_AND_ERROR;
}
break;
}
default:
// should not happen
PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
goto FREE_AND_ERROR;
}
}
// check if post processing is needed
if (attrdef->m_checkFunction != NULL)
{
if ((*attrdef->m_checkFunction)(self, 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);
return 1;
}
}
if (undoBuffer)
free(undoBuffer);
return 0;
}
#if 0
int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value)
{
const PyAttributeDef *attrdef;
char *attr_str= PyString_AsString(attr);
for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
{
if (!strcmp(attr_str, attrdef->m_name))
{
if (attrdef->m_access == KX_PYATTRIBUTE_RO ||
attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
{
PyErr_SetString(PyExc_AttributeError, "property is read-only");
return 1;
}
return py_set_attrdef(self, attrdef, value);
}
}
return -1;
}
#endif
/*------------------------------
* PyObjectPlus repr -- representations
------------------------------*/
PyObject *PyObjectPlus::py_repr(void)
{
PyErr_SetString(PyExc_SystemError, "Representation not overridden by object.");
return NULL;
}
/*------------------------------
* PyObjectPlus isA -- the isA functions
------------------------------*/
bool PyObjectPlus::isA(PyTypeObject *T) // if called with a Type, use "typename"
{
int i;
PyParentObject P;
PyParentObject *Ps = GetParents();
for (P = Ps[i=0]; P != NULL; P = Ps[i++])
if (P==T)
return true;
return false;
}
bool PyObjectPlus::isA(const char *mytypename) // check typename of each parent
{
int i;
PyParentObject P;
PyParentObject *Ps = GetParents();
for (P = Ps[i=0]; P != NULL; P = Ps[i++])
if (strcmp(P->tp_name, mytypename)==0)
return true;
return false;
}
PyObject *PyObjectPlus::Py_isA(PyObject *value) // Python wrapper for isA
{
if (PyType_Check(value)) {
return PyBool_FromLong(isA((PyTypeObject *)value));
} else if (PyString_Check(value)) {
return PyBool_FromLong(isA(PyString_AsString(value)));
}
PyErr_SetString(PyExc_TypeError, "expected a type or a string");
return NULL;
}
/* Utility function called by the macro py_getattro_up()
* for getting ob.__dict__() values from our PyObject
* this is used by python for doing dir() on an object, so its good
* if we return a list of attributes and methods.
*
* Other then making dir() useful the value returned from __dict__() is not useful
* since every value is a Py_None
* */
PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict)
{
if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
PyErr_Clear();
pydict = PyDict_New();
}
PyDict_Update(pydict, tp_dict);
return pydict;
}
#endif //NO_EXP_PYTHON_EMBEDDING
|