jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[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(-)

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 change 742198. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I550b7c56fcce1a5a50f8ea6d5a0c47b781f80dc3
Gerrit-Change-Number: 742198
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: BryanDavis <bdavis@wikimedia.org>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged