Commit 4c00fd9e by Wenzel Jakob

extra python version sanity check at import time

Python 3.5 can often import pybind11 modules compiled compiled for
Python 3.4 (i.e. all symbols can be resolved), but this leads to crashes
later on due to changes in various Python-internal data structures. This
commit adds an extra sanity check to prevent a successful import when
the Python versions don't match.
parent f6661015
...@@ -147,6 +147,18 @@ extern "C" { ...@@ -147,6 +147,18 @@ extern "C" {
#define PYBIND11_PLUGIN(name) \ #define PYBIND11_PLUGIN(name) \
static PyObject *pybind11_init(); \ static PyObject *pybind11_init(); \
PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_PLUGIN_IMPL(name) { \
int major, minor; \
if (sscanf(Py_GetVersion(), "%i.%i", &major, &minor) != 2) { \
PyErr_SetString(PyExc_ImportError, "Can't parse Python version."); \
return nullptr; \
} else if (major != PY_MAJOR_VERSION || minor != PY_MINOR_VERSION) { \
PyErr_Format(PyExc_ImportError, \
"Python version mismatch: module was compiled for " \
"version %i.%i, while the interpreter is running " \
"version %i.%i.", PY_MAJOR_VERSION, PY_MINOR_VERSION, \
major, minor); \
return nullptr; \
} \
try { \ try { \
return pybind11_init(); \ return pybind11_init(); \
} catch (const std::exception &e) { \ } catch (const std::exception &e) { \
......
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