forked from bartvdbraak/blender
bge py api: raise an overflow exception when assigning a float to a bge object which is out of the float range.
also avoid raising exceptions by ConvertPythonToValue when they will be ignored.
This commit is contained in:
parent
d92bd6bb04
commit
854fd94016
@ -425,7 +425,7 @@ static PyObject *listvalue_buffer_concat(PyObject *self, PyObject *other)
|
||||
|
||||
for (i=0;i<numitems;i++)
|
||||
{
|
||||
listitemval = listval->ConvertPythonToValue(PyList_GetItem(other,i), "cList + pyList: CListValue, ");
|
||||
listitemval = listval->ConvertPythonToValue(PyList_GetItem(other,i), true, "cList + pyList: CListValue, ");
|
||||
|
||||
if (listitemval) {
|
||||
listval_new->SetValue(i+numitems_orig, listitemval);
|
||||
@ -570,7 +570,7 @@ PyAttributeDef CListValue::Attributes[] = {
|
||||
|
||||
PyObject *CListValue::Pyappend(PyObject *value)
|
||||
{
|
||||
CValue* objval = ConvertPythonToValue(value, "CList.append(i): CValueList, ");
|
||||
CValue *objval = ConvertPythonToValue(value, true, "CList.append(i): CValueList, ");
|
||||
|
||||
if (!objval) /* ConvertPythonToValue sets the error */
|
||||
return NULL;
|
||||
@ -595,7 +595,7 @@ PyObject *CListValue::Pyindex(PyObject *value)
|
||||
{
|
||||
PyObject *result = NULL;
|
||||
|
||||
CValue* checkobj = ConvertPythonToValue(value, "val = cList[i]: CValueList, ");
|
||||
CValue *checkobj = ConvertPythonToValue(value, true, "val = cList[i]: CValueList, ");
|
||||
if (checkobj==NULL)
|
||||
return NULL; /* ConvertPythonToValue sets the error */
|
||||
|
||||
@ -624,7 +624,7 @@ PyObject *CListValue::Pycount(PyObject *value)
|
||||
{
|
||||
int numfound = 0;
|
||||
|
||||
CValue* checkobj = ConvertPythonToValue(value, ""); /* error ignored */
|
||||
CValue *checkobj = ConvertPythonToValue(value, false, ""); /* error ignored */
|
||||
|
||||
if (checkobj==NULL) { /* in this case just return that there are no items in the list */
|
||||
PyErr_Clear();
|
||||
|
@ -537,10 +537,17 @@ PyObject *CValue::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrde
|
||||
return PyUnicode_From_STR_String(self->GetName());
|
||||
}
|
||||
|
||||
CValue* CValue::ConvertPythonToValue(PyObject *pyobj, const char *error_prefix)
|
||||
/**
|
||||
* There are 2 reasons this could return NULL
|
||||
* - unsupported type.
|
||||
* - error converting (overflow).
|
||||
*
|
||||
* \param do_type_exception Use to skip raising an exception for unknown types.
|
||||
*/
|
||||
CValue *CValue::ConvertPythonToValue(PyObject *pyobj, const bool do_type_exception, const char *error_prefix)
|
||||
{
|
||||
|
||||
CValue* vallie = NULL;
|
||||
CValue *vallie;
|
||||
/* refcounting is broking here! - this crashes anyway, just store a python list for KX_GameObject */
|
||||
#if 0
|
||||
if (PyList_Check(pyobj))
|
||||
@ -581,7 +588,14 @@ CValue* CValue::ConvertPythonToValue(PyObject *pyobj, const char *error_prefix)
|
||||
} else
|
||||
if (PyFloat_Check(pyobj))
|
||||
{
|
||||
vallie = new CFloatValue( (float)PyFloat_AsDouble(pyobj) );
|
||||
const double tval = PyFloat_AsDouble(pyobj);
|
||||
if (tval > (double)FLT_MAX || tval < (double)-FLT_MAX) {
|
||||
PyErr_Format(PyExc_OverflowError, "%soverflow converting from float, out of internal range", error_prefix);
|
||||
vallie = NULL;
|
||||
}
|
||||
else {
|
||||
vallie = new CFloatValue((float)tval);
|
||||
}
|
||||
} else
|
||||
if (PyLong_Check(pyobj))
|
||||
{
|
||||
@ -594,10 +608,13 @@ CValue* CValue::ConvertPythonToValue(PyObject *pyobj, const char *error_prefix)
|
||||
if (PyObject_TypeCheck(pyobj, &CValue::Type)) /* Note, don't let these get assigned to GameObject props, must check elsewhere */
|
||||
{
|
||||
vallie = (static_cast<CValue *>(BGE_PROXY_REF(pyobj)))->AddRef();
|
||||
} else
|
||||
{
|
||||
/* return an error value from the caller */
|
||||
PyErr_Format(PyExc_TypeError, "%scould convert python value to a game engine property", error_prefix);
|
||||
}
|
||||
else {
|
||||
if (do_type_exception) {
|
||||
/* return an error value from the caller */
|
||||
PyErr_Format(PyExc_TypeError, "%scould convert python value to a game engine property", error_prefix);
|
||||
}
|
||||
vallie = NULL;
|
||||
}
|
||||
return vallie;
|
||||
|
||||
|
@ -220,7 +220,7 @@ public:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
virtual CValue *ConvertPythonToValue(PyObject *pyobj, const char *error_prefix);
|
||||
virtual CValue *ConvertPythonToValue(PyObject *pyobj, const bool do_type_exception, const char *error_prefix);
|
||||
|
||||
static PyObject *pyattr_get_name(void *self, const KX_PYATTRIBUTE_DEF *attrdef);
|
||||
|
||||
|
@ -1858,15 +1858,14 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
|
||||
}
|
||||
}
|
||||
else { /* ob["key"] = value */
|
||||
int set= 0;
|
||||
bool set = false;
|
||||
|
||||
/* as CValue */
|
||||
if (attr_str && PyObject_TypeCheck(val, &PyObjectPlus::Type)==0) /* don't allow GameObjects for eg to be assigned to CValue props */
|
||||
{
|
||||
CValue* vallie = self->ConvertPythonToValue(val, ""); /* error unused */
|
||||
CValue *vallie = self->ConvertPythonToValue(val, false, "gameOb[key] = value: ");
|
||||
|
||||
if (vallie)
|
||||
{
|
||||
if (vallie) {
|
||||
CValue* oldprop = self->GetProperty(attr_str);
|
||||
|
||||
if (oldprop)
|
||||
@ -1875,7 +1874,7 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
|
||||
self->SetProperty(attr_str, vallie);
|
||||
|
||||
vallie->Release();
|
||||
set= 1;
|
||||
set = true;
|
||||
|
||||
/* try remove dict value to avoid double ups */
|
||||
if (self->m_attr_dict) {
|
||||
@ -1883,13 +1882,12 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_Clear();
|
||||
else if (PyErr_Occurred()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (set==0)
|
||||
{
|
||||
if (set == false) {
|
||||
if (self->m_attr_dict==NULL) /* lazy init */
|
||||
self->m_attr_dict= PyDict_New();
|
||||
|
||||
@ -1898,7 +1896,7 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
|
||||
{
|
||||
if (attr_str)
|
||||
self->RemoveProperty(attr_str); /* overwrite the CValue if it exists */
|
||||
set= 1;
|
||||
set = true;
|
||||
}
|
||||
else {
|
||||
if (attr_str) PyErr_Format(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key \"%s\" not be added to internal dictionary", attr_str);
|
||||
@ -1906,8 +1904,9 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
|
||||
}
|
||||
}
|
||||
|
||||
if (set==0)
|
||||
if (set == false) {
|
||||
return -1; /* pythons error value */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user