Skip to content

Commit 9bb5a53

Browse files
committed
Added clf function and fixed mistakes due to the way PyTuple_SetItem steals references.
1 parent 579bf03 commit 9bb5a53

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

matplotlibcpp.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace matplotlibcpp {
3232
PyObject *s_python_function_xlabel;
3333
PyObject *s_python_function_ylabel;
3434
PyObject *s_python_function_grid;
35+
PyObject *s_python_function_clf;
3536
PyObject *s_python_empty_tuple;
3637
PyObject *s_python_function_annotate;
3738

@@ -79,6 +80,7 @@ namespace matplotlibcpp {
7980
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
8081
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
8182
s_python_function_annotate = PyObject_GetAttrString(pymod, "annotate");
83+
s_python_function_clf = PyObject_GetAttrString(pymod, "clf");
8284

8385
if (!s_python_function_show
8486
|| !s_python_function_figure
@@ -93,6 +95,7 @@ namespace matplotlibcpp {
9395
|| !s_python_function_grid
9496
|| !s_python_function_xlim
9597
|| !s_python_function_save
98+
|| !s_python_function_clf
9699
|| !s_python_function_annotate
97100
) { throw std::runtime_error("Couldn't find required function!"); }
98101

@@ -110,6 +113,7 @@ namespace matplotlibcpp {
110113
|| !PyFunction_Check(s_python_function_grid)
111114
|| !PyFunction_Check(s_python_function_xlim)
112115
|| !PyFunction_Check(s_python_function_save)
116+
|| !PyFunction_Check(s_python_function_clf)
113117
) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
114118

115119
s_python_empty_tuple = PyTuple_New(0);
@@ -163,9 +167,6 @@ namespace matplotlibcpp {
163167
PyTuple_SetItem(args, 0, xlist);
164168
PyTuple_SetItem(args, 1, ylist);
165169

166-
Py_DECREF(xlist);
167-
Py_DECREF(ylist);
168-
169170
// construct keyword args
170171
PyObject *kwargs = PyDict_New();
171172
for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it) {
@@ -492,11 +493,18 @@ namespace matplotlibcpp {
492493
PyObject *res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args);
493494
if (!res) throw std::runtime_error("Call to save() failed.");
494495

495-
Py_DECREF(pyfilename);
496496
Py_DECREF(args);
497497
Py_DECREF(res);
498498
}
499499

500+
inline void clf() {
501+
PyObject *res = PyObject_CallObject(detail::_interpreter::get().s_python_function_clf,
502+
detail::_interpreter::get().s_python_empty_tuple);
503+
if (!res) throw std::runtime_error("Call to clf() failed.");
504+
505+
Py_DECREF(res);
506+
}
507+
500508
#if __cplusplus > 199711L
501509
// C++11-exclusive content starts here (variadic plot() and initializer list support)
502510

@@ -577,8 +585,6 @@ namespace matplotlibcpp {
577585

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

580-
Py_DECREF(xlist);
581-
Py_DECREF(ylist);
582588
Py_DECREF(plot_args);
583589
if (res) Py_DECREF(res);
584590

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