diff --git a/pymysqlreplication/binlogstream.py b/pymysqlreplication/binlogstream.py index 7d9d3121..1fd8bff0 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 @@ -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() 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..02ab3800 100644 --- a/pymysqlreplication/row_event.py +++ b/pymysqlreplication/row_event.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- +import sys 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 @@ -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(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, 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