Skip to content

[cppyy] Set LowLevelView itemsize to sizeof(T) for fixed multidim arrays#21388

Open
aaronj0 wants to merge 3 commits intoroot-project:masterfrom
aaronj0:fix-lowlevel-fixed-itemsize
Open

[cppyy] Set LowLevelView itemsize to sizeof(T) for fixed multidim arrays#21388
aaronj0 wants to merge 3 commits intoroot-project:masterfrom
aaronj0:fix-lowlevel-fixed-itemsize

Conversation

@aaronj0
Copy link
Contributor

@aaronj0 aaronj0 commented Feb 26, 2026

For fixed contiguous C arrays (e.g. float a[2][2]), the ndim > 1 path in CreateLowLevelViewT was unconditionally setting view.itemsize to sizeof(void*), which is correct for pointer-to-pointer arrays but does not work for fixed arrays. Fixes #21378

For fixed contiguous C arrays (e.g. float a[2][2]), the ndim > 1 path in CreateLowLevelViewT was unconditionally setting view.itemsize to sizeof(void*), which is correct for pointer-to-pointer arrays but does not work for fixed arrays. Fixes root-project#21378
Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Could you also add a test?

@aaronj0
Copy link
Contributor Author

aaronj0 commented Feb 26, 2026

This is great! Could you also add a test?

neat, this actually revealed a bug in a test pyunittests-bindings-pyroot-pythonizations-pyroot-array-numpy-views that I wrote for fixed-width integer low level views: #18492 :)

Back then, I didn't realise why I had to read the buffer as 1D and reshape the resulting Py_buffer but now it all makes sense...
In the test, we see the same case, view.len = 16, and view.itemsize = 8 so frombuffer saw 2 elements of size 8, but count=22 forced it to reinterpret the raw bytes as 22 elements anyway. That's a bad pattern..

With this patch, frombuffer correctly sees 4 floats, but count=22 now exceeds the buffer size (as it should). Now that the underlying issue is fixed, we can just use np.array(cpp_arr, dtype=dtype[0]) and it works as expected!

…numpy

This was a (terrible) workaround to the fact that resulting views of multidim arrays did not convey the right itemsize based on the underlying type. Now that root-project#21378 is fixed, the workaround breaks and we can just use `np.array(arr, dtype)` which is a lot better and Pythonic for users.
@github-actions
Copy link

github-actions bot commented Feb 26, 2026

Test Results

    21 files      21 suites   2d 22h 13m 21s ⏱️
 3 807 tests  3 804 ✅ 1 💤 2 ❌
72 874 runs  72 863 ✅ 9 💤 2 ❌

For more details on these failures, see this check.

Results for commit 6d9f885.

♻️ This comment has been updated with latest results.

@aaronj0 aaronj0 force-pushed the fix-lowlevel-fixed-itemsize branch from cbf159a to 6d9f885 Compare February 26, 2026 21:29
Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] NumPy conversion fails for multidimensional arrays (incorrect itemsize in LowLevelView)

2 participants