Don't return ndarray with empty shape through named indexers.

NumPy arrays with an empty shape cannot be assigned via the usual syntax.

Fixes: #238.
Related: #237.
PiperOrigin-RevId: 441265437
Change-Id: Ib0de22c83f9babe9a97682dc5a32660c198ad68c
This commit is contained in:
Saran Tunyasuvunakool
2022-04-12 12:49:17 -07:00
parent 0d5d1f5ff2
commit 55c1c91382
2 changed files with 25 additions and 1 deletions
+5
View File
@@ -102,6 +102,11 @@ py::array_t<T> MakeArray(T* base_ptr, int index, std::vector<int>&& shape,
offset = m.tuple_adr[index];
shape.insert(shape.begin(), m.tuple_size[index]);
} else {
// Do not return a NumPy array with shape () since these aren't very nice
// to work with. Instead, always return singleton arrays with shape (1,).
if (shape.empty()) {
shape.push_back(1);
}
int size = 1;
for (int s : shape) {
size *= s;