-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BUG] bytes constructor takes size as size_t, not ssize_t #2690
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
Comments
But you're passing the wrong type to pybind11? I don't see why 1) a length of a string should be possibly negative, and 2) why you would ant to pass a |
pybind11 is just forwarding the arguments to PyBytes_FromStringAndSize, which takes a ssize_t (indeed, calling |
Yes, but pybind11 wraps the C API. Saying that
That ís an argument. I would kind of argue that's a wrong return type, but OK, nothing we can do about that.
Well, fair enough, but this is not an index, but a size. So we don't need an index type, but a size type. |
There's also the argument of consistency with py::array_t (whose constructor also takes signed sizes). You were apparently fine with that in #2293? |
That was increasing consistency. Most things already were To the contrary, constructing I'm not saying it might not be a good idea to add an overload, btw. I'm just not convinced by the argument that the API pybind11 wraps takes a different type, so pybind11 should have a different API (what's the point of wrapping an API, otherwise?), and definitely not by a warning about narrowing when you pass the wrong type to an API. Guess an overload can't really hurt, though (can it?), if you feel like making a PR. |
FWIW I mostly tend to view pybind11 as an (nice) RAII/autorefcounted version of the C-API. Changing ssize_t to size_t doesn't really help (me) on that point.
Sure, I'll push that on my todo list :) Just one additional data point in favor of signed sizes: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1227r2.html. |
Not sure that outweighs all of the already existing code and standard that uses/return |
Closed by new PR. #3219 |
Issue description
The
py::bytes(char const*, size_t)
constructor takes its input size as unsigned size_t, even though the underlying CPython API (https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromStringAndSize) uses signed ssize_t. See also #1599 for a similar case.Reproducible example code
Use the following snippet into the setup provided in the pybind11/python_example repo:
Compilation (pybind11 2.6.0, gcc 10.2.0) yields
The text was updated successfully, but these errors were encountered: