8000 check functions with narrower error message scope · OhHandsome/matplotlib-cpp@019cd23 · GitHub
[go: up one dir, main page]

Skip to content

Commit 019cd23

Browse files
tkphdlava
authored andcommitted
check functions with narrower error message scope
1 parent 4ace1ad commit 019cd23

File tree

1 file changed

+51
-114
lines changed

1 file changed

+51
-114
lines changed

matplotlibcpp.h

Lines changed: 51 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ struct _interpreter {
8484
return ctx;
8585
}
8686

87+
PyObject* safe_import(PyObject* module, std::string fname) {
88+
PyObject* fn = PyObject_GetAttrString(module, fname.c_str());
89+
90+
if (!fn)
91+
throw std::runtime_error(std::string("Couldn't find required function: ") + fname);
92+
93+
if (!PyFunction_Check(fn))
94+
throw std::runtime_error(fname + std::string(" is unexpectedly not a PyFunction."));
95+
96+
return fn;
97+
}
98+
8799
private:
88100

89101
#ifndef WITHOUT_NUMPY
@@ -151,120 +163,45 @@ struct _interpreter {
151163
Py_DECREF(pylabname);
152164
if (!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
153165

154-
s_python_function_show = PyObject_GetAttrString(pymod, "show");
155-
s_python_function_close = PyObject_GetAttrString(pymod, "close");
156-
s_python_function_draw = PyObject_GetAttrString(pymod, "draw");
157-
s_python_function_pause = PyObject_GetAttrString(pymod, "pause");
158-
s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
159-
s_python_function_fignum_exists = PyObject_GetAttrString(pymod, "fignum_exists");
160-
s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
161-
s_python_function_quiver = PyObject_GetAttrString(pymod, "quiver");
162-
s_python_function_semilogx = PyObject_GetAttrString(pymod, "semilogx");
163-
s_python_function_semilogy = PyObject_GetAttrString(pymod, "semilogy");
164-
s_python_function_loglog = PyObject_GetAttrString(pymod, "loglog");
165-
s_python_function_fill = PyObject_GetAttrString(pymod, "fill");
166-
s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between");
167-
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
168-
s_python_function_scatter = PyObject_GetAttrString(pymod,"scatter");
169-
s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot");
170-
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
171-
s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim");
172-
s_python_function_title = PyObject_GetAttrString(pymod, "title");
173-
s_python_function_axis = PyObject_GetAttrString(pymod, "axis");
174-
s_python_function_xlabel = PyObject_GetAttrString(pymod, "xlabel");
175-
s_python_function_ylabel = PyObject_GetAttrString(pymod, "ylabel");
176-
s_python_function_xticks = PyObject_GetAttrString(pymod, "xticks");
177-
s_python_function_yticks = PyObject_GetAttrString(pymod, "yticks");
178-
s_python_function_grid = PyObject_GetAttrString(pymod, "grid");
179-
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
180-
s_python_function_ion = PyObject_GetAttrString(pymod, "ion");
181-
s_python_function_ginput = PyObject_GetAttrString(pymod, "ginput");
182-
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
183-
s_python_function_annotate = PyObject_GetAttrString(pymod,"annotate");
184-
s_python_function_clf = PyObject_GetAttrString(pymod, "clf");
185-
s_python_function_errorbar = PyObject_GetAttrString(pymod, "errorbar");
186-
s_python_function_tight_layout = PyObject_GetAttrString(pymod, "tight_layout");
187-
s_python_function_stem = PyObject_GetAttrString(pymod, "stem");
188-
s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
189-
s_python_function_text = PyObject_GetAttrString(pymod, "text");
190-
s_python_function_suptitle = PyObject_GetAttrString(pymod, "suptitle");
191-
s_python_function_bar = PyObject_GetAttrString(pymod,"bar");
192-
s_python_function_subplots_adjust = PyObject_GetAttrString(pymod,"subplots_adjust");
193-
194-
if( !s_python_function_show
195-
|| !s_python_function_close
196-
|| !s_python_function_draw
197-
|| !s_python_function_pause
198-
|| !s_python_function_figure
199-
|| !s_python_function_fignum_exists
200-
|| !s_python_function_plot
201-
|| !s_python_function_quiver
202-
|| !s_python_function_semilogx
203-
|| !s_python_function_semilogy
204-
|| !s_python_function_loglog
205-
|| !s_python_function_fill
206-
|| !s_python_function_fill_between
207-
|| !s_python_function_subplot
208-
|| !s_python_function_legend
209-
|| !s_python_function_ylim
210-
|| !s_python_function_title
211-
|| !s_python_function_axis
212-
|| !s_python_function_xlabel
213-
|| !s_python_function_ylabel
214-
|| !s_python_function_grid
215-
|| !s_python_function_xlim
216-
|| !s_python_function_ion
217-
|| !s_python_function_ginput
218-
|| !s_python_function_save
219-
|| !s_python_function_clf
220-
|| !s_python_function_annotate
221-
|| !s_python_function_errorbar
222-
|| !s_python_function_errorbar
223-
|| !s_python_function_tight_layout
224-
|| !s_python_function_stem
225-
|| !s_python_function_xkcd
226-
|| !s_python_function_text
227-
|| !s_python_function_suptitle
228-
|| !s_python_function_bar
229-
|| !s_python_function_subplots_adjust
230-
) { throw std::runtime_error("Couldn't find required function!"); }
231-
232-
if ( !PyFunction_Check(s_python_function_show)
233-
|| !PyFunction_Check(s_python_function_close)
234-
|| !PyFunction_Check(s_python_function_draw)
235-
|| !PyFunction_Check(s_python_function_pause)
236-
|| !PyFunction_Check(s_python_function_figure)
237-
|| !PyFunction_Check(s_python_function_fignum_exists)
238-
|| !PyFunction_Check(s_python_function_plot)
239-
|| !PyFunction_Check(s_python_function_quiver)
240-
|| !PyFunction_Check(s_python_function_semilogx)
241-
|| !PyFunction_Check(s_python_function_semilogy)
242-
|| !PyFunction_Check(s_python_function_loglog)
243-
|| !PyFunction_Check(s_python_function_fill)
244-
|| !PyFunction_Check(s_python_function_fill_between)
245-
|| !PyFunction_Check(s_python_function_subplot)
246-
|| !PyFunction_Check(s_python_function_legend)
247-
|| !PyFunction_Check(s_python_function_annotate)
248-
|| !PyFunction_Check(s_python_function_ylim)
249-
|| !PyFunction_Check(s_python_function_title)
250-
|| !PyFunction_Check(s_python_function_axis)
251-
|| !PyFunction_Check(s_python_function_xlabel)
252-
|| !PyFunction_Check(s_python_function_ylabel)
253-
|| !PyFunction_Check(s_python_function_grid)
254-
|| !PyFunction_Check(s_python_function_xlim)
255-
|| !PyFunction_Check(s_python_function_ion)
256-
|| !PyFunction_Check(s_python_function_ginput)
257-
|| !PyFunction_Check(s_python_function_save)
258-
|| !PyFunction_Check(s_python_function_clf)
259-
|| !PyFunction_Check(s_python_function_tight_layout)
260-
|| !PyFunction_Check(s_python_function_errorbar)
261-
|| !PyFunction_Check(s_python_function_stem)
262-
|| !PyFunction_Check(s_python_function_xkcd)
263-
|| !PyFunction_Check(s_python_function_text)
264-
|| !PyFunction_Check(s_python_function_suptitle)
265-
|| !PyFunction_Check(s_python_function_bar)
266-
|| !PyFunction_Check(s_python_function_subplots_adjust)
267-
) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
166+
s_python_function_show = safe_import(pymod, "show");
167+
s_python_function_close = safe_import(pymod, "close");
168+
s_python_function_draw = safe_import(pymod, "draw");
169+
s_python_function_pause = safe_import(pymod, "pause");
170+
s_python_function_figure = safe_import(pymod, "figure");
171+
s_python_function_fignum_exists = safe_import(pymod, "fignum_exists");
172+
s_python_function_plot = safe_import(pymod, "plot");
173+
s_python_function_quiver = safe_import(pymod, "quiver");
174+
s_python_function_semilogx = safe_import(pymod, "semilogx");
175+
s_python_function_semilogy = safe_import(pymod, "semilogy");
176+
s_python_function_loglog = safe_import(pymod, "loglog");
177+
s_python_function_fill = safe_import(pymod, "fill");
178+
s_python_function_fill_between = safe_import(pymod, "fill_between");
179+
s_python_function_hist = safe_import(pymod,"hist");
180+
s_python_function_scatter = safe_import(pymod,"scatter");
181+
s_python_function_subplot = safe_import(pymod, "subplot");
182+
s_python_function_legend = safe_import(pymod, "legend");
183+
s_python_function_ylim = safe_import(pymod, "ylim");
184+
s_python_function_title = safe_import(pymod, "title");
185+
s_python_function_axis = safe_import(pymod, "axis");
186+
s_python_function_xlabel = safe_import(pymod, "xlabel");
187+
s_python_function_ylabel = safe_import(pymod, "ylabel");
188+
s_python_function_xticks = safe_import(pymod, "xticks");
189+
s_python_function_yticks = safe_import(pymod, "yticks");
190+
s_python_function_grid = safe_import(pymod, "grid");
191+
s_python_function_xlim = safe_import(pymod, "xlim");
192+
s_python_function_ion = safe_import(pymod, "ion");
193+
s_python_function_ginput = safe_import(pymod, "ginput");
194+
s_python_function_save = safe_import(pylabmod, "savefig");
195+
s_python_function_annotate = safe_import(pymod,"annotate");
196+
s_python_function_clf = safe_import(pymod, "clf");
197+
s_python_function_errorbar = safe_import(pymod, "errorbar");
198+
s_python_function_tight_layout = safe_import(pymod, "tight_layout");
199+
s_python_function_stem = safe_import(pymod, "stem");
200+
s_python_function_xkcd = safe_import(pymod, "xkcd");
201+
s_python_function_text = safe_import(pymod, "text");
202+
s_python_function_suptitle = safe_import(pymod, "suptitle");
203+
s_python_function_bar = safe_import(pymod,"bar");
204+
s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust");
268205

269206
s_python_empty_tuple = PyTuple_New(0);
270207
}

0 commit comments

Comments
 (0)
0