8000 Make Cursor an iterator (#995) · PyMySQL/PyMySQL@a6f53db · GitHub
[go: up one dir, main page]

Skip to content

Commit a6f53db

Browse files
sanchezgGonzalo Sanchezmethane
authored
Make Cursor an iterator (#995)
Fix #992 Co-authored-by: Gonzalo Sanchez <gsanchez@shiphero.com> Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent 3cd76d7 commit a6f53db

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pymysql/cursors.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,13 @@ def _do_get_result(self):
342342
self._rows = result.rows
343343

344344
def __iter__(self):
345-
return iter(self.fetchone, None)
345+
return self
346+
347+
def __next__(self):
348+
row = self.fetchone()
349+
if row is None:
350+
raise StopIteration
351+
return row
346352

347353
Warning = err.Warning
348354
Error = err.Error
@@ -459,9 +465,6 @@ def fetchall_unbuffered(self):
459465
"""
460466
return iter(self.fetchone, None)
461467

462-
def __iter__(self):
463-
return self.fetchall_unbuffered()
464-
465468
def fetchmany(self, size=None):
466469
"""Fetch many."""
467470
self._check_executed()

pymysql/tests/test_cursor.py

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def setUp(self):
2525
self.test_connection = pymysql.connect(**self.databases[0])
2626
self.addCleanup(self.test_connection.close)
2727

28+
def test_cursor_is_iterator(self):
29+
"""Test that the cursor is an iterator"""
30+
conn = self.test_connection
31+
cursor = conn.cursor()
32+
cursor.execute("select * from test")
33+
self.assertEqual(cursor.__iter__(), cursor)
34+
self.assertEqual(cursor.__next__(), ("row1",))
35+
2836
def test_cleanup_rows_unbuffered(self):
2937
conn = self.test_connection
3038
cursor = conn.cursor(pymysql.cursors.SSCursor)

0 commit comments

Comments
 (0)
0