Skip to content

Commit 672be9a

Browse files
authored
GG-27270 Support big-endian systems
Fix primitives decoding on big-endian architectures
1 parent b00b7d5 commit 672be9a

7 files changed

Lines changed: 30 additions & 11 deletions

File tree

pygridgain/api/binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_binary_type(
8585
if result.status != 0:
8686
return result
8787
result.value = {
88-
'type_exists': response.type_exists
88+
'type_exists': Bool.to_python(response.type_exists)
8989
}
9090
if hasattr(response, 'body'):
9191
result.value.update(body_struct.to_python(response.body))

pygridgain/datatypes/internal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Struct:
190190

191191
def parse(
192192
self, client: 'Client'
193-
) -> Tuple[ctypes.BigEndianStructure, bytes]:
193+
) -> Tuple[ctypes.LittleEndianStructure, bytes]:
194194
buffer = b''
195195
fields = []
196196

@@ -201,7 +201,7 @@ def parse(
201201
fields.append((name, c_type))
202202

203203
data_class = type(
204-
'Struct',
204+
'StructLE',
205205
(ctypes.LittleEndianStructure,),
206206
{
207207
'_pack_': 1,

pygridgain/datatypes/primitive.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
#
1616
import ctypes
17+
import sys
1718

1819
from pygridgain.constants import *
1920
from .base import GridGainDataType
@@ -48,13 +49,20 @@ class Primitive(GridGainDataType):
4849
def parse(cls, client: 'Client'):
4950
return cls.c_type, client.recv(ctypes.sizeof(cls.c_type))
5051

51-
@staticmethod
52-
def to_python(ctype_object, *args, **kwargs):
52+
@classmethod
53+
def to_python(cls, ctype_object, *args, **kwargs):
5354
return ctype_object
5455

5556
@classmethod
5657
def from_python(cls, value):
57-
return bytes(cls.c_type(value))
58+
return Primitive.fix_endianness(bytes(cls.c_type(value)))
59+
60+
@staticmethod
61+
def fix_endianness(buf):
62+
if len(buf) > 1 and sys.byteorder != PROTOCOL_BYTE_ORDER:
63+
buf = buf[::-1]
64+
65+
return buf
5866

5967

6068
class Byte(Primitive):
@@ -122,4 +130,8 @@ def from_python(cls, value):
122130
class Bool(Primitive):
123131
_type_name = NAME_BOOLEAN
124132
_type_id = TYPE_BOOLEAN
125-
c_type = ctypes.c_bool
133+
c_type = ctypes.c_byte # Use c_byte because c_bool throws endianness conversion error on BE systems.
134+
135+
@classmethod
136+
def to_python(cls, ctype_object, *args, **kwargs):
137+
return ctype_object != 0

pygridgain/datatypes/primitive_objects.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,16 @@ def from_python(cls, value):
207207
class BoolObject(DataObject):
208208
_type_name = NAME_BOOLEAN
209209
_type_id = TYPE_BOOLEAN
210-
c_type = ctypes.c_bool
210+
c_type = ctypes.c_byte # Use c_byte because c_bool throws endianness conversion error on BE systems.
211211
type_code = TC_BOOL
212212
pythonic = bool
213213
default = False
214214

215215
@staticmethod
216216
def hashcode(value: bool, *args, **kwargs) -> int:
217217
return 1231 if value else 1237
218+
219+
@classmethod
220+
def to_python(cls, ctype_object, *args, **kwargs):
221+
return ctype_object.value != 0
222+

pygridgain/queries/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def parse(self, conn: 'Connection'):
189189
)
190190
fields += body_class._fields_ + [
191191
('data', data_class),
192-
('more', ctypes.c_bool),
192+
('more', ctypes.c_byte),
193193
]
194194
buffer += body_buffer + data_buffer
195195

@@ -371,7 +371,7 @@ def parse(self, client: 'Client'):
371371
)
372372
fields += body_class._fields_ + [
373373
('data', data_class),
374-
('more', ctypes.c_bool),
374+
('more', ctypes.c_byte),
375375
]
376376
buffer += body_buffer + data_buffer
377377
else:

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
log_cli = True

tests/test_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class OuterType(
225225

226226
def test_add_schema_to_binary_object(client):
227227

228-
migrate_cache = client.create_cache('migrate_binary')
228+
migrate_cache = client.get_or_create_cache('migrate_binary')
229229

230230
class MyBinaryType(
231231
metaclass=GenericObjectMeta,

0 commit comments

Comments
 (0)