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