Skip to content

Commit b5b4c25

Browse files
committed
Throw error_already_set if module creation fails in module_::create_extension_module
1 parent a65a38b commit b5b4c25

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

include/pybind11/pybind11.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -976,18 +976,18 @@ class module_ : public object {
976976
/* m_free */ nullptr
977977
};
978978
auto m = PyModule_Create(def);
979-
if (m == nullptr)
980-
pybind11_fail("Internal error in module_::create_extension_module()");
981-
// TODO: Should be reinterpret_steal, but Python also steals it again when returned from PyInit_...
982-
return reinterpret_borrow<module_>(m);
983979
#else
984980
// Ignore module_def *def; only necessary for Python 3
985981
(void) def;
986982
auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
983+
#endif
984+
if (PyErr_Occurred())
985+
throw error_already_set();
987986
if (m == nullptr)
988987
pybind11_fail("Internal error in module_::create_extension_module()");
988+
// TODO: Sould be reinterpret_steal for Python 3, but Python also steals it again when returned from PyInit_...
989+
// For Python 2, reinterpret_borrow is correct.
989990
return reinterpret_borrow<module_>(m);
990-
#endif
991991
}
992992
};
993993

0 commit comments

Comments
 (0)