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