|
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | 16 | import ctypes |
| 17 | +import struct |
| 18 | +import sys |
17 | 19 |
|
18 | 20 | from pyignite.constants import * |
19 | 21 | from .base import IgniteDataType |
@@ -48,50 +50,70 @@ class Primitive(IgniteDataType): |
48 | 50 | def parse(cls, client: 'Client'): |
49 | 51 | return cls.c_type, client.recv(ctypes.sizeof(cls.c_type)) |
50 | 52 |
|
51 | | - @staticmethod |
52 | | - def to_python(ctype_object, *args, **kwargs): |
53 | | - return ctype_object |
54 | | - |
55 | 53 | @classmethod |
56 | | - def from_python(cls, value): |
57 | | - return bytes(cls.c_type(value)) |
| 54 | + def to_python(cls, ctype_object, *args, **kwargs): |
| 55 | + return ctype_object |
58 | 56 |
|
59 | 57 |
|
60 | 58 | class Byte(Primitive): |
61 | 59 | _type_name = NAME_BYTE |
62 | 60 | _type_id = TYPE_BYTE |
63 | 61 | c_type = ctypes.c_byte |
64 | 62 |
|
| 63 | + @classmethod |
| 64 | + def from_python(cls, value): |
| 65 | + return struct.pack("<b", value) |
| 66 | + |
65 | 67 |
|
66 | 68 | class Short(Primitive): |
67 | 69 | _type_name = NAME_SHORT |
68 | 70 | _type_id = TYPE_SHORT |
69 | 71 | c_type = ctypes.c_short |
70 | 72 |
|
| 73 | + @classmethod |
| 74 | + def from_python(cls, value): |
| 75 | + return struct.pack("<h", value) |
| 76 | + |
71 | 77 |
|
72 | 78 | class Int(Primitive): |
73 | 79 | _type_name = NAME_INT |
74 | 80 | _type_id = TYPE_INT |
75 | 81 | c_type = ctypes.c_int |
76 | 82 |
|
| 83 | + @classmethod |
| 84 | + def from_python(cls, value): |
| 85 | + return struct.pack("<i", value) |
| 86 | + |
77 | 87 |
|
78 | 88 | class Long(Primitive): |
79 | 89 | _type_name = NAME_LONG |
80 | 90 | _type_id = TYPE_LONG |
81 | 91 | c_type = ctypes.c_longlong |
82 | 92 |
|
| 93 | + @classmethod |
| 94 | + def from_python(cls, value): |
| 95 | + return struct.pack("<q", value) |
| 96 | + |
83 | 97 |
|
84 | 98 | class Float(Primitive): |
85 | 99 | _type_name = NAME_FLOAT |
86 | 100 | _type_id = TYPE_FLOAT |
87 | 101 | c_type = ctypes.c_float |
88 | 102 |
|
| 103 | + @classmethod |
| 104 | + def from_python(cls, value): |
| 105 | + return struct.pack("<f", value) |
| 106 | + |
89 | 107 |
|
90 | 108 | class Double(Primitive): |
91 | 109 | _type_name = NAME_DOUBLE |
92 | 110 | _type_id = TYPE_DOUBLE |
93 | 111 | c_type = ctypes.c_double |
94 | 112 |
|
| 113 | + @classmethod |
| 114 | + def from_python(cls, value): |
| 115 | + return struct.pack("<d", value) |
| 116 | + |
95 | 117 |
|
96 | 118 | class Char(Primitive): |
97 | 119 | _type_name = NAME_CHAR |
@@ -122,4 +144,12 @@ def from_python(cls, value): |
122 | 144 | class Bool(Primitive): |
123 | 145 | _type_name = NAME_BOOLEAN |
124 | 146 | _type_id = TYPE_BOOLEAN |
125 | | - c_type = ctypes.c_bool |
| 147 | + c_type = ctypes.c_byte # Use c_byte because c_bool throws endianness conversion error on BE systems. |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def to_python(cls, ctype_object, *args, **kwargs): |
| 151 | + return ctype_object != 0 |
| 152 | + |
| 153 | + @classmethod |
| 154 | + def from_python(cls, value): |
| 155 | + return struct.pack("<b", 1 if value else 0) |
0 commit comments