-
Notifications
You must be signed in to change notification settings - Fork 1.2k
template to handle strings on x axis w/ legend #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1394,6 +1394,33 @@ bool named_plot(const std::string& name, const std::vector<Numeric>& x, const st | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template<typename Numeric> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool named_plot(const std::string& name, const std::vector<std::string>& x, const std::vector<Numeric>& y, const std::string& format = "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detail::_interpreter::get(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* kwargs = PyDict_New(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* xarray = detail::get_array(x); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* yarray = detail::get_array(y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* pystring = PyString_FromString(format.c_str()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* plot_args = PyTuple_New(3); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyTuple_SetItem(plot_args, 0, xarray); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyTuple_SetItem(plot_args, 1, yarray); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyTuple_SetItem(plot_args, 2, pystring); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Py_DECREF(kwargs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Py_DECREF(plot_args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (res) Py_DECREF(res); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template<typename Numeric> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1730
to
+1756
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool named_plot(const std::string& name, const std::vector<std::string>& x, const std::vector<Numeric>& y, const std::string& format = "") | |
| { | |
| detail::_interpreter::get(); | |
| PyObject* kwargs = PyDict_New(); | |
| PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); | |
| PyObject* xarray = detail::get_array(x); | |
| PyObject* yarray = detail::get_array(y); | |
| PyObject* pystring = PyString_FromString(format.c_str()); | |
| PyObject* plot_args = PyTuple_New(3); | |
| PyTuple_SetItem(plot_args, 0, xarray); | |
| PyTuple_SetItem(plot_args, 1, yarray); | |
| PyTuple_SetItem(plot_args, 2, pystring); | |
| PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs); | |
| Py_DECREF(kwargs); | |
| Py_DECREF(plot_args); | |
| if (res) Py_DECREF(res); | |
| return res; | |
| } | |
| template<typename Numeric> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyDict_SetItemStringdoes not steal a reference to the value; passing the temporary result ofPyString_FromString(...)here leaks a reference. Store the created Python string in a localPyObject*, insert it intokwargs, thenPy_DECREFit after insertion (same pattern as used elsewhere when the API doesn’t steal refs).