Skip to content

array_t constructor takes shape as size_t, not ssize_t #1599

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

Closed
anntzer opened this issue Nov 8, 2018 · 0 comments · Fixed by #2293
Closed

array_t constructor takes shape as size_t, not ssize_t #1599

anntzer opened this issue Nov 8, 2018 · 0 comments · Fixed by #2293

Comments

@anntzer
Copy link
Contributor

anntzer commented Nov 8, 2018

Issue description

The array_t constructor takes its input shape as unsigned size_t (== npt_uintp), even though the numpy API (e.g. PyArray_NewFromDescr) uses signed ssize_t (== npy_intp) and pybind11 itself uses signed sizes in most places (e.g. ssize_t shape(ssize_t dim)).

Reproducible example code

Use the following snippet into the setup provided in the pybind11/python_example repo:

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

py::array_t<int> make_array(ssize_t size) {
    return py::array_t<int>{{size}};
}

PYBIND11_MODULE(python_example, m) {
    m.def("make_array", &make_array);
}

Compilation (pybind11 2.2.4, gcc 8.2.1) yields

src/main.cpp: In function ‘pybind11::array_t<int> make_array(ssize_t)’:
src/main.cpp:7:35: warning: narrowing conversion of ‘size’ from ‘ssize_t’ {aka ‘long int’} to ‘pybind11::size_t’ {aka ‘long unsigned int’} inside { } [-Wnarrowing]
     return py::array_t<int>{{size}};
bstaletic added a commit to bstaletic/pybind11 that referenced this issue Jul 12, 2020
bstaletic added a commit to bstaletic/pybind11 that referenced this issue Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant