Open aldanor opened 4 years ago
Just to complete the issue's documentation: what versions of Python and Numpy are you using? In principle, numpy scalars are quite broken/very complicated in Python2 as well as prior to Numpy 1.15.
Nevertheless, for numpy scalars such as np.float32
you have to implement py::buffer
(or py::array
) overloads. Those types are fundamentally arrays/buffers with 1 element in size and 0D dimensionality.
https://github.com/openPMD/openPMD-api/blob/0.10.3-alpha/src/binding/python/RecordComponent.cpp#L438-L530
https://github.com/openPMD/openPMD-api/blob/0.10.3-alpha/src/binding/python/Attributable.cpp#L266-L315
I've already fixed it myself, was just lazy to push a PR here :) If there's any interest, I can push it.
No, you don't need to implement buffer APIs. NumPy provides C API for extracting scalars out of PyObject directly.
So, something along the lines of py::numpy_scalar<int64_t>
or py::numpy_scalar<float>
which will be super strict and only accepts numpy scalars of the types requested.
@ax3l Ok, pushed a PR - #2060
(on 2.4.3) Looks like there's a mix of a few issues in the example below:
np.float32()
actually worked. You may expect it would have worked but because it doesn't inherit frombuiltin.float
it won't. Is it worth special-casing scalar float32 case?I do sort of understand where this is coming from, but someone who doesn't know py11 internals may assume that "well, np.float32[:] arrays work, and np.float64[:] arrays work, and I can pass np.float64 and builtin.float for double-type arguments, so np.float32 should work as well?", but then it gets converted to a different type altogether.
Let's give this a spin: