From 6f3d3f62de96ef32efd1d606797a3e92a4a67afb Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Wed, 11 Mar 2020 10:36:38 -0400 Subject: [PATCH] [python/matplotlib] Validate the plot parameters --- python/port/mod/matplotlib/modpyplot.cpp | 16 +++++++++++++--- python/port/mod/matplotlib/plot_store.cpp | 3 --- python/port/mod/matplotlib/plot_store.h | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 482afe24d..2f9e91430 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -35,7 +35,8 @@ void modpyplot_gc_collect() { * Returns : xmin, xmax, ymin, ymax : float */ mp_obj_t modpyplot_axis(mp_obj_t arg) { - mp_obj_is_type(arg, &mp_type_enumerate); + assert(sPlotStore != nullptr); + #warning Use mp_obj_is_bool when upgrading uPy if (mp_obj_is_type(arg, &mp_type_bool)) { sPlotStore->setGrid(mp_obj_is_true(arg)); @@ -59,9 +60,18 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) { mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) { assert(sPlotStore != nullptr); + + // Input parameter validation + size_t xLength, yLength; + mp_obj_t * xItems, yItems; + mp_obj_get_array(x, &xLength, &xItems); + mp_obj_get_array(y, &yLength, &yItems); + if (xLength != yLength) { + mp_raise_msg_varg(&mp_type_ValueError, "x and y must have same dimension"); + } + sPlotStore->addDots(x, y); - // Ensure x and y are arrays - // "Push" x and y on bigger arrays + return mp_const_none; } diff --git a/python/port/mod/matplotlib/plot_store.cpp b/python/port/mod/matplotlib/plot_store.cpp index 681077f44..075174d20 100644 --- a/python/port/mod/matplotlib/plot_store.cpp +++ b/python/port/mod/matplotlib/plot_store.cpp @@ -16,9 +16,6 @@ void PlotStore::addDots(mp_obj_t x, mp_obj_t y) { mp_obj_t items[2] = {x, y}; mp_obj_t tuple = mp_obj_new_tuple(2, items); mp_obj_list_append(m_dots, tuple); - - //mp_obj_tuple_t * t = static_cast(MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL))); - //t->items[0] = MP_OBJ_NEW_SMALL_INT(r); } PlotStore::Dot PlotStore::dotAtIndex(int i) { diff --git a/python/port/mod/matplotlib/plot_store.h b/python/port/mod/matplotlib/plot_store.h index 1cca7b3b9..5b76c53a4 100644 --- a/python/port/mod/matplotlib/plot_store.h +++ b/python/port/mod/matplotlib/plot_store.h @@ -35,7 +35,7 @@ public: void setGrid(bool grid) { m_grid = grid; } bool grid() { return m_grid; } private: - mp_obj_t m_dots; + mp_obj_t m_dots; // A list of (x,y), where x and y are lists of numbers bool m_grid; /*