Skip to content

Commit

Permalink
[bugfix] Add support for PyMySQL 1.0.0+
Browse files Browse the repository at this point in the history
- pymysql.Connection() postionial parameter order has been changed
  several times as is dopped since 1.0 where keyword only parameters
  are required. Therefore always use keyword parameters but use the
  renamed 'database' instead of 'db', 'password' instead 'passwd';
  the latter are deprecated.
- Require PyMySQ >= 0.6.6 due to Cursor context manager support;
  Python 3.5 is dropped with 1.0.0
- Require PyMySQ >= 1.0.0 due to Connection context manager support
- use contextlib.closing() context manager for PyMySQ < 1.0.0

See:
PyMySQL/PyMySQL@26a1370

Change-Id: Ida4e5edf2920b4302ff89ec1f4ea1e4e0a1da74d
  • Loading branch information
xqt committed Mar 19, 2021
1 parent f006365 commit 6a52075
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
19 changes: 11 additions & 8 deletions pywikibot/data/mysql.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Miscellaneous helper functions for mysql queries."""
#
# (C) Pywikibot team, 2016-2020
# (C) Pywikibot team, 2016-2021
#
# Distributed under the terms of the MIT license.
#
from contextlib import closing
from typing import Optional

import pywikibot
Expand All @@ -16,7 +15,7 @@


from pywikibot import config2 as config
from pywikibot.tools import deprecated_args
from pywikibot.tools import deprecated_args, PYTHON_VERSION


@deprecated_args(encoding=None)
Expand Down Expand Up @@ -52,16 +51,20 @@ def mysql_query(query: str, params=None,

if config.db_connect_file is None:
credentials = {'user': config.db_username,
'passwd': config.db_password}
'password': config.db_password}
else:
credentials = {'read_default_file': config.db_connect_file}

with closing(pymysql.connect(config.db_hostname_format.format(dbname),
db=config.db_name_format.format(dbname),
connection = pymysql.connect(host=config.db_hostname_format.format(dbname),
database=config.db_name_format.format(dbname),
port=config.db_port,
charset='utf8',
**credentials)) as conn, \
closing(conn.cursor()) as cursor:
**credentials)
if PYTHON_VERSION < (3, 6):
from contextlib import closing
connection = closing(connection)

with connection as conn, conn.cursor() as cursor:

if verbose:
_query = cursor.mogrify(query, params)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ sseclient >= 0.0.18,!=0.0.23,!=0.0.24
mwparserfromhell>=0.3.3

# The mysql generator in pagegenerators depends on PyMySQL
PyMySQL
PyMySQL >= 0.6.6, < 1.0.0 ; python_version < '3.6'
PyMySQL >= 1.0.0 ; python_version >= '3.6'

# core HTML comparison parser in diff module
beautifulsoup4
Expand Down

0 comments on commit 6a52075

Please sign in to comment.