Closed
Description
Since commit 496feac
the following code executes the right function but quits with an unhandled exception.
Failing code
test.cpp
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <iostream>
namespace py = pybind11;
PYBIND11_PLUGIN(test) {
py::module m("test");
m.def("test", [] ( float x ) {
std::cout << x << std::endl;
});
m.def("test", [] ( std::complex<float> x ) {
std::cout << x << std::endl;
});
return m.ptr();
}
test.py
import test
test.test(1)
test.test(2j)
The output:
$ python3 test.py
1
(0,2)
TypeError: can't convert complex to int
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 4, in <module>
test.test(2j)
SystemError: <built-in method test of PyCapsule object at 0x7fba58d193c0> returned a result with an error set
Suggested solution
In cast.h, line 453, it can happen that PyNumber_Long
fails. In this case it leaves an unhandled TypeError
.
451 PyErr_Clear();
452 if (type_error && PyNumber_Check(src.ptr()))
453 return load(object(PyNumber_Long(src.ptr()), true), false);
454 return false;
My suggestion is to call PyErr_Clear() again after trying the conversion:
if (type_error && PyNumber_Check(src.ptr())) {
bool res = load(object(PyNumber_Long(src.ptr()), true), false);
PyErr_Clear();
return res;
}
Metadata
Metadata
Assignees
Labels
No labels