diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-09-15 11:14:36 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-09-15 14:03:27 -0400 |
| commit | 339416b821ed543f289fccff8f6fc9c44dbc9d23 (patch) | |
| tree | 694b8be639abeb14c89e8101b3d8d1c2670c6abf /lib/sqlalchemy | |
| parent | 31f80b9eaeb3c3435b7f6679b41e434478b1d11c (diff) | |
| download | sqlalchemy-339416b821ed543f289fccff8f6fc9c44dbc9d23.tar.gz | |
Add __next__(), next() to ResultProxyreview/mike_bayer/ticket_4077
Added ``__next__()`` and ``next()`` methods to :class:`.ResultProxy`,
so that the ``next()`` builtin function works on the object directly.
:class:`.ResultProxy` has long had an ``__iter__()`` method which already
allows it to respond to the ``iter()`` builtin. The implementation
for ``__iter__()`` is unchanged, as performance testing has indicated
that iteration using a ``__next__()`` method with ``StopIteration``
is about 20% slower in both Python 2.7 and 3.6.
Change-Id: I70569a4c48ad85a3c21a7ad422f270a559926cfb
Fixes: #4077
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/engine/result.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 3aae932f2..79f362500 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -862,6 +862,8 @@ class ResultProxy(object): self.closed = True def __iter__(self): + """Implement iteration protocol.""" + while True: row = self.fetchone() if row is None: @@ -869,6 +871,20 @@ class ResultProxy(object): else: yield row + def __next__(self): + """Implement the next() protocol. + + .. versionadded:: 1.2 + + """ + row = self.fetchone() + if row is None: + raise StopIteration() + else: + return row + + next = __next__ + @util.memoized_property def inserted_primary_key(self): """Return the primary key for the row just inserted. |
