Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make Cursor an iterator #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pymysql/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ def _do_get_result(self):
self._rows = result.rows

def __iter__(self):
return iter(self.fetchone, None)
return self

def __next__(self):
row = self.fetchone()
if row is None:
raise StopIteration
return row

Warning = err.Warning
Error = err.Error
Expand Down Expand Up @@ -459,9 +465,6 @@ def fetchall_unbuffered(self):
"""
return iter(self.fetchone, None)

def __iter__(self):
return self.fetchall_unbuffered()

def fetchmany(self, size=None):
"""Fetch many."""
self._check_executed()
Expand Down
8 changes: 8 additions & 0 deletions pymysql/tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def setUp(self):
self.test_connection = pymysql.connect(**self.databases[0])
self.addCleanup(self.test_connection.close)

def test_cursor_is_iterator(self):
"""Test that the cursor is an iterator"""
conn = self.test_connection
cursor = conn.cursor()
cursor.execute("select * from test")
self.assertEqual(cursor.__iter__(), cursor)
self.assertEqual(cursor.__next__(), ("row1",))

def test_cleanup_rows_unbuffered(self):
conn = self.test_connection
cursor = conn.cursor(pymysql.cursors.SSCursor)
Expand Down