Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python host-endianness dependencies #7773

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions python/flatbuffers/flexbuffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def I(value):
@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

Expand All @@ -95,20 +95,20 @@ def B(byte_width):


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):
Expand Down
6 changes: 3 additions & 3 deletions tests/py_flexbuffers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 15 additions & 15 deletions tests/py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,18 +701,18 @@ def asserter(stmt):
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
Expand Down Expand Up @@ -857,33 +857,33 @@ def check(table, desc, want, got):
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')
Expand Down