address number caster regression (fixes #484)
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index dd81d9a..ddc1890 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -449,8 +449,13 @@
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);
+ if (type_error && PyNumber_Check(src.ptr())) {
+ object tmp(std::is_floating_point<T>::value
+ ? PyNumber_Float(src.ptr())
+ : PyNumber_Long(src.ptr()), true);
+ PyErr_Clear();
+ return load(tmp, false);
+ }
return false;
}
diff --git a/tests/test_issues.cpp b/tests/test_issues.cpp
index 1374afe..084ea31 100644
--- a/tests/test_issues.cpp
+++ b/tests/test_issues.cpp
@@ -11,7 +11,7 @@
#include "constructor_stats.h"
#include <pybind11/stl.h>
#include <pybind11/operators.h>
-
+#include <pybind11/complex.h>
#define TRACKERS(CLASS) CLASS() { print_default_created(this); } ~CLASS() { print_destroyed(this); }
struct NestABase { int value = -2; TRACKERS(NestABase) };
@@ -346,10 +346,12 @@
.def("child", &SpecialHolderObj::child, pybind11::return_value_policy::reference_internal)
.def_readwrite("val", &SpecialHolderObj::val)
.def_static("holder_cstats", &ConstructorStats::get<custom_unique_ptr<SpecialHolderObj>>,
- py::return_value_policy::reference)
- ;
-};
+ py::return_value_policy::reference);
+ /// Issue #484: number conversion generates unhandled exceptions
+ m2.def("test_complex", [](float x) { py::print("{}"_s.format(x)); });
+ m2.def("test_complex", [](std::complex<float> x) { py::print("({}, {})"_s.format(x.real(), x.imag())); });
+}
// MSVC workaround: trying to use a lambda here crashes MSCV
test_initializer issues(&init_issues);
diff --git a/tests/test_issues.py b/tests/test_issues.py
index 6f84f77..a2cf530 100644
--- a/tests/test_issues.py
+++ b/tests/test_issues.py
@@ -60,7 +60,7 @@
assert i == v.value()
-def test_no_id(capture, msg):
+def test_no_id(msg):
from pybind11_tests.issues import get_element, expect_float, expect_int
with pytest.raises(TypeError) as excinfo:
@@ -147,6 +147,7 @@
m1 = get_moveissue1(1)
assert m1.value == 1
+
def test_override_ref():
from pybind11_tests.issues import OverrideTest
o = OverrideTest("asdf")
@@ -162,6 +163,7 @@
a.value = "bye"
assert a.value == "bye"
+
def test_operators_notimplemented(capture):
from pybind11_tests.issues import OpTest1, OpTest2
with capture:
@@ -175,6 +177,7 @@
Add OpTest2 with OpTest1
Add OpTest2 with OpTest1"""
+
def test_iterator_rvpolicy():
""" Issue 388: Can't make iterators via make_iterator() with different r/v policies """
from pybind11_tests.issues import make_iterator_1
@@ -184,6 +187,7 @@
assert list(make_iterator_2()) == [1, 2, 3]
assert(type(make_iterator_1()) != type(make_iterator_2()))
+
def test_dupe_assignment():
""" Issue 461: overwriting a class with a function """
from pybind11_tests.issues import dupe_exception_failures
@@ -202,6 +206,7 @@
del child, parent
assert cstats.alive() == 0
+
def test_non_destructed_holders():
""" Issue #478: unique ptrs constructed and freed without destruction """
from pybind11_tests import SpecialHolderObj
@@ -218,3 +223,17 @@
assert cstats.alive() == 1
del a
assert cstats.alive() == 0
+
+
+def test_complex_cast(capture):
+ """ Issue #484: number conversion generates unhandled exceptions """
+ from pybind11_tests.issues import test_complex
+
+ with capture:
+ test_complex(1)
+ test_complex(2j)
+
+ assert capture == """
+1.0
+(0.0, 2.0)
+"""