]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where :meth:`.in_()` would go into an endless loop if
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 13 Feb 2014 20:19:12 +0000 (15:19 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 13 Feb 2014 20:19:12 +0000 (15:19 -0500)
erroneously passed a column expression whose comparator included
the ``__getitem__()`` method, such as a column that uses the
:class:`.postgresql.ARRAY` type. [ticket:2957]

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/sql/default_comparator.py
test/sql/test_operators.py

index 860a18a1ee4d6736e9bf59340a9dacf835162cfa..e45abad56196336fc9aaf5deb3749d3eda478f50 100644 (file)
 .. changelog::
     :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
index c39dce9c60d33e45ce6a3832e3f11fdb391cdd73..6d595450be3d7415cb13d8bea17046dc4bce1155 100644 (file)
@@ -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()
index 79b0a717b4c0ef745023011d25c1194bfe80c016..d46a9fbd2938442329cce61fd5668f86a824c675 100644 (file)
@@ -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"