Skip to content

Commit 7898225

Browse files
committed
Adds close() function.
1 parent ffa4809 commit 7898225

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

matplotlibcpp.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace matplotlibcpp {
3131

3232
struct _interpreter {
3333
PyObject *s_python_function_show;
34+
PyObject *s_python_function_close;
3435
PyObject *s_python_function_draw;
3536
PyObject *s_python_function_pause;
3637
PyObject *s_python_function_save;
@@ -116,6 +117,7 @@ namespace matplotlibcpp {
116117
if(!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
117118

118119
s_python_function_show = PyObject_GetAttrString(pymod, "show");
120+
s_python_function_close = PyObject_GetAttrString(pymod, "close");
119121
s_python_function_draw = PyObject_GetAttrString(pymod, "draw");
120122
s_python_function_pause = PyObject_GetAttrString(pymod, "pause");
121123
s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
@@ -138,6 +140,7 @@ namespace matplotlibcpp {
138140
s_python_function_tight_layout = PyObject_GetAttrString(pymod, "tight_layout");
139141

140142
if( !s_python_function_show
143+
|| !s_python_function_close
141144
|| !s_python_function_draw
142145
|| !s_python_function_pause
143146
|| !s_python_function_figure
@@ -161,6 +164,7 @@ namespace matplotlibcpp {
161164
) { throw std::runtime_error("Couldn't find required function!"); }
162165

163166
if ( !PyFunction_Check(s_python_function_show)
167+
|| !PyFunction_Check(s_python_function_close)
164168
|| !PyFunction_Check(s_python_function_draw)
165169
|| !PyFunction_Check(s_python_function_pause)
166170
|| !PyFunction_Check(s_python_function_figure)
@@ -673,11 +677,21 @@ namespace matplotlibcpp {
673677
res = PyObject_Call( detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple, kwargs);
674678
}
675679

680+
if (!res) throw std::runtime_error("Call to show() failed.");
676681

677-
if (!res) throw std::runtime_error("Call to show() failed.");
682+
Py_DECREF(res);
683+
}
678684

679-
Py_DECREF(res);
680-
}
685+
inline void close()
686+
{
687+
PyObject* res = PyObject_CallObject(
688+
detail::_interpreter::get().s_python_function_close,
689+
detail::_interpreter::get().s_python_empty_tuple);
690+
691+
if (!res) throw std::runtime_error("Call to close() failed.");
692+
693+
Py_DECREF(res);
694+
}
681695

682696
inline void draw()
683697
{

0 commit comments

Comments
 (0)