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:
Jason Rhinelander
2016-07-17 17:43:00 -04:00
parent fbdd30e5c5
commit 4e45e1805b
4 changed files with 56 additions and 4 deletions

View File

@@ -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()) + "]";
})
;
}