BGE API cleanup: more consistent type check on set attribute (mouse and keyboard...
authorBenoit Bolsee <benoit.bolsee@online.be>
Tue, 30 Dec 2008 16:44:34 +0000 (16:44 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Tue, 30 Dec 2008 16:44:34 +0000 (16:44 +0000)
source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
source/gameengine/GameLogic/SCA_MouseSensor.cpp

index f880cd7768e07d6ff83979245372af4e5488f3fc..0e014628ac7ac7afb352a571314567679ea8e1d7 100644 (file)
@@ -846,49 +846,64 @@ SCA_KeyboardSensor::_getattr(const STR_String& attr)
 
 int SCA_KeyboardSensor::_setattr(const STR_String& attr, PyObject *value)
 {
-       if (PyInt_Check(value))
+       if (attr == "key")
        {
-               int val = PyInt_AsLong(value);
-
-               if (attr == "key")
-               {
-                       m_hotkey = val;
-                       return 0;
+               if (!PyInt_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                       return 1;                       
                }
+               m_hotkey = PyInt_AsLong(value);
+               return 0;
+       }
 
-               if (attr == "hold1")
-               {
-                       m_qual = val;
-                       return 0;
+       if (attr == "hold1")
+       {
+               if (!PyInt_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                       return 1;                       
                }
-               
-               if (attr == "hold2")
-               {
-                       m_qual2 = val;
-                       return 0;
+               m_qual = PyInt_AsLong(value);
+               return 0;
+       }
+       
+       if (attr == "hold2")
+       {
+               if (!PyInt_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                       return 1;                       
                }
+               m_qual2 = PyInt_AsLong(value);
+               return 0;
+       }
 
-               if (attr == "useAllKeys")
-               {
-                       m_bAllKeys = (val != 0);
-                       return 0;
+       if (attr == "useAllKeys")
+       {
+               if (!PyInt_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                       return 1;                       
                }
+               m_bAllKeys = (PyInt_AsLong(value) != 0);
+               return 0;
        }
-
-       if (PyString_Check(value))
+       
+       if (attr == "logToggleProperty")
        {
-               STR_String val = PyString_AsString(value);
-               if (attr == "logToggleProperty")
-               {
-                       m_toggleprop = val;
-                       return 0;
+               if (!PyString_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected a string");
+                       return 1;                       
                }
+               m_toggleprop = PyString_AsString(value);
+               return 0;
+       }
 
-               if (attr == "logTargetProperty")
-               {
-                       m_targetprop = val;
-                       return 0;
+       if (attr == "logTargetProperty")
+       {
+               if (!PyString_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected a string");
+                       return 1;                       
                }
+               m_targetprop = PyString_AsString(value);
+               return 0;
        }
 
        return SCA_ISensor::_setattr(attr, value);
index 9562003aec0e7faf13b5ee7acd9c53f94ee7bc05..8281eed47bcb3b2285443ab03997a181d2900040 100644 (file)
@@ -343,31 +343,30 @@ PyObject* SCA_MouseSensor::_getattr(const STR_String& attr) {
 
 int SCA_MouseSensor::_setattr(const STR_String& attr, PyObject *value)
 {
-       if (PyInt_Check(value))
+       if (attr == "mode")
        {
+               if (!PyInt_Check(value)){
+                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                       return 1;                       
+               }
+               
                int val = PyInt_AsLong(value);
-
-               if (attr == "mode")
-               {
-                       if ((val < KX_MOUSESENSORMODE_NODEF) 
-                               || (val > KX_MOUSESENSORMODE_MAX)){     
-                               
-                               PyErr_SetString(PyExc_ValueError, "invalid mode specified!");
-                               return NULL;
-                       }
-                       
-                       m_mousemode = val;
-                       UpdateHotkey();
-                       return 0;
+               
+               if ((val < KX_MOUSESENSORMODE_NODEF) 
+                       || (val > KX_MOUSESENSORMODE_MAX)){             
+                       PyErr_SetString(PyExc_ValueError, "invalid mode specified!");
+                       return 1;
                }
+               
+               m_mousemode = val;
+               UpdateHotkey();
+               return 0;
        }
-       else 
+       
+       if (attr == "position")
        {
-               if (attr == "position")
-               {
-                       PyErr_SetString(PyExc_AttributeError, "read-only property!");
-                       return NULL;            
-               }
+               PyErr_SetString(PyExc_AttributeError, "'position' is a read-only property!");
+               return 1;               
        }
        
        return SCA_ISensor::_setattr(attr, value);