print traceback on failed import (#537)

This commit is contained in:
esquires 2016-12-01 05:35:34 -05:00 committed by Wenzel Jakob
parent 14bfe622f8
commit 67a68f1394
2 changed files with 2 additions and 3 deletions

View File

@ -511,7 +511,6 @@ PYBIND11_RUNTIME_EXCEPTION(stop_iteration, PyExc_StopIteration)
PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError) PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError)
PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError) PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError)
PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError) PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError)
PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError) PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally

View File

@ -580,7 +580,7 @@ public:
static module import(const char *name) { static module import(const char *name) {
PyObject *obj = PyImport_ImportModule(name); PyObject *obj = PyImport_ImportModule(name);
if (!obj) if (!obj)
throw import_error("Module \"" + std::string(name) + "\" not found!"); throw error_already_set();
return reinterpret_steal<module>(obj); return reinterpret_steal<module>(obj);
} }
@ -1495,7 +1495,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
} else { } else {
try { try {
file = module::import("sys").attr("stdout"); file = module::import("sys").attr("stdout");
} catch (const import_error &) { } catch (const error_already_set &) {
/* If print() is called from code that is executed as /* If print() is called from code that is executed as
part of garbage collection during interpreter shutdown, part of garbage collection during interpreter shutdown,
importing 'sys' can fail. Give up rather than crashing the importing 'sys' can fail. Give up rather than crashing the