jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/742198 )

Change subject: [bugfix]  Fix AttributeError on sock.close() on toolforge
......................................................................

[bugfix]  Fix AttributeError on sock.close() on toolforge

On toolforge an old pymysql release is installed.
THe AttributeError bug is solved with pymysql >= 0.7.11.
Backport this fix but deprecate the old package.

Bug: T216741
Change-Id: I550b7c56fcce1a5a50f8ea6d5a0c47b781f80dc3
---
M pywikibot/data/mysql.py
1 file changed, 52 insertions(+), 8 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/data/mysql.py b/pywikibot/data/mysql.py
index af8ae6e..da74f75 100644
--- a/pywikibot/data/mysql.py
+++ b/pywikibot/data/mysql.py
@@ -1,15 +1,22 @@
-"""Miscellaneous helper functions for mysql queries."""
+"""Miscellaneous helper functions for mysql queries.
+
+.. deprecated:: 7.0
+   Support of pymysql < 0.7.11
+"""
 #
 # (C) Pywikibot team, 2016-2021
 #
 # Distributed under the terms of the MIT license.
 #
+import struct
 from typing import Optional

 import pkg_resources

 import pywikibot
 from pywikibot import config
+from pywikibot.backports import removesuffix
+from pywikibot.tools import issue_deprecation_warning


 try:
@@ -18,6 +25,34 @@
     raise ImportError('MySQL python module not found. Please install PyMySQL.')

 
+COM_QUIT = 0x01
+
+
+class _OldConnection(pymysql.connections.Connection):
+
+    """Representation of a socket with a mysql server.
+
+    This class is used to patch close() method for pymysql<0.7.11 (T216741).
+
+    .. versionadded:: 7.0
+    .. deprecated:: 7.0
+       Update your pymysql package
+    """
+
+    def close(self):
+        """Send the quit message and close the socket."""
+        if self._closed or self._sock is None:
+            super().close()
+
+        send_data = struct.pack('<iB', 1, COM_QUIT)
+        try:
+            self._write_bytes(send_data)
+        except Exception:
+            pass
+        finally:
+            self._force_close()
+
+
 def mysql_query(query: str, params=None,
                 dbname: Optional[str] = None,
                 verbose: Optional[bool] = None):
@@ -54,14 +89,23 @@
     else:
         credentials = {'read_default_file': config.db_connect_file}

-    connection = pymysql.connect(host=config.db_hostname_format.format(dbname),
-                                 database=config.db_name_format.format(dbname),
-                                 port=config.db_port,
-                                 charset='utf8',
-                                 defer_connect=query == 'test',  # for tests
-                                 **credentials)
+    pymysql_version = pkg_resources.parse_version(
+        removesuffix(pymysql.__version__, '.None'))
+    args = {
+        'host': config.db_hostname_format.format(dbname),
+        'database': config.db_name_format.format(dbname),
+        'port': config.db_port,
+        'charset': 'utf8',
+        'defer_connect': query == 'test',  # for tests
+    }

-    pymysql_version = pkg_resources.parse_version(pymysql.__version__)
+    if pymysql_version < pkg_resources.parse_version('0.7.11'):
+        issue_deprecation_warning(
+            'pymysql package release {}'.format(pymysql_version),
+            instead='pymysql >= 0.7.11', since='7.0.0')
+        connection = _OldConnection(**args, **credentials)
+    else:
+        connection = pymysql.connect(**args, **credentials)

     if pymysql_version < pkg_resources.parse_version('1.0.0'):
         from contextlib import closing

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/742198
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I550b7c56fcce1a5a50f8ea6d5a0c47b781f80dc3
Gerrit-Change-Number: 742198
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: Dvorapa <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to