Skip to content

Commit 0e39f8f

Browse files
author
Benno Evers
committed
Load mplot3d module lazily.
Load the mplot3d module lazily when `plot_surface()` is called, to avoid introducing accidental dependencies.
1 parent eae3824 commit 0e39f8f

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

matplotlibcpp.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,9 @@ struct _interpreter {
116116

117117
PyObject* matplotlibname = PyString_FromString("matplotlib");
118118
PyObject* pyplotname = PyString_FromString("matplotlib.pyplot");
119-
PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
120-
PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
121-
PyObject* pylabname = PyString_FromString("pylab");
122119
PyObject* cmname = PyString_FromString("matplotlib.cm");
123-
if (!pyplotname || !pylabname || !matplotlibname || !mpl_toolkits ||
124-
!axis3d || !cmname) {
120+
PyObject* pylabname = PyString_FromString("pylab");
121+
if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
125122
throw std::runtime_error("couldnt create string");
126123
}
127124

@@ -147,14 +144,6 @@ struct _interpreter {
147144
Py_DECREF(pylabname);
148145
if (!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
149146

150-
PyObject* mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
151-
Py_DECREF(mpl_toolkitsmod);
152-
if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }
153-
154-
PyObject* axis3dmod = PyImport_Import(axis3d);
155-
Py_DECREF(axis3dmod);
156-
if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
157-
158147
s_python_function_show = PyObject_GetAttrString(pymod, "show");
159148
s_python_function_close = PyObject_GetAttrString(pymod, "close");
160149
s_python_function_draw = PyObject_GetAttrString(pymod, "draw");
@@ -414,7 +403,29 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
414403
const std::vector<::std::vector<Numeric>> &y,
415404
const std::vector<::std::vector<Numeric>> &z,
416405
const std::map<std::string, std::string> &keywords =
417-
std::map<std::string, std::string>()) {
406+
std::map<std::string, std::string>())
407+
{
408+
// We lazily load the modules here the first time this function is called
409+
// because I'm not sure that we can assume "matplotlib installed" implies
410+
// "mpl_toolkits installed" on all platforms, and we don't want to require
411+
// it for people who don't need 3d plots.
412+
static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
413+
if (!mpl_toolkitsmod) {
414+
detail::_interpreter::get();
415+
416+
PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
417+
PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
418+
if (!mpl_toolkits || !axis3d) { throw std::runtime_error("couldnt create string"); }
419+
420+
mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
421+
Py_DECREF(mpl_toolkits);
422+
if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }
423+
424+
axis3dmod = PyImport_Import(axis3d);
425+
Py_DECREF(axis3d);
426+
if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
427+
}
428+
418429
assert(x.size() == y.size());
419430
assert(y.size() == z.size());
420431

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