3 ms·
Do you actually need ctypes here, or can you just use np.reshape? That would cut out the unsafety.
by comex 2y ago
Do you actually need ctypes here, or can you just use np.reshape? That would cut out the unsafety.
- Retr0id 2y agoYou can exploit memory unsafety in cpython using only built-in methods, no imports needed at all: https://github.com/DavidBuchanan314/unsafe-python/ https://github.com/DavidBuchanan314/unsafe-python/
- yosefk 2y agoYou... probably shouldn't, but, very interesting stuff in that link! I didn't know CPython had no bounds checks in the load_const bytecode op
- im3w1l 2y agoI think what you want is a combination of swapaxes and flip (reshape does not turn rows into columns, it turns m input rows into n output rows), but yeah. Actually let me include a little figure original reshape swapaxes abc ab ad def cd be ef cf
- yosefk 2y agoI think you'll have a problem with BGR data absent ctypes, since you have a numpy array with the base pointing at the first red channel value, and you want an array with the base pointing 2 bytes before the first red channel value. This is almost definitely "unsafe"?.. or does numpy have functions knowing that due to the negative z stride, the red value before the blue value is within the bounds of the original array? I somehow doubt it though it would be very impressive. And I think the last A value is hopelessly out of reach absent ctypes since a safe API has no information that this last value is within the array bounds; more strictly speaking all the alpha values are out of bounds according to the shape and strides of the BGRA array.