Skip to content

Commit d612b52

Browse files
committed
Fix python3.8 segfault for the named_* family of functions
The fix might be more widely required, but in particular python3.8 would segfault when PyUnicode_FromString() was called before the interpreter was initialized. Which is expected tbh, but a change in behaviour from earlier versions.
1 parent f5f8ce3 commit d612b52

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

matplotlibcpp.h

+15
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ bool errorbar(const std::vector<NumericX> &x, const std::vector<NumericY> &y, co
11741174
template<typename Numeric>
11751175
bool named_plot(const std::string& name, const std::vector<Numeric>& y, const std::string& format = "")
11761176
{
1177+
// Make sure python is initialized.
1178+
detail::_interpreter::get();
1179+
11771180
PyObject* kwargs = PyDict_New();
11781181
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
11791182

@@ -1198,6 +1201,9 @@ bool named_plot(const std::string& name, const std::vector<Numeric>& y, const st
11981201
template<typename Numeric>
11991202
bool named_plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
12001203
{
1204+
// Make sure python is initialized.
1205+
detail::_interpreter::get();
1206+
12011207
PyObject* kwargs = PyDict_New();
12021208
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
12031209

@@ -1223,6 +1229,9 @@ bool named_plot(const std::string& name, const std::vector<Numeric>& x, const st
12231229
template<typename Numeric>
12241230
bool named_semilogx(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
12251231
{
1232+
// Make sure python is initialized.
1233+
detail::_interpreter::get();
1234+
12261235
PyObject* kwargs = PyDict_New();
12271236
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
12281237

@@ -1248,6 +1257,9 @@ bool named_semilogx(const std::string& name, const std::vector<Numeric>& x, cons
12481257
template<typename Numeric>
12491258
bool named_semilogy(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
12501259
{
1260+
// Make sure python is initialized.
1261+
detail::_interpreter::get();
1262+
12511263
PyObject* kwargs = PyDict_New();
12521264
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
12531265

@@ -1273,6 +1285,9 @@ bool named_semilogy(const std::string& name, const std::vector<Numeric>& x, cons
12731285
template<typename Numeric>
12741286
bool named_loglog(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
12751287
{
1288+
// Make sure python is initialized.
1289+
detail::_interpreter::get();
1290+
12761291
PyObject* kwargs = PyDict_New();
12771292
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
12781293

0 commit comments

Comments
 (0)