-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit conversion of ints (and char const*) to handles? #1309
Comments
I don't think there's any particularly good reason why it's not there--other than that no one has proposed/implemented it yet. (As a side note, for tuples in particular, you're better off keeping the object as a For the more generic |
OK, I really thought that was such an obvious thing to do that I must have had missed a good reason :-) For example, is there a way to ensure that the literal 0 becomes the integer 0 rather than a null pointer (as it does now)? |
Yup. Meanwhile there are other places that already do accept arbitrary arguments--the arguments to |
@jagerman @anntzer do you think woodychow@98c9f77 works? /cc @woodychow |
I guess so? Haven't tried, though. |
cielavenir, hope you are doing well. Well, let me know if you want me to create a pull request. I don't really remember the context of this anymore. |
as pybind11 2.10 dropped python2, perhaps we should make MR to v2.9 branch? could you? I resolved conflict in https://github.com/cielavenir/pybind11/commits/v2.9_item_accessor_T . By the way I was able to test our system on https://github.com/cielavenir/pybind11/commits/v2.9_ty . |
Well, it's time to upgrade to Python 3 buddy. I guess I can CP to v2.9 if this MR is accepted, and v2.9 is still open to new MRs... |
What's the motivation? For pytypes that have int or char* as a valid subscript type, pytypes already extends that as can be found here. int to pointer conversion is not desirable for a lot of reasons, including hurting the compilers' optimization performance. Any explicit conversions to handles or objects etc can already be handled by pybind11::cast() function so I am unsure what the motivation is here. |
To be clear, I was originally only proposing this for #include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(python_example, m) {
py::object d = py::dict();
d["foo"] = "foo"; py::print(d["foo"]); // OK.
// d[1] = 1; py::print(d[1]); // Not OK.
} |
@anntzer It seems like the real issue is that 0 is implicitly convertible to a pyobject* which is implicitly convertible to a handle. It's not intended behavior for ints to be implicitly convertible to a handle (and bugprone behavior). I would argue that this implicit behavior makes things a bit harder to debug especially for more complex casts than primitives. Thoughts @rwgk @henryiii ? |
Alternative solution: seems like it may have encountered an issue in our test suite though: #4006 |
@woodychow sorry - could you add me as a collaborator of your pybimd11 repository? |
Issue description
Is there a reason why handles are not implicitly constructible from ints (I may well have missed something obvious...)? e.g. something like
seems to have a clear meaning but fails to compile (
error: invalid conversion from ‘int’ to ‘const char*’
(there are two overloads for operator[], one taking a handle and the other a const char*)).If char const* was also implicitly convertible to handle then the second overload would not be necessary either.
For additional confusion, 0 is implicitly convertible to a handle (an invalid one: the null pointer).
Asked on gitter (a more specific version of this) but got no reply.
Reproducible example code
Include the above-mentioned code in python_example (replacing the
add
function there, and them.def(...
line).The text was updated successfully, but these errors were encountered: