Skip to content

Commit 0401e3e

Browse files
tustvoldlava
authored andcommitted
Added clf function and fixed mistakes due to the way PyTuple_SetItem steals references.
1 parent be35f6a commit 0401e3e

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

matplotlibcpp.h

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ namespace matplotlibcpp {
3030
PyObject *s_python_function_xlabel;
3131
PyObject *s_python_function_ylabel;
3232
PyObject *s_python_function_grid;
33+
PyObject *s_python_function_clf;
3334
PyObject *s_python_empty_tuple;
34-
PyObject *s_python_function_annotate;
35+
PyObject *s_python_function_annotate;
3536

3637
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
3738
multiple independent embedded python interpreters without patching the python source code
@@ -77,30 +78,31 @@ namespace matplotlibcpp {
7778
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
7879
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
7980
s_python_function_annotate = PyObject_GetAttrString(pymod,"annotate");
81+
s_python_function_clf = PyObject_GetAttrString(pymod, "clf");
8082

8183
if( !s_python_function_show
82-
|| !s_python_function_figure
83-
|| !s_python_function_plot
84-
|| !s_python_function_subplot
84+
|| !s_python_function_figure
85+
|| !s_python_function_plot
86+
|| !s_python_function_subplot
8587
|| !s_python_function_legend
86-
|| !s_python_function_ylim
87-
|| !s_python_function_title
88-
|| !s_python_function_axis
89-
|| !s_python_function_xlabel
90-
|| !s_python_function_ylabel
91-
|| !s_python_function_grid
92-
|| !s_python_function_xlim
93-
|| !s_python_function_save
94-
|| !s_python_function_annotate
95-
)
96-
{ throw std::runtime_error("Couldn't find required function!"); }
97-
98-
if( !PyFunction_Check(s_python_function_show)
88+
|| !s_python_function_ylim
89+
|| !s_python_function_title
90+
|| !s_python_function_axis
91+
|| !s_python_function_xlabel
92+
|| !s_python_function_ylabel
93+
|| !s_python_function_grid
94+
|| !s_python_function_xlim
95+
|| !s_python_function_save
96+
|| !s_python_function_clf
97+
|| !s_python_function_annotate
98+
) { throw std::runtime_error("Couldn't find required function!"); }
99+
100+
if ( !PyFunction_Check(s_python_function_show)
99101
|| !PyFunction_Check(s_python_function_figure)
100102
|| !PyFunction_Check(s_python_function_plot)
101103
|| !PyFunction_Check(s_python_function_subplot)
102-
|| !PyFunction_Check(s_python_function_legend)
103-
|| !PyFunction_Check(s_python_function_annotate)
104+
|| !PyFunction_Check(s_python_function_legend)
105+
|| !PyFunction_Check(s_python_function_annotate)
104106
|| !PyFunction_Check(s_python_function_ylim)
105107
|| !PyFunction_Check(s_python_function_title)
106108
|| !PyFunction_Check(s_python_function_axis)
@@ -109,8 +111,8 @@ namespace matplotlibcpp {
109111
|| !PyFunction_Check(s_python_function_grid)
110112
|| !PyFunction_Check(s_python_function_xlim)
111113
|| !PyFunction_Check(s_python_function_save)
112-
)
113-
{ throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
114+
|| !PyFunction_Check(s_python_function_clf)
115+
) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
114116

115117
s_python_empty_tuple = PyTuple_New(0);
116118
}
@@ -164,9 +166,6 @@ namespace matplotlibcpp {
164166
PyTuple_SetItem(args, 0, xlist);
165167
PyTuple_SetItem(args, 1, ylist);
166168

167-
Py_DECREF(xlist);
168-
Py_DECREF(ylist);
169-
170169
// construct keyword args
171170
PyObject* kwargs = PyDict_New();
172171
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
@@ -217,7 +216,7 @@ namespace matplotlibcpp {
217216
PyObject* ylist = PyList_New(y.size());
218217
PyObject* kwargs = PyDict_New();
219218
PyDict_SetItemString(kwargs, "label", PyString_FromString(label.c_str()));
220-
PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
219+
PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
221220
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
222221
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
223222

@@ -264,7 +263,8 @@ namespace matplotlibcpp {
264263

265264
return res;
266265
}
267-
template<typename Numeric>
266+
267+
template<typename Numeric>
268268
bool named_plot(const std::string& name, const std::vector<Numeric>& y, const std::string& format = "") {
269269
PyObject* kwargs = PyDict_New();
270270
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
@@ -499,11 +499,18 @@ namespace matplotlibcpp {
499499
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args);
500500
if(!res) throw std::runtime_error("Call to save() failed.");
501501

502-
Py_DECREF(pyfilename);
503502
Py_DECREF(args);
504503
Py_DECREF(res);
505504
}
506505

506+
inline void clf() {
507+
PyObject *res = PyObject_CallObject(detail::_interpreter::get().s_python_function_clf,
508+
detail::_interpreter::get().s_python_empty_tuple);
509+
if (!res) throw std::runtime_error("Call to clf() failed.");
510+
511+
Py_DECREF(res);
512+
}
513+
507514
#if __cplusplus > 199711L
508515
// C++11-exclusive content starts here (variadic plot() and initializer list support)
509516

@@ -582,8 +589,6 @@ namespace matplotlibcpp {
582589

583590
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
584591

585-
Py_DECREF(xlist);
586-
Py_DECREF(ylist);
587592
Py_DECREF(plot_args);
588593
if(res) Py_DECREF(res);
589594

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy