Support non-byte and non-1d memoryviews when converting to IOBufs
Summary: Using `shape[0]` as the length of the IOBuf means using the number of _elements_ along the first dimension, rather than the total number of bytes in the memoryview. Thus - if the element size is not 1 byte, this will get only a fraction of the memory; - if the memoryview is shapeless, the code will segfault; - if the memoryview is multidimensional, it will only get a fraction of the memory (and, if the memory is C-contiguous, it will be an odd slice: in a 2x3 memoryview, it will get the entire first row and half of the second). Perhaps the above was intentional, but I believe that when converting a shaped and typed object to an unshaped and untyped buffer, it's more intuitive to ignore the type and size and just look at the memory backing it. I also added a check to ensure the view is contiguous. IOBufs can't represent a non-contiguous buffer, except by using a chain, which however in this case could have an arbitrary unbounded length, making it unadvisable. Alternatively we could copy the memory, but this is probably not what the user would expect. Disallowing this seems the safest option. Reviewed By: yfeldblum Differential Revision: D18006273 fbshipit-source-id: f21dbb462266eee68f1a5ce0ecea85b1a46e0a8b
Showing
Please register or sign in to comment