diff options
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/orm/collections.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/sqlalchemy/orm/collections.py b/lib/sqlalchemy/orm/collections.py index 2a02eb8ff..e08a4b8b7 100644 --- a/lib/sqlalchemy/orm/collections.py +++ b/lib/sqlalchemy/orm/collections.py @@ -402,6 +402,21 @@ def collection_adapter(collection): return getattr(collection, '_sa_adapter', None) +def collection_iter(collection): + """Iterate over an object supporting the @iterator or __iter__ protocols. + + If the collection is an ORM collection, it need not be attached to an + object to be iterable. + """ + + try: + return getattr(collection, '_sa_iterator', + getattr(collection, '__iter__'))() + except AttributeError: + raise TypeError("'%s' object is not iterable" % + type(collection).__name__) + + class CollectionAdapter(object): """Bridges between the ORM and arbitrary Python collections. |