gh-90751: memoryview now supports half-float (#96738)

Co-authored-by: Antoine Pitrou <antoine@python.org>
This commit is contained in:
Dong-hee Na 2022-09-11 05:44:10 +09:00 committed by GitHub
parent c4e57fb6df
commit 8d75a13fde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 10 deletions

View file

@ -13,6 +13,7 @@ import array
import io
import copy
import pickle
import struct
from test.support import import_helper
@ -527,6 +528,14 @@ class OtherTest(unittest.TestCase):
m[2:] = memoryview(p6).cast(format)[2:]
self.assertEqual(d.value, 0.6)
def test_half_float(self):
half_data = struct.pack('eee', 0.0, -1.5, 1.5)
float_data = struct.pack('fff', 0.0, -1.5, 1.5)
half_view = memoryview(half_data).cast('e')
float_view = memoryview(float_data).cast('f')
self.assertEqual(half_view.nbytes * 2, float_view.nbytes)
self.assertListEqual(half_view.tolist(), float_view.tolist())
def test_memoryview_hex(self):
# Issue #9951: memoryview.hex() segfaults with non-contiguous buffers.
x = b'0' * 200000