diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-02-13 15:19:12 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-02-13 15:19:12 -0500 |
| commit | 036cb93abfb44f4ab7fdb125eaaf2597a95a0187 (patch) | |
| tree | 2be5b3a5cb535d6397979dd479ea94ed7a38414d | |
| parent | 4a79bc578c67297c707a00d8fafaba533e2833d9 (diff) | |
| download | sqlalchemy-036cb93abfb44f4ab7fdb125eaaf2597a95a0187.tar.gz | |
- Fixed bug where :meth:`.in_()` would go into an endless loop if
erroneously passed a column expression whose comparator included
the ``__getitem__()`` method, such as a column that uses the
:class:`.postgresql.ARRAY` type. [ticket:2957]
| -rw-r--r-- | doc/build/changelog/changelog_08.rst | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/default_comparator.py | 10 | ||||
| -rw-r--r-- | test/sql/test_operators.py | 33 |
3 files changed, 50 insertions, 3 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 860a18a1e..e45abad56 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -12,6 +12,16 @@ :version: 0.8.5 .. change:: + :tags: bug, sql + :tickets: 2957 + :versions: 0.9.3 + + Fixed bug where :meth:`.in_()` would go into an endless loop if + erroneously passed a column expression whose comparator included + the ``__getitem__()`` method, such as a column that uses the + :class:`.postgresql.ARRAY` type. + + .. change:: :tags: bug, orm :tickets: 2951 :versions: 0.9.3 diff --git a/lib/sqlalchemy/sql/default_comparator.py b/lib/sqlalchemy/sql/default_comparator.py index c39dce9c6..6d595450b 100644 --- a/lib/sqlalchemy/sql/default_comparator.py +++ b/lib/sqlalchemy/sql/default_comparator.py @@ -13,7 +13,7 @@ from . import type_api from .elements import BindParameter, True_, False_, BinaryExpression, \ Null, _const_expr, _clause_element_as_expr, \ ClauseList, ColumnElement, TextClause, UnaryExpression, \ - collate, _is_literal, _literal_as_text + collate, _is_literal, _literal_as_text, ClauseElement from .selectable import SelectBase, Alias, Selectable, ScalarSelect class _DefaultColumnComparator(operators.ColumnOperators): @@ -146,14 +146,18 @@ class _DefaultColumnComparator(operators.ColumnOperators): elif isinstance(seq_or_selectable, (Selectable, TextClause)): return self._boolean_compare(expr, op, seq_or_selectable, negate=negate_op, **kw) + elif isinstance(seq_or_selectable, ClauseElement): + raise exc.InvalidRequestError('in_() accepts' + ' either a list of expressions ' + 'or a selectable: %r' % seq_or_selectable) # Handle non selectable arguments as sequences args = [] for o in seq_or_selectable: if not _is_literal(o): if not isinstance(o, operators.ColumnOperators): - raise exc.InvalidRequestError('in() function accept' - 's either a list of non-selectable values, ' + raise exc.InvalidRequestError('in_() accepts' + ' either a list of expressions ' 'or a selectable: %r' % o) elif o is None: o = Null() diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 79b0a717b..d46a9fbd2 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -153,6 +153,39 @@ class DefaultColumnComparatorTest(fixtures.TestBase): ) self._loop_test(operators.notin_op, [1, 2, 3]) + def test_in_no_accept_list_of_non_column_element(self): + left = column('left') + foo = ClauseList() + assert_raises_message( + exc.InvalidRequestError, + r"in_\(\) accepts either a list of expressions or a selectable:", + left.in_, [foo] + ) + + def test_in_no_accept_non_list_non_selectable(self): + left = column('left') + right = column('right') + assert_raises_message( + exc.InvalidRequestError, + r"in_\(\) accepts either a list of expressions or a selectable:", + left.in_, right + ) + + def test_in_no_accept_non_list_thing_with_getitem(self): + # test [ticket:2726] + class HasGetitem(String): + class comparator_factory(String.Comparator): + def __getitem__(self, value): + return value + + left = column('left') + right = column('right', HasGetitem) + assert_raises_message( + exc.InvalidRequestError, + r"in_\(\) accepts either a list of expressions or a selectable:", + left.in_, right + ) + def test_collate(self): left = column('left') right = "some collation" |
