Bug #117548 mysql/connector python fetchmany() has an off by one bug when argument given as 1
Submitted: 23 Feb 23:07 Modified: 24 Feb 7:48
Reporter: Mike Bayer Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / Python Severity:S1 (Critical)
Version:9.2.0 OS:Any
Assigned to: CPU Architecture:Any

[23 Feb 23:07] Mike Bayer
Description:
Re-evaluating mysql-connector-python to be supported in SQLAlchemy, using a server side cursor fetchmany(1) never returns an empty list.  Run demonstration program below

might have appeared with bz#97830

How to repeat:
from mysql import connector

connection = connector.connect(user="scott", password="tiger", host="mysql80", database="test")
cursor = connection.cursor(buffered=False)

cursor.execute("Drop table if exists test")
cursor.execute("create table test (id integer)")
cursor.execute("insert into test (id) values (1), (2), (3), (4), (5)")

cursor.execute("select * from test")
while True:
    many = cursor.fetchmany(2)
    print(f"Fetchmany(2): {many}")
    if not many:
        break

cursor.execute("select * from test")
while True:
    many = cursor.fetchmany(1)
    print(f"Fetchmany(1): {many}")
    if not many:
        break

for the fetchmany(2) case, output is correct:

Fetchmany(2): [(1,), (2,)]
Fetchmany(2): [(3,), (4,)]
Fetchmany(2): [(5,)]
Fetchmany(2): []

for the fetchmany(1) case, it gets stuck on [(2,)] and never exits:

Fetchmany(1): [(1,)]
Fetchmany(1): [(2,)]
Fetchmany(1): [(2,)]
Fetchmany(1): [(2,)]
Fetchmany(1): [(2,)]

...

turning on buffered mode also resolves, then we get the correct output

Fetchmany(2): [(1,), (2,)]
Fetchmany(2): [(3,), (4,)]
Fetchmany(2): [(5,)]
Fetchmany(2): []
Fetchmany(1): [(1,)]
Fetchmany(1): [(2,)]
Fetchmany(1): [(3,)]
Fetchmany(1): [(4,)]
Fetchmany(1): [(5,)]
Fetchmany(1): []

The above program continuously returns [(2, )]
[24 Feb 7:48] MySQL Verification Team
Hello Mike Bayer,

Thank you for the report and test case.
Verified as described.

regards,
Umesh