8000 Cursor.fetchall() always return list. (#1115) · PyMySQL/PyMySQL@bfbc6a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfbc6a5

Browse files
authored
Cursor.fetchall() always return list. (#1115)
Cursor.fetchmany() returns empty tuple when exhausted all rows. It is for Django compatibility. Fix #1042.
1 parent 9228700 commit bfbc6a5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pymysql/cursors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ def fetchmany(self, size=None):
282282
"""Fetch several rows."""
283283
self._check_executed()
284284
if self._rows is None:
285+
# Django expects () for EOF.
286+
# https://github.com/django/django/blob/0c1518ee429b01c145cf5b34eab01b0b92f8c246/django/db/backends/mysql/features.py#L8
285287
return ()
286288
end = self.rownumber + (size or self.arraysize)
287289
result = self._rows[self.rownumber : end]
@@ -292,7 +294,7 @@ def fetchall(self):
292294
"""Fetch all the rows."""
293295
self._check_executed()
294296
if self._rows is None:
295-
return ()
297+
return []
296298
if self.rownumber:
297299
result = self._rows[self.rownumber :]
298300
else:
@@ -479,6 +481,10 @@ def fetchmany(self, size=None):
479481
break
480482
rows.append(row)
481483
self.rownumber += 1
484+
if not rows:
485+
# Django expects () for EOF.
486+
# https://github.com/django/django/blob/0c1518ee429b01c145cf5b34eab01b0b92f8c246/django/db/backends/mysql/features.py#L8
487+
return ()
482488
return rows
483489

484490
def scroll(self, value, mode="relative"):

pymysql/tests/test_nextset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_nextset_error(self):
3838
self.assertEqual([(i,)], list(cur))
3939
with self.assertRaises(pymysql.ProgrammingError):
4040
cur.nextset()
41-
self.assertEqual((), cur.fetchall())
41+
self.assertEqual([], cur.fetchall())
4242

4343
def test_ok_and_next(self):
4444
cur = self.connect(client_flag=CLIENT.MULTI_STATEMENTS).cursor()

0 commit comments

Comments
 (0)
0