mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-06 05:39:53 +00:00
Fix #283: don't print first arg of constructor
This changes the exception error message of a bad-arguments error to suppress the constructor argument when the failure is a constructor. This changes both the "Invoked with: " output to omit the object instances, and rewrites the constructor signature to make it look like a constructor (changing the first argument to the object name, and removing the ' -> NoneType' return type.
This commit is contained in:
@@ -138,4 +138,23 @@ void init_issues(py::module &m) {
|
||||
} catch (std::runtime_error &) {
|
||||
/* All good */
|
||||
}
|
||||
|
||||
// Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
|
||||
class StrIssue {
|
||||
public:
|
||||
StrIssue(int i) : val{i} {}
|
||||
StrIssue() : StrIssue(-1) {}
|
||||
int value() const { return val; }
|
||||
private:
|
||||
int val;
|
||||
};
|
||||
py::class_<StrIssue> si(m2, "StrIssue");
|
||||
si .def(py::init<int>())
|
||||
.def(py::init<>())
|
||||
.def("__str__", [](const StrIssue &si) {
|
||||
std::cout << "StrIssue.__str__ called" << std::endl;
|
||||
return "StrIssue[" + std::to_string(si.value()) + "]";
|
||||
})
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user