Page MenuHomePhabricator

MySQL: AttributeError: 'Cursor' object has no attribute 'cursor
Closed, ResolvedPublic

Description

The database queries in my bots, based on Pywikibot, crashes after the March changes (renamed the database replica T278252 and a few changes in Pywikibot like).
From my PC (via SSH tunnel) scripts work, but not on toolforge.org server.

To example, I run python promt on the server:

>>> from pywikibot.data import mysql

>>> sql = "SELECT * FROM page LIMIT 10;"
>>> result = mysql.mysql_query(sql, dbname='ruwiki')
>>> list(result)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/project/shared/pywikipedia/core/pywikibot/data/mysql.py", line 67, in mysql_query
    with connection as conn, conn.cursor() as cursor:
AttributeError: 'Cursor' object has no attribute 'cursor'
>>> from pywikibot import config2 as config

>>> config.db_hostname_format.format(dbname)
'ruwiki.web.db.svc.wikimedia.cloud'

>>> config.db_name_format.format(dbname)
'ruwiki_p'

>>> config.db_port
3306

>>> credentials
{'read_default_file': '/data/project/vltools/replica.my.cnf'}

Event Timeline

I updated pywikibot and pymysql and it worked.
I think, need to add check for the version of pymysql to pywikibot/data/mysql.py, and revert the code for support version <1.0.0. Or at least a notification that bots developers need to update pymysql.

Xqt triaged this task as Medium priority.Apr 9 2021, 7:31 AM

@Vladis13: What was your previous environment (Python, Pywikibot, PyMySQL releases)?

pwb.py wrapper does version checking but this is missing for PyMySQL and should be implemented.

Works for me with pymysql 0.10 and Python 3.5 as follows:

>>> import pymysql
>>> from contextlib import closing
>>> connection = pymysql.connect(defer_connect=True)
>>> connection = closing(connection)
>>> with connection as conn, conn.cursor() as cursor:
	pass

>>>

Was Pwb 3.0.dev0 (this version number did not change until this year for several years, although Pwb itself was regularly updated, probably it was a bug) → 6.0.1
PyMySQL 0.9.3 → 1.0.2
Python 3.7.

Was Pwb 3.0.dev0 (this version number did not change until this year for several years, although Pwb itself was regularly updated, probably it was a bug) → 6.0.1

Your traceback is surely Pywikibot 6.0.1 (stable). Anyway I can reproduce it:

>>> import pymysql
>>> from contextlib import closing
>>> connection = pymysql.connect(defer_connect=True)
>>> connection = closing(connection)
>>> with connection as conn, conn.cursor() as cursor:
	pass

>>> connection = pymysql.connect(defer_connect=True)
>>> with connection as conn, conn.cursor() as cursor:
	pass

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    with connection as conn, conn.cursor() as cursor:
AttributeError: 'Cursor' object has no attribute 'cursor'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#8>", line 2, in <module>
    pass
  File "C:\Python39\lib\site-packages\pymysql\connections.py", line 503, in __exit__
    self.rollback()
  File "C:\Python39\lib\site-packages\pymysql\connections.py", line 429, in rollback
    self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
  File "C:\Python39\lib\site-packages\pymysql\connections.py", line 750, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')
>>>

connection is not a context manager if pymysql < 1.0.0

Change 678221 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [bugfix] Context manager depends on pymysql version, not Python release

https://gerrit.wikimedia.org/r/678221

Change 678221 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] Context manager depends on pymysql version, not Python release

https://gerrit.wikimedia.org/r/678221