diff options
author | Tamito Kajiyama <rd6t-kjym@asahi-net.or.jp> | 2013-03-03 05:53:49 +0400 |
---|---|---|
committer | Tamito Kajiyama <rd6t-kjym@asahi-net.or.jp> | 2013-03-03 05:53:49 +0400 |
commit | 0c5dfc8a639b04e1767852121b9dee7cd5bb050f (patch) | |
tree | bcdceea173ef425daa08ede0ca15f0bebc414cf0 /source/blender/freestyle | |
parent | 39b9d925ee1724dc0e16351b112c0c426a3f1921 (diff) |
Fix for exceptions in converting Python float objects to C variables not properly handled.
Based on review comment from Campbell.
Diffstat (limited to 'source/blender/freestyle')
-rw-r--r-- | source/blender/freestyle/intern/python/BPy_Convert.cpp | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/source/blender/freestyle/intern/python/BPy_Convert.cpp b/source/blender/freestyle/intern/python/BPy_Convert.cpp index e1b4aca270b..4c8e6557d02 100644 --- a/source/blender/freestyle/intern/python/BPy_Convert.cpp +++ b/source/blender/freestyle/intern/python/BPy_Convert.cpp @@ -608,62 +608,94 @@ Vec3r * Vec3r_ptr_from_Color(PyObject* obj) return new Vec3r(r,g,b); } +static int float_array_from_PyList(PyObject *obj, float *v, int n) +{ + for (int i = 0; i < n; i++) { + v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i)); + if (v[i] == -1.0f && PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "list elements must be a number"); + return 0; + } + } + return 1; +} + Vec2f * Vec2f_ptr_from_PyList(PyObject* obj) { + float v[2]; + if (!PyList_Check(obj) || PyList_Size(obj) != 2) return NULL; - float x = PyFloat_AsDouble(PyList_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyList_GetItem(obj, 1)); - return new Vec2f(x,y); + if (!float_array_from_PyList(obj, v, 2)) + return NULL; + return new Vec2f(v[0], v[1]); } Vec3f * Vec3f_ptr_from_PyList(PyObject* obj) { + float v[3]; + if (!PyList_Check(obj) || PyList_Size(obj) != 3) return NULL; - float x = PyFloat_AsDouble(PyList_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyList_GetItem(obj, 1)); - float z = PyFloat_AsDouble(PyList_GetItem(obj, 2)); - return new Vec3f(x,y,z); + if (!float_array_from_PyList(obj, v, 3)) + return NULL; + return new Vec3f(v[0], v[1], v[2]); } Vec3r * Vec3r_ptr_from_PyList(PyObject* obj) { + float v[3]; + if (!PyList_Check(obj) || PyList_Size(obj) != 3) return NULL; - float x = PyFloat_AsDouble(PyList_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyList_GetItem(obj, 1)); - float z = PyFloat_AsDouble(PyList_GetItem(obj, 2)); - return new Vec3r(x,y,z); + if (!float_array_from_PyList(obj, v, 3)) + return NULL; + return new Vec3r(v[0], v[1], v[2]); +} + +static int float_array_from_PyTuple(PyObject *obj, float *v, int n) +{ + for (int i = 0; i < n; i++) { + v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i)); + if (v[i] == -1.0f && PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "tuple elements must be a number"); + return 0; + } + } + return 1; } Vec2f * Vec2f_ptr_from_PyTuple(PyObject* obj) { + float v[2]; + if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) return NULL; - float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1)); - return new Vec2f(x,y); + if (!float_array_from_PyTuple(obj, v, 2)) + return NULL; + return new Vec2f(v[0], v[1]); } Vec3f * Vec3f_ptr_from_PyTuple(PyObject* obj) { + float v[3]; + if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3) return NULL; - float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1)); - float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2)); - return new Vec3f(x,y,z); + if (!float_array_from_PyTuple(obj, v, 3)) + return NULL; + return new Vec3f(v[0], v[1], v[2]); } Vec3r * Vec3r_ptr_from_PyTuple(PyObject* obj) { + float v[3]; + if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3) return NULL; - float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0)); - float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1)); - float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2)); - return new Vec3r(x,y,z); + if (!float_array_from_PyTuple(obj, v, 3)) + return NULL; + return new Vec3r(v[0], v[1], v[2]); } // helper for argument parsing @@ -673,19 +705,15 @@ int float_array_from_PyObject(PyObject *obj, float *v, int n) if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == n) { for (int i = 0; i < n; i++) v[i] = ((VectorObject *)obj)->vec[i]; + return 1; } else if (PyList_Check(obj) && PyList_Size(obj) == n) { - for (int i = 0; i < n; i++) - v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i)); + return float_array_from_PyList(obj, v, n); } else if (PyTuple_Check(obj) && PyTuple_Size(obj) == n) { - for (int i = 0; i < n; i++) - v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i)); - } - else { - return 0; + return float_array_from_PyList(obj, v, n); } - return 1; + return 0; } /////////////////////////////////////////////////////////////////////////////////////////// |