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):