Fix Python host-endianness dependencies (#7773)

* In Python tests, use host-endian-independent dtypes

* Fix host endianness dependence in Python flexbuffers

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Ben Beasley
2023-01-10 13:20:08 -05:00
committed by GitHub
parent b50b6be60a
commit b23493a7d2
3 changed files with 23 additions and 23 deletions

View File

@@ -75,7 +75,7 @@ class BitWidth(enum.IntEnum):
@staticmethod
def F(value):
"""Returns the `BitWidth` to encode floating point value."""
if struct.unpack('f', struct.pack('f', value))[0] == value:
if struct.unpack('<f', struct.pack('<f', value))[0] == value:
return BitWidth.W32
return BitWidth.W64
@@ -95,20 +95,20 @@ F = {4: 'f', 8: 'd'} # Floating point formats
def _Unpack(fmt, buf):
return struct.unpack(fmt[len(buf)], buf)[0]
return struct.unpack('<%s' % fmt[len(buf)], buf)[0]
def _UnpackVector(fmt, buf, length):
byte_width = len(buf) // length
return struct.unpack('%d%s' % (length, fmt[byte_width]), buf)
return struct.unpack('<%d%s' % (length, fmt[byte_width]), buf)
def _Pack(fmt, value, byte_width):
return struct.pack(fmt[byte_width], value)
return struct.pack('<%s' % fmt[byte_width], value)
def _PackVector(fmt, values, byte_width):
return struct.pack('%d%s' % (len(values), fmt[byte_width]), *values)
return struct.pack('<%d%s' % (len(values), fmt[byte_width]), *values)
def _Mutate(fmt, buf, value, byte_width, value_bit_width):

View File

@@ -74,15 +74,15 @@ def int_sizes(value):
def int_bytes(value, byte_width):
return struct.pack({1: 'b', 2: 'h', 4: 'i', 8: 'q'}[byte_width], value)
return struct.pack('<%s' % {1: 'b', 2: 'h', 4: 'i', 8: 'q'}[byte_width], value)
def uint_bytes(value, byte_width):
return struct.pack({1: 'B', 2: 'H', 4: 'I', 8: 'Q'}[byte_width], value)
return struct.pack('<%s' % {1: 'B', 2: 'H', 4: 'I', 8: 'Q'}[byte_width], value)
def float_bytes(value, byte_width):
return struct.pack({4: 'f', 8: 'd'}[byte_width], value)
return struct.pack('<%s' % {4: 'f', 8: 'd'}[byte_width], value)
def min_value(type_, byte_width):

View File

@@ -701,18 +701,18 @@ def CheckReadBuffer(buf, offset, sizePrefix=False, file_identifier=None):
import numpy as np
asserter(monster.InventoryAsNumpy().sum() == 10)
asserter(monster.InventoryAsNumpy().dtype == np.dtype('uint8'))
asserter(monster.InventoryAsNumpy().dtype == np.dtype('<u1'))
VectorOfLongs = monster.VectorOfLongsAsNumpy()
asserter(VectorOfLongs.dtype == np.dtype('int64'))
asserter(VectorOfLongs.dtype == np.dtype('<i8'))
for i in range(5):
asserter(VectorOfLongs[i] == 10**(i * 2))
VectorOfDoubles = monster.VectorOfDoublesAsNumpy()
asserter(VectorOfDoubles.dtype == np.dtype('float64'))
asserter(VectorOfDoubles[0] == np.finfo('float64').min)
asserter(VectorOfDoubles.dtype == np.dtype('<f8'))
asserter(VectorOfDoubles[0] == np.finfo('<f8').min)
asserter(VectorOfDoubles[1] == 0.0)
asserter(VectorOfDoubles[2] == np.finfo('float64').max)
asserter(VectorOfDoubles[2] == np.finfo('<f8').max)
except ImportError:
# If numpy does not exist, trying to get vector as numpy
@@ -857,33 +857,33 @@ class TestFuzz(unittest.TestCase):
check(table, 'bool', self.boolVal,
table.GetSlot(f, False, N.BoolFlags))
elif choice == 1:
check(table, 'int8', self.int8Val, table.GetSlot(f, 0, N.Int8Flags))
check(table, '<i1', self.int8Val, table.GetSlot(f, 0, N.Int8Flags))
elif choice == 2:
check(table, 'uint8', self.uint8Val,
check(table, '<u1', self.uint8Val,
table.GetSlot(f, 0, N.Uint8Flags))
elif choice == 3:
check(table, 'int16', self.int16Val,
check(table, '<i2', self.int16Val,
table.GetSlot(f, 0, N.Int16Flags))
elif choice == 4:
check(table, 'uint16', self.uint16Val,
check(table, '<u2', self.uint16Val,
table.GetSlot(f, 0, N.Uint16Flags))
elif choice == 5:
check(table, 'int32', self.int32Val,
check(table, '<i4', self.int32Val,
table.GetSlot(f, 0, N.Int32Flags))
elif choice == 6:
check(table, 'uint32', self.uint32Val,
check(table, '<u4', self.uint32Val,
table.GetSlot(f, 0, N.Uint32Flags))
elif choice == 7:
check(table, 'int64', self.int64Val,
check(table, '<i8', self.int64Val,
table.GetSlot(f, 0, N.Int64Flags))
elif choice == 8:
check(table, 'uint64', self.uint64Val,
check(table, '<u8', self.uint64Val,
table.GetSlot(f, 0, N.Uint64Flags))
elif choice == 9:
check(table, 'float32', self.float32Val,
check(table, '<f4', self.float32Val,
table.GetSlot(f, 0, N.Float32Flags))
elif choice == 10:
check(table, 'float64', self.float64Val,
check(table, '<f8', self.float64Val,
table.GetSlot(f, 0, N.Float64Flags))
else:
raise RuntimeError('unreachable')