Skip to content

array_t constructor takes shape as size_t, not ssize_t #1599

Closed
@anntzer

Description

@anntzer

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}};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions