Skip to content

Commit 401ea6c

Browse files
committed
Merge pull request julien-duponchelle#49 from alex/fix-int24
Fixed parsing of small int24 values
2 parents 7511325 + 527b140 commit 401ea6c

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

pymysqlreplication/packet.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,17 @@ def read_length_coded_pascal_string(self, size):
206206

207207
def read_int24(self):
208208
a, b, c = struct.unpack("BBB", self.read(3))
209-
if a & 128:
210-
return a + (b << 8) + (c << 16)
211-
else:
212-
return (a + (b << 8) + (c << 16)) * -1
209+
res = a | (b << 8) | (c << 16)
210+
if res >= 0x800000:
211+
res -= 0x1000000
212+
return res
213213

214214
def read_int24_be(self):
215215
a, b, c = struct.unpack('BBB', self.read(3))
216-
if a & 128 == 0:
217-
return (a << 16) | (b << 8) | c
218-
else:
219-
return (-1 << 24) | (a << 16) | (b << 8) | c
216+
res = (a << 16) | (b << 8) | c
217+
if res >= 0x800000:
218+
res -= 0x1000000
219+
return res
220220

221221
def read_uint8(self):
222222
return struct.unpack('<B', self.read(1))[0]

pymysqlreplication/tests/test_data_type.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,15 @@ def test_longlong(self):
222222
self.assertEqual(event.rows[0]["values"]["test"], -9223372036854775808)
223223

224224
def test_int24(self):
225-
create_query = "CREATE TABLE test (id MEDIUMINT UNSIGNED NOT NULL, test MEDIUMINT, test2 MEDIUMINT)"
226-
insert_query = "INSERT INTO test VALUES(16777215, 8388607, -8388608)"
225+
create_query = "CREATE TABLE test (id MEDIUMINT UNSIGNED NOT NULL, test MEDIUMINT, test2 MEDIUMINT, test3 MEDIUMINT, test4 MEDIUMINT, test5 MEDIUMINT)"
226+
insert_query = "INSERT INTO test VALUES(16777215, 8388607, -8388608, 8, -8, 0)"
227227
event = self.create_and_insert_value(create_query, insert_query)
228228
self.assertEqual(event.rows[0]["values"]["id"], 16777215)
229229
self.assertEqual(event.rows[0]["values"]["test"], 8388607)
230230
self.assertEqual(event.rows[0]["values"]["test2"], -8388608)
231+
self.assertEqual(event.rows[0]["values"]["test3"], 8)
232+
self.assertEqual(event.rows[0]["values"]["test4"], -8)
233+
self.assertEqual(event.rows[0]["values"]["test5"], 0)
231234

232235
def test_date(self):
233236
create_query = "CREATE TABLE test (test DATE);"

0 commit comments

Comments
 (0)