[Sqlalchemy-tickets] [sqlalchemy] #2726: ColumnClause pretends to be iterable and then isn't
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-05-13 23:12:45
|
#2726: ColumnClause pretends to be iterable and then isn't
--------------------+-----------------------------------------
Reporter: gthb | Owner: zzzeek
Type: defect | Status: new
Priority: medium | Milestone: 0.8.xx
Component: sql | Severity: no triage selected yet
Keywords: | Progress State: awaiting triage
--------------------+-----------------------------------------
Updating to SQLAlchemy 0.8, I'm getting regressions because
`ColumnClause.__iter__` now exists (defined in `ColumnOperators` in
r4230396c6e24) only to raise an exception. This sucker-punches code that
checks for iterableness either by `hasattr(o, '__iter__')` or
`isinstance(o, collections.Iterable)` ... such as this `VALUES` compiler
that supports tuples/lists/iterables on PostgreSQL, representing them as
`ARRAY`s:
{{{#!python
@compiles(values)
def compile_values(element, compiler, asfrom=False, **kw):
v = u"VALUES %s" % ", ".join(
u"(%s)" % u", ".join(
u'ARRAY[%s]' % ','.join(
(u"'%s'" % subel.replace(u"'", u"''").replace(u"%",
u"%%"))
if isinstance(subel, unicode)
else str(subel) if isinstance(subel, int)
else compiler.process(subel)
for subel in elem
) if hasattr(elem, '__iter__')
and elem.__iter__.__func__ is not blocking_iter_func
else (u"'%s'" % elem.replace(u"'", u"''").replace(u"%",
u"%%"))
if isinstance(elem, unicode)
else u'NULL' if elem is None
else compiler.process(elem) if isinstance(elem, ColumnElement)
else repr(elem)
for elem in tup)
for tup in element.list
)
if asfrom:
v = "(%s)" % v
return v
}}}
I get what the method is there for, but it satisfies the conventional ways
of checking for iterableness, while it's actually there to assert
''non-''iterableness. That's not very neat.
I'm provisionally working around this as a special case:
{{{#!python
from sqlalchemy.sql.operators import ColumnOperators
blocking_iter_func = ColumnOperators.__iter__.__func__
... if hasattr(elem, '__iter__') and elem.__iter__.__func__ is not
blocking_iter_func
}}}
(and the wind carries a soft murmur of kittens screaming in the
distance...)
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2726>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|