Commit dd57a34e by Wenzel Jakob

improved error handling at module import time

parent 9d573f44
...@@ -67,12 +67,24 @@ ...@@ -67,12 +67,24 @@
#endif #endif
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define PYBIND11_PLUGIN(name) \ #define PYBIND11_PLUGIN_IMPL(name) \
extern "C" PYBIND11_EXPORT PyObject *PyInit_##name() extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
#else #else
#define PYBIND11_PLUGIN(name) \ #define PYBIND11_PLUGIN_IMPL(name) \
extern "C" PYBIND11_EXPORT PyObject *init##name() extern "C" PYBIND11_EXPORT PyObject *init##name()
#endif #endif
#define PYBIND11_PLUGIN(name) \
static PyObject *pybind11_init(); \
PYBIND11_PLUGIN_IMPL(name) { \
try { \
return pybind11_init(); \
} catch (const std::exception &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \
return nullptr; \
} \
} \
PyObject *pybind11_init()
NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(pybind11)
......
...@@ -471,7 +471,10 @@ public: ...@@ -471,7 +471,10 @@ public:
} }
static module import(const char *name) { static module import(const char *name) {
return module(PyImport_ImportModule(name), false); PyObject *obj = PyImport_ImportModule(name);
if (!obj)
throw std::runtime_error("Module \"" + std::string(name) + "\" not found!");
return module(obj, false);
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment