find(): if table doesn't exist, return an iterator

... rather than []

If a table exists, you can treat the output of `find` as an iterator,
specifically you can call `next(find(...))`:

```
>>> db = dataset.connect('sqlite:///:memory1:')
>>> table = db['table_which_exists']
>>> table.insert({'foo': 'x'})
>>> next(table.find(foo='x'))  # no problem
```

But if the table hasn't been created yet, `find(...)` returns an empty list,
which you can't call `next()` on:

```
>>> db = dataset.connect('sqlite:///:memory2:')
>>> table = db_no_table['table_which_doesnt_exis']
>>> next(table.find(foo='x'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not an iterator
```

^^^ rather than a `TypeError`, I'd expect to get a `StopIteration`
exception.
This commit is contained in:
Paul M Furley 2018-05-29 17:54:18 +01:00
parent aa5dad5b80
commit c414f85df9
No known key found for this signature in database
GPG Key ID: 0AC6AD63E8E8A9B0

View File

@ -446,7 +446,7 @@ class Table(object):
instead. instead.
""" """
if not self.exists: if not self.exists:
return [] return iter([])
_limit = kwargs.pop('_limit', None) _limit = kwargs.pop('_limit', None)
_offset = kwargs.pop('_offset', 0) _offset = kwargs.pop('_offset', 0)