From 27a6e48fce1793eeaed41677601f96a4ebf35a11 Mon Sep 17 00:00:00 2001 From: Eli Kirmayer Date: Mon, 18 Dec 2023 14:04:15 -0800 Subject: [PATCH 1/2] Refactoring version checks to be a static variable Currently, the code has multiple instances where the pymysql version is checked inline with a vall to Version.py. This is unlikely to change during the duration of the script running so we can optimize by storing this in a variable once. --- pymysqlreplication/binlogstream.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pymysqlreplication/binlogstream.py b/pymysqlreplication/binlogstream.py index c4c3efb3..e6386638 100644 --- a/pymysqlreplication/binlogstream.py +++ b/pymysqlreplication/binlogstream.py @@ -51,6 +51,7 @@ # 2006 MySQL server has gone away MYSQL_EXPECTED_ERROR_CODES = [2013, 2006] +PYMYSQL_VERSION_LT_06 = Version(pymysql.__version__) < Version("0.6") class ReportSlave(object): """Represent the values that you may report when connecting as a slave @@ -330,7 +331,7 @@ def _register_slave(self): packet = self.report_slave.encoded(self.__server_id) - if Version(pymysql.__version__) < Version("0.6"): + if PYMYSQL_VERSION_LT_06: self._stream_connection.wfile.write(packet) self._stream_connection.wfile.flush() self._stream_connection.read_packet() @@ -502,7 +503,7 @@ def __connect_to_stream(self): # encoded_data prelude += gtid_set.encoded() - if Version(pymysql.__version__) < Version("0.6"): + if PYMYSQL_VERSION_LT_06: self._stream_connection.wfile.write(prelude) self._stream_connection.wfile.flush() else: @@ -588,7 +589,7 @@ def fetchone(self): self.__connect_to_ctl() try: - if Version(pymysql.__version__) < Version("0.6"): + if PYMYSQL_VERSION_LT_06: pkt = self._stream_connection.read_packet() else: pkt = self._stream_connection._read_packet() From dd5ae000555335acbf2b80ae6be88ffe0df81566 Mon Sep 17 00:00:00 2001 From: Eli Kirmayer Date: Mon, 18 Dec 2023 14:06:44 -0800 Subject: [PATCH 2/2] Adding whitespace according to black --- pymysqlreplication/binlogstream.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pymysqlreplication/binlogstream.py b/pymysqlreplication/binlogstream.py index e6386638..061a345e 100644 --- a/pymysqlreplication/binlogstream.py +++ b/pymysqlreplication/binlogstream.py @@ -53,6 +53,7 @@ PYMYSQL_VERSION_LT_06 = Version(pymysql.__version__) < Version("0.6") + class ReportSlave(object): """Represent the values that you may report when connecting as a slave to a master. SHOW SLAVE HOSTS related"""