Skip to content

Commit 8c1c602

Browse files
authored
Make Cursor an iterator
1 parent 0a9621d commit 8c1c602

1,170 files changed

Lines changed: 425022 additions & 47 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/MySQLdb/cursors.py‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from ._exceptions import ProgrammingError
1010

11-
1211
#: Regular expression for ``Cursor.executemany```.
1312
#: executemany only supports simple bulk insert.
1413
#: You can use it to load large dataset.
@@ -329,7 +328,13 @@ def _fetch_row(self, size=1):
329328
return self._result.fetch_row(size, self._fetch_type)
330329

331330
def __iter__(self):
332-
return iter(self.fetchone, None)
331+
return self
332+
333+
def __next__(self):
334+
row = self.fetchone()
335+
if row is None:
336+
raise StopIteration
337+
return row
333338

334339
def __getattr__(self, name):
335340
# DB-API 2.0 optional extension says these errors can be accessed
@@ -419,11 +424,6 @@ def scroll(self, value, mode="relative"):
419424
raise IndexError("out of range")
420425
self.rownumber = r
421426

422-
def __iter__(self):
423-
self._check_executed()
424-
result = self.rownumber and self._rows[self.rownumber :] or self._rows
425-
return iter(result)
426-
427427

428428
class CursorUseResultMixIn:
429429
"""This is a MixIn class which causes the result set to be stored
@@ -464,17 +464,6 @@ def fetchall(self):
464464
self.rownumber = self.rownumber + len(r)
465465
return r
466466

467-
def __iter__(self):
468-
return self
469-
470-
def next(self):
471-
row = self.fetchone()
472-
if row is None:
473-
raise StopIteration
474-
return row
475-
476-
__next__ = next
477-
478467

479468
class CursorTupleRowsMixIn:
480469
"""This is a MixIn class that causes all rows to be returned as tuples,

‎tests/test_cursor.py‎

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from MySQLdb.constants import ER
44
from configdb import connection_factory
55

6-
76
_conns = []
87
_tables = []
98

@@ -57,9 +56,9 @@ def test_executemany():
5756
"INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s) ON duplicate update"
5857
)
5958
assert m is not None, "error parse %(id_name)s"
60-
assert m.group(3) == " ON duplicate update", (
61-
"group 3 not ON duplicate update, bug in RE_INSERT_VALUES?"
62-
)
59+
assert (
60+
m.group(3) == " ON duplicate update"
61+
), "group 3 not ON duplicate update, bug in RE_INSERT_VALUES?"
6362

