Uh oh!
There was an error while loading. Please reload this page.
Extended options object to choose between the print of __repr__ and __str__ (edited title) - #901
Extended options object to choose between the print of __repr__ and __str__ (edited title)#901DerThorsten wants to merge 6 commits into
Conversation
update from pybind11 to my fork
…ble function arguments.
jagerman
commented
Jun 12, 2017
Unfortunately, neither |
jagerman
commented
Jun 12, 2017
Also, for making this configurable, a define is discouraged—it's easy to cause an ODR violation if you link together different compilation units which don't have exact feature macro matches. If you do want to write up support for showing |
DerThorsten
commented
Jun 12, 2017
I'll try to implement it as suggested using and commit the results the next days |
…ould be used if argmuments mismatch
DerThorsten
commented
Jun 12, 2017
I used More suggestions? |
jagerman
commented
Jun 12, 2017
I'd prefer the names |
So |
jagerman
commented
Jun 12, 2017
Ah, right, I forgot about the conflicting getter. How about: |
DerThorsten
commented
Jun 12, 2017
Fine ! commit is coming in a sec |
DerThorsten
commented
Jun 12, 2017
Anything else I can do to accept this PR? |
Some suggestions:
py::class_<C> c(m, "C");
{
py::options options;
options.set_type_error_print_str();
c.def("f", [](ArgType a) { /* ... */ }); // Uses str() on argument failure
}
c.def("g", [](ArgType a) { /* ... */ }); // Uses repr() on argument failureThe problem is that once you're out of the scope, the option gets reset to default, so you need to preserve the option as part of the function definition at the time the definition is set. To do this, you'll need to add a
|
DerThorsten
commented
Jun 12, 2017
Thanks for the detailed instructions! |
dean0x7d
commented
Jun 12, 2017
Long messages for arrays are annoying, but I'm not sure that switching to importnumpyasnpdefstr_vs_repr(n):
a=np.arange(n)
print(str(a))
print(repr(a))For Perhaps it would be better to leave |
@dean0x7d good suggestion. I would suggest the following: |
jagerman
commented
Jun 12, 2017
Another option that might be valuable: displaying the argument type name, rather than either |
So in our usecases with https://github.com/DerThorsten/nifty/ we get the TypeError because we called the function with the wrong arguments. Therefore I really like your suggestion |
jagerman
commented
Jun 12, 2017
Okay, forget the |
Ok, you get a PR soon (but most probably not today) |
dean0x7d
commented
Jun 12, 2017
I'm not really a fan of making everything an option. Good defaults are very important. An option should be added only if there is a compelling reason. For |
jagerman
commented
Jun 12, 2017
I don't feel particularly strongly either way, so I guess that means go with the truncated |
wjakob
commented
Jun 12, 2017
I am not so sure about this PR. It's possible to extend pybind11 in a myriad of different ways to address every one-off requirement that users of this project might have, which will make everyone happy in the short term, but which would lead to significant bloat eventually. When we accept PRs, they should provide a clear improvement that helps more than just one or two users. This one IMO seems below the threshold. |
jagerman
commented
Jun 12, 2017
@wjakob - right; I think we settled on just truncating the |
@jagerman I impemented the trucation and add I think this message could reduce confusing. Are you fine with this impl. ATM I use 100 as a size limit which could be to small. Suggestions? Should I open a new PR or should we stick to this one? Greetings |
jagerman
commented
Jun 13, 2017
Stick to this PR.
// Returns the string size (in unicode codepoints, *not* bytes)size_tsize() { return (size_t) PyUnicode_GET_LENGTH(m_ptr); }and then in the "invoked with" code use something like this for the shortening logic: auto arg_repr = pybind11::repr(args_[ti]);
std::string arg =
#ifdef NDEBUG
arg_repr.size() <= 100 ? (std::string) arg_repr : (std::string) pybind11::str("{!r:.95}").format(arg_repr) + "[...]";
#else
arg_repr;
#endifI'm using 100 for the sake of example: I think we could shorten it to, e.g., 50 (with 45 in the format). We could also use just "..." instead of "[...]", and change the format size value to I think it might also be worthwhile only when |
jagerman
commented
Jul 20, 2017
@DerThorsten - ping, any updates on this? |
wjakob
commented
Sep 11, 2018
I took another look at this and just don't think that this is important enough to warrant the necessary changes. Sorry, it seems like you spent quite a bit of time on this. |
pybind11 will raise a TypeError if the exported classes / functions are called with incompatible function arguments.
In the cpp impl
pybind11::repr/__repr__is called, but I would strongly suggest to callpybind::str/__str_.__repr__can get very very long for array like classes.The printout can take very long and furthermore it does not help the user very much.
One could also something like:
If this is preferred, I could implement this.
But I can see almost no benefit to just using
__str__