wrappy icon indicating copy to clipboard operation
wrappy copied to clipboard

C++ error in loadModule

Open rgiot opened this issue 8 years ago • 2 comments

With g++6.2.0, wrappy raise the following error when launching example_plot

terminate called after throwing an instance of 'wrappy::WrappyError'
  what():  Wrappy: Lookup of function matplotlib.pyplot.plotfailed.
zsh: abort (core dumped)  ./example_plot

The following patch solves the issue

diff --git a/wrappy.cpp b/wrappy.cpp
index 9a0ec17..bca9895 100644
--- a/wrappy.cpp
+++ b/wrappy.cpp
@@ -68,9 +68,9 @@ PythonObject loadModule(const std::string& name, size_t& dot)
     PythonObject module;
     while (!module && dot != std::string::npos) {
         dot = name.rfind('.', dot-1);
-        auto prefix = name.substr(0, dot).c_str();
+        auto prefix = name.substr(0, dot);
         module = PythonObject(PythonObject::owning {},
-            PyImport_ImportModule(prefix));
+            PyImport_ImportModule(prefix.c_str()));
     }

     return module;

In the original version, prefix is a char * which address the buffer of a temporary string object which is supposed to not live the next line (and thus when used by PyImport_ImportModule). In the corrected version, prefix is a string which lives until the end of the function, its buffer is used when the string is still alive

rgiot avatar Dec 23 '16 23:12 rgiot

Nice find, thanks! If you care about having your name on the commit, feel free to create a pull request (or a full .patch file using git format-patch), otherwise I'll just apply the fix myself.

lava avatar Dec 25 '16 23:12 lava

No worries, you can patch by yourself ;)

rgiot avatar Dec 27 '16 12:12 rgiot