From fd702c6a586bef83fe1c0d777e882094b4c43274 Mon Sep 17 00:00:00 2001 From: Alf Date: Mon, 12 Apr 2021 17:00:28 +0800 Subject: [PATCH 1/4] fix int2byte() import. --- pymysqlreplication/binlogstream.py | 2 +- pymysqlreplication/event.py | 2 +- pymysqlreplication/packet.py | 2 +- pymysqlreplication/row_event.py | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pymysqlreplication/binlogstream.py b/pymysqlreplication/binlogstream.py index 7d9d3121..8a58a365 100644 --- a/pymysqlreplication/binlogstream.py +++ b/pymysqlreplication/binlogstream.py @@ -6,7 +6,7 @@ from pymysql.constants.COMMAND import COM_BINLOG_DUMP, COM_REGISTER_SLAVE from pymysql.cursors import DictCursor -from pymysql.util import int2byte +from six import int2byte from .packet import BinLogPacketWrapper from .constants.BINLOG import TABLE_MAP_EVENT, ROTATE_EVENT diff --git a/pymysqlreplication/event.py b/pymysqlreplication/event.py index 97ff5817..971320a0 100644 --- a/pymysqlreplication/event.py +++ b/pymysqlreplication/event.py @@ -4,7 +4,7 @@ import struct import datetime -from pymysql.util import byte2int, int2byte +from six import byte2int, int2byte class BinLogEvent(object): diff --git a/pymysqlreplication/packet.py b/pymysqlreplication/packet.py index f1680abc..1cbd6620 100644 --- a/pymysqlreplication/packet.py +++ b/pymysqlreplication/packet.py @@ -2,7 +2,7 @@ import struct -from pymysql.util import byte2int +from six import byte2int from pymysqlreplication import constants, event, row_event diff --git a/pymysqlreplication/row_event.py b/pymysqlreplication/row_event.py index 15329b7f..e0540663 100644 --- a/pymysqlreplication/row_event.py +++ b/pymysqlreplication/row_event.py @@ -3,9 +3,9 @@ import struct import decimal import datetime -import json +# import json -from pymysql.util import byte2int +from six import byte2int from pymysql.charset import charset_by_name from .event import BinLogEvent @@ -617,7 +617,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs) 'COLUMN_TYPE': 'BLOB', # we don't know what it is, so let's not do anything with it. 'COLUMN_KEY': '', } - col = Column(byte2int(column_type), column_schema, from_packet) + col = Column(column_type, column_schema, from_packet) self.columns.append(col) self.table_obj = Table(self.column_schemas, self.table_id, self.schema, From b18bf5ff5dd7f1023a8385d6d72143e1a8be4322 Mon Sep 17 00:00:00 2001 From: Alf Date: Wed, 21 Apr 2021 15:18:13 +0800 Subject: [PATCH 2/4] using global loose_version for better performence. --- pymysqlreplication/binlogstream.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pymysqlreplication/binlogstream.py b/pymysqlreplication/binlogstream.py index 8a58a365..1fd8bff0 100644 --- a/pymysqlreplication/binlogstream.py +++ b/pymysqlreplication/binlogstream.py @@ -32,6 +32,9 @@ MYSQL_EXPECTED_ERROR_CODES = [2013, 2006] +loose_version = LooseVersion("0.6") + + class ReportSlave(object): """Represent the values that you may report when connecting as a slave @@ -260,7 +263,7 @@ def _register_slave(self): packet = self.report_slave.encoded(self.__server_id) - if pymysql.__version__ < LooseVersion("0.6"): + if pymysql.__version__ < loose_version: self._stream_connection.wfile.write(packet) self._stream_connection.wfile.flush() self._stream_connection.read_packet() @@ -408,7 +411,7 @@ def __connect_to_stream(self): # encoded_data prelude += gtid_set.encoded() - if pymysql.__version__ < LooseVersion("0.6"): + if pymysql.__version__ < loose_version: self._stream_connection.wfile.write(prelude) self._stream_connection.wfile.flush() else: @@ -425,7 +428,7 @@ def fetchone(self): self.__connect_to_ctl() try: - if pymysql.__version__ < LooseVersion("0.6"): + if pymysql.__version__ < loose_version: pkt = self._stream_connection.read_packet() else: pkt = self._stream_connection._read_packet() From 79716a5d99b7ccf773313a369226f13861361246 Mon Sep 17 00:00:00 2001 From: Alf Date: Wed, 21 Apr 2021 15:28:53 +0800 Subject: [PATCH 3/4] fix int(column_type) --- pymysqlreplication/row_event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymysqlreplication/row_event.py b/pymysqlreplication/row_event.py index e0540663..9b105c93 100644 --- a/pymysqlreplication/row_event.py +++ b/pymysqlreplication/row_event.py @@ -617,7 +617,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs) 'COLUMN_TYPE': 'BLOB', # we don't know what it is, so let's not do anything with it. 'COLUMN_KEY': '', } - col = Column(column_type, column_schema, from_packet) + col = Column(int(column_type), column_schema, from_packet) self.columns.append(col) self.table_obj = Table(self.column_schemas, self.table_id, self.schema, From 8b9c39d6d8efe405d44fa75bc16541f1c6338f73 Mon Sep 17 00:00:00 2001 From: Alf Date: Mon, 26 Apr 2021 17:11:28 +0800 Subject: [PATCH 4/4] fix the byte2int import in tests. --- pymysqlreplication/row_event.py | 12 +++++++++++- pymysqlreplication/tests/binlogfilereader.py | 2 +- pymysqlreplication/tests/test_data_type.py | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pymysqlreplication/row_event.py b/pymysqlreplication/row_event.py index 9b105c93..02ab3800 100644 --- a/pymysqlreplication/row_event.py +++ b/pymysqlreplication/row_event.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import sys import struct import decimal import datetime @@ -531,6 +532,12 @@ def _dump(self): row["after_values"][key])) + +_need_byte2int = False +if sys.version.startswith('2') or sys.version.startswith('3.4'): + _need_byte2int = True + + class TableMapEvent(BinLogEvent): """This event describes the structure of a table. It's sent before a change happens on a table. @@ -594,6 +601,9 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs) self.packet.read_length_coded_binary() for i in range(0, len(column_types)): column_type = column_types[i] + if _need_byte2int: + column_type = byte2int(column_types[i]) + try: column_schema = self.column_schemas[ordinal_pos_loc] @@ -617,7 +627,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs) 'COLUMN_TYPE': 'BLOB', # we don't know what it is, so let's not do anything with it. 'COLUMN_KEY': '', } - col = Column(int(column_type), column_schema, from_packet) + col = Column(column_type, column_schema, from_packet) self.columns.append(col) self.table_obj = Table(self.column_schemas, self.table_id, self.schema, diff --git a/pymysqlreplication/tests/binlogfilereader.py b/pymysqlreplication/tests/binlogfilereader.py index c0a111c0..d127f604 100644 --- a/pymysqlreplication/tests/binlogfilereader.py +++ b/pymysqlreplication/tests/binlogfilereader.py @@ -1,7 +1,7 @@ '''Read binlog files''' import struct -from pymysql.util import byte2int +from six import byte2int from pymysqlreplication import constants from pymysqlreplication.event import FormatDescriptionEvent from pymysqlreplication.event import QueryEvent diff --git a/pymysqlreplication/tests/test_data_type.py b/pymysqlreplication/tests/test_data_type.py index 21351114..d8d07265 100644 --- a/pymysqlreplication/tests/test_data_type.py +++ b/pymysqlreplication/tests/test_data_type.py @@ -8,6 +8,7 @@ else: import unittest +import json from decimal import Decimal from pymysqlreplication.tests import base