pybind11: implicitly convert NumPy integer scalars

The current integer caster was unnecessarily strict and rejected
various kinds of NumPy integer types when calling C++ functions
expecting normal integers. This relaxes the current behavior.
This commit is contained in:
Wenzel Jakob
2016-10-28 00:37:07 +02:00
parent a6f85eb985
commit 496feacfd0
3 changed files with 16 additions and 0 deletions

View File

@@ -443,7 +443,14 @@ public:
(std::is_integral<T>::value && sizeof(py_type) != sizeof(T) &&
(py_value < (py_type) std::numeric_limits<T>::min() ||
py_value > (py_type) std::numeric_limits<T>::max()))) {
#if PY_VERSION_HEX < 0x03000000
bool type_error = PyErr_ExceptionMatches(PyExc_SystemError);
#else
bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
#endif
PyErr_Clear();
if (type_error && PyNumber_Check(src.ptr()))
return load(object(PyNumber_Long(src.ptr()), true), false);
return false;
}