mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-08 23:39:12 +00:00
Fix undefined memoryview format (#2223)
* Fix undefined memoryview format * Add missing <algorithm> header * Add workaround for py27 array compatibility * Workaround py27 memoryview behavior * Fix memoryview constructor from buffer_info * Workaround PyMemoryView_FromMemory availability in py27 * Fix up memoryview tests * Update memoryview test from buffer to check signedness * Use static factory method to create memoryview * Remove ndim arg from memoryview::frombuffer and add tests * Allow ndim=0 memoryview and documentation fixup * Use void* to align to frombuffer method signature * Add const variants of frombuffer and frommemory * Add memory view section in doc * Fix docs * Add test for null buffer * Workaround py27 nullptr behavior in test * Rename frombuffer to from_buffer
This commit is contained in:
@@ -318,4 +318,53 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("test_list_slicing", [](py::list a) {
|
||||
return a[py::slice(0, -1, 2)];
|
||||
});
|
||||
|
||||
m.def("test_memoryview_object", [](py::buffer b) {
|
||||
return py::memoryview(b);
|
||||
});
|
||||
|
||||
m.def("test_memoryview_buffer_info", [](py::buffer b) {
|
||||
return py::memoryview(b.request());
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer", [](bool is_unsigned) {
|
||||
static const int16_t si16[] = { 3, 1, 4, 1, 5 };
|
||||
static const uint16_t ui16[] = { 2, 7, 1, 8 };
|
||||
if (is_unsigned)
|
||||
return py::memoryview::from_buffer(
|
||||
ui16, { 4 }, { sizeof(uint16_t) });
|
||||
else
|
||||
return py::memoryview::from_buffer(
|
||||
si16, { 5 }, { sizeof(int16_t) });
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_nativeformat", []() {
|
||||
static const char* format = "@i";
|
||||
static const int32_t arr[] = { 4, 7, 5 };
|
||||
return py::memoryview::from_buffer(
|
||||
arr, sizeof(int32_t), format, { 3 }, { sizeof(int32_t) });
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_empty_shape", []() {
|
||||
static const char* buf = "";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", { }, { });
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_invalid_strides", []() {
|
||||
static const char* buf = "\x02\x03\x04";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", { 3 }, { });
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_nullptr", []() {
|
||||
return py::memoryview::from_buffer(
|
||||
static_cast<void*>(nullptr), 1, "B", { }, { });
|
||||
});
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
m.def("test_memoryview_from_memory", []() {
|
||||
const char* buf = "\xff\xe1\xab\x37";
|
||||
return py::memoryview::from_memory(
|
||||
buf, static_cast<ssize_t>(strlen(buf)));
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user