mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 22:52:01 +00:00
Made arithmetic and complex casters respect convert
Arithmetic and complex casters now only do a converting cast when `convert=true`; previously they would convert always (e.g. when passing an int to a float-accepting function, or a float to complex-accepting function).
This commit is contained in:
parent
0558a9a739
commit
709675a7aa
@ -473,18 +473,22 @@ public:
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value>> {
|
struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value>> {
|
||||||
typedef typename std::conditional<sizeof(T) <= sizeof(long), long, long long>::type _py_type_0;
|
using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>;
|
||||||
typedef typename std::conditional<std::is_signed<T>::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>::type _py_type_1;
|
using _py_type_1 = conditional_t<std::is_signed<T>::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>;
|
||||||
typedef typename std::conditional<std::is_floating_point<T>::value, double, _py_type_1>::type py_type;
|
using py_type = conditional_t<std::is_floating_point<T>::value, double, _py_type_1>;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
bool load(handle src, bool) {
|
bool load(handle src, bool convert) {
|
||||||
py_type py_value;
|
py_type py_value;
|
||||||
|
|
||||||
if (!src) {
|
if (!src)
|
||||||
return false;
|
return false;
|
||||||
} if (std::is_floating_point<T>::value) {
|
|
||||||
py_value = (py_type) PyFloat_AsDouble(src.ptr());
|
if (std::is_floating_point<T>::value) {
|
||||||
|
if (convert || PyFloat_Check(src.ptr()))
|
||||||
|
py_value = (py_type) PyFloat_AsDouble(src.ptr());
|
||||||
|
else
|
||||||
|
return false;
|
||||||
} else if (sizeof(T) <= sizeof(long)) {
|
} else if (sizeof(T) <= sizeof(long)) {
|
||||||
if (PyFloat_Check(src.ptr()))
|
if (PyFloat_Check(src.ptr()))
|
||||||
return false;
|
return false;
|
||||||
@ -511,7 +515,7 @@ public:
|
|||||||
bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
|
bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
|
||||||
#endif
|
#endif
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
if (type_error && PyNumber_Check(src.ptr())) {
|
if (type_error && convert && PyNumber_Check(src.ptr())) {
|
||||||
auto tmp = reinterpret_borrow<object>(std::is_floating_point<T>::value
|
auto tmp = reinterpret_borrow<object>(std::is_floating_point<T>::value
|
||||||
? PyNumber_Float(src.ptr())
|
? PyNumber_Float(src.ptr())
|
||||||
: PyNumber_Long(src.ptr()));
|
: PyNumber_Long(src.ptr()));
|
||||||
|
@ -28,9 +28,11 @@ template <typename T> struct is_fmt_numeric<std::complex<T>> {
|
|||||||
|
|
||||||
template <typename T> class type_caster<std::complex<T>> {
|
template <typename T> class type_caster<std::complex<T>> {
|
||||||
public:
|
public:
|
||||||
bool load(handle src, bool) {
|
bool load(handle src, bool convert) {
|
||||||
if (!src)
|
if (!src)
|
||||||
return false;
|
return false;
|
||||||
|
if (!convert && !PyComplex_Check(src.ptr()))
|
||||||
|
return false;
|
||||||
Py_complex result = PyComplex_AsCComplex(src.ptr());
|
Py_complex result = PyComplex_AsCComplex(src.ptr());
|
||||||
if (result.real == -1.0 && PyErr_Occurred()) {
|
if (result.real == -1.0 && PyErr_Occurred()) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
Loading…
Reference in New Issue
Block a user