6463
# https://github.com/PyMySQL/mysqlclient-python/issues/178
6564
m = MySQLdb.cursors.RE_INSERT_VALUES.match(
@@ -75,31 +74,29 @@ def test_executemany():
7574
# list args
7675
data = [(i,) for i in range(10)]
7776
cursor.executemany("insert into test (data) values (%s)", data)
78-
assert cursor._executed.endswith(b",(7),(8),(9)"), (
79-
"execute many with %s not in one query"
80-
)
77+
assert cursor._executed.endswith(
78+
b",(7),(8),(9)"
79+
), "execute many with %s not in one query"
8180

8281
# dict args
8382
data_dict = [{"data": i} for i in range(10)]
8483
cursor.executemany("insert into test (data) values (%(data)s)", data_dict)
85-
assert cursor._executed.endswith(b",(7),(8),(9)"), (
86-
"execute many with %(data)s not in one query"
87-
)
84+
assert cursor._executed.endswith(
85+
b",(7),(8),(9)"
86+
), "execute many with %(data)s not in one query"
8887

8988
# %% in column set
90-
cursor.execute(
91-
"""\
89+
cursor.execute("""\
9290
CREATE TABLE percent_test (
9391
`A%` INTEGER,
94-
`B%` INTEGER)"""
95-
)
92+
`B%` INTEGER)""")
9693
try:
9794
q = "INSERT INTO percent_test (`A%%`, `B%%`) VALUES (%s, %s)"
9895
assert MySQLdb.cursors.RE_INSERT_VALUES.match(q) is not None
9996
cursor.executemany(q, [(3, 4), (5, 6)])
100-
assert cursor._executed.endswith(b"(3, 4),(5, 6)"), (
101-
"executemany with %% not in one query"
102-
)
97+
assert cursor._executed.endswith(
98+
b"(3, 4),(5, 6)"
99+
), "executemany with %% not in one query"
103100
finally:
104101
cursor.execute("DROP TABLE IF EXISTS percent_test")
105102

@@ -195,27 +192,23 @@ def test_cursor_discard_result(Cursor):
195192
conn = connect()
196193
cursor = conn.cursor(Cursor)
197194

198-
cursor.execute(
199-
"""\
195+
cursor.execute("""\
200196
CREATE TABLE test_cursor_discard_result (
201197
id INTEGER PRIMARY KEY AUTO_INCREMENT,
202198
data VARCHAR(100)
203-
)"""
204-
)
199+
)""")
205200
_tables.append("test_cursor_discard_result")
206201

207202
cursor.executemany(
208203
"INSERT INTO test_cursor_discard_result (id, data) VALUES (%s, %s)",
209204
[(i, f"row {i}") for i in range(1, 101)],
210205
)
211206

212-
cursor.execute(
213-
"""\
207+
cursor.execute("""\
214208
SELECT * FROM test_cursor_discard_result WHERE id <= 10;
215209
SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 11 AND 20;
216210
SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 21 AND 30;
217-
"""
218-
)
211+
""")
219212
cursor.nextset()
220213
assert cursor.fetchone() == (11, "row 11")
221214

@@ -231,14 +224,12 @@ def test_binary_prefix():
231224
cursor = conn.cursor()
232225

233226
cursor.execute("DROP TABLE IF EXISTS test_binary_prefix")
234-
cursor.execute(
235-
"""\
227+
cursor.execute("""\
236228
CREATE TABLE test_binary_prefix (
237229
id INTEGER NOT NULL AUTO_INCREMENT,
238230
json JSON NOT NULL,
239231
PRIMARY KEY (id)
240-
) CHARSET=utf8mb4"""
241-
)
232+
) CHARSET=utf8mb4""")
242233

243234
cursor.executemany(
244235
"INSERT INTO test_binary_prefix (id, json) VALUES (%(id)s, %(json)s)",
@@ -284,3 +275,27 @@ def test_sscursor_warning_count():
284275
rows = cursor.fetchmany(2)
285276
assert len(rows) == 1
286277
assert cursor.warning_count == 1
278+
279+
280+
@pytest.mark.parametrize("Cursor", [MySQLdb.cursors.Cursor, MySQLdb.cursors.SSCursor])
281+
def test_cursor_is_iterator(Cursor):
282+
conn = connect()
283+
cursor = conn.cursor(Cursor)
284+
285+
cursor.execute("DROP TABLE IF EXISTS test_cursor_is_iterator")
286+
cursor.execute(
287+
"CREATE TABLE test_cursor_is_iterator (id INT PRIMARY KEY, name VARCHAR(20))"
288+
)
289+
_tables.append("test_cursor_is_iterator")
290+
cursor.executemany(
291+
"INSERT INTO test_cursor_is_iterator (id, name) VALUES (%s, %s)",
292+
[(1, "a"), (2, "b"), (3, "c")],
293+
)
294+
295+
cursor.execute("SELECT name FROM test_cursor_is_iterator ORDER BY id")
296+
assert iter(cursor) is cursor
297+
assert next(cursor) == ("a",)
298+
assert next(cursor) == ("b",)
299+
assert next(cursor) == ("c",)
300+
with pytest.raises(StopIteration):
301+
next(cursor)

0 commit comments

Comments
 (0)