From 959b1500caaa7b11c175c644398652c35ad0af5e Mon Sep 17 00:00:00 2001 From: Martin Renold Date: Sun, 24 Jul 2011 11:36:05 +0200 Subject: [PATCH] fix brushlib Python exception handling Return NULL from the original python call (stroke_to) if an exception has occurred while trying to get the tile memory. Also, move some Python specific code away from brushlib. --- brushlib/brush.hpp | 40 ++++++++++++++---------------------- lib/brush.py | 10 +++++++-- lib/mypaintlib.hpp | 1 + lib/mypaintlib.i | 1 + lib/python_brush.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/tiledsurface.hpp | 3 ++- 6 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 lib/python_brush.hpp diff --git a/brushlib/brush.hpp b/brushlib/brush.hpp index 2056fb5..d10c397 100644 --- a/brushlib/brush.hpp +++ b/brushlib/brush.hpp @@ -104,22 +104,34 @@ public: } void set_base_value (int id, float value) { - g_assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); + assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); settings[id]->base_value = value; settings_base_values_have_changed (); } void set_mapping_n (int id, int input, int n) { - g_assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); + assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); settings[id]->set_n (input, n); } void set_mapping_point (int id, int input, int index, float x, float y) { - g_assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); + assert (id >= 0 && id < BRUSH_SETTINGS_COUNT); settings[id]->set_point (input, index, x, y); } + float get_state (int i) + { + assert (i >= 0 && i < STATE_COUNT); + return states[i]; + } + + void set_state (int i, float value) + { + assert (i >= 0 && i < STATE_COUNT); + states[i] = value; + } + private: // returns the fraction still left after t seconds float exp_decay (float T_const, float t) @@ -767,26 +779,4 @@ public: return false; } - PyObject * get_state () - { - npy_intp dims = {STATE_COUNT}; - PyObject * data = PyArray_SimpleNew(1, &dims, NPY_FLOAT32); - npy_float32 * data_p = (npy_float32*)PyArray_DATA(data); - for (int i=0; i + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +class PythonBrush : public Brush { + +public: + // get state as numpy array + PyObject * python_get_state () + { + npy_intp dims = {STATE_COUNT}; + PyObject * data = PyArray_SimpleNew(1, &dims, NPY_FLOAT32); + npy_float32 * data_p = (npy_float32*)PyArray_DATA(data); + for (int i=0; i