]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed a gotcha where inadvertently calling list() on a
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 18 Nov 2012 15:38:02 +0000 (10:38 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 18 Nov 2012 15:38:02 +0000 (10:38 -0500)
      :class:`.ColumnElement` would go into an endless loop, if
      :meth:`.ColumnOperators.__getitem__` were implemented.
      A new NotImplementedError is emitted via ``__iter__()``.

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/sql/operators.py
test/dialect/test_postgresql.py
test/sql/test_operators.py

index 1fd8719f262a00788ff29423381536df48543708..15206134e0d1c6075ce0cfdee6bec31f19561b93 100644 (file)
@@ -7,13 +7,21 @@
     :version: 0.8.0b2
 
     .. change::
-        :tags: orm, extensions
+        :tags: sql, bug
+
+      Fixed a gotcha where inadvertently calling list() on a
+      :class:`.ColumnElement` would go into an endless loop, if
+      :meth:`.ColumnOperators.__getitem__` were implemented.
+      A new NotImplementedError is emitted via ``__iter__()``.
+
+    .. change::
+        :tags: orm, extensions, feature
 
       The :mod:`sqlalchemy.ext.mutable` extension now includes the
       example :class:`.MutableDict` class as part of the extension.
 
     .. change::
-        :tags: postgresql, hstore
+        :tags: postgresql, feature
         :tickets: 2606
 
       :class:`.HSTORE` is now available in the Postgresql dialect.
index 88f56e5ec02c88f16c5ebe211bfcb26bd2b5ee3d..7513c0b82c52cf93f5ba4e42ec79bcbb334e9dbc 100644 (file)
@@ -310,6 +310,11 @@ class ColumnOperators(Operators):
         """
         return self.operate(neg)
 
+    def __iter__(self):
+        """Block calls to list() from calling __getitem__() endlessly."""
+
+        raise NotImplementedError("Class %s is not iterable" % self.__class__)
+
     def __getitem__(self, index):
         """Implement the [] operator.
 
index 40d8d0c79b3155b0a8aeeab1bbb02fc14cd18da8..ab3f54a66abd1e7063ae734097ae0486a0fd91c3 100644 (file)
@@ -2941,7 +2941,8 @@ class HStoreTest(fixtures.TestBase):
 
     def test_cols_keys(self):
         self._test_cols(
-            self.hashcol.keys(),
+            # hide from 2to3
+            getattr(self.hashcol, 'keys')(),
             "akeys(test_table.hash) AS akeys_1",
             True
         )
index eb28856582bdf99e32050388659c2f1918040533..9da9d94c320dfbeec8a7bfdbba70fdcf9c8a496c 100644 (file)
@@ -325,6 +325,18 @@ class ExtensionOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             "x -> :x_1"
         )
 
+    def test_no_endless_list_call(self):
+        class MyType(UserDefinedType):
+            class comparator_factory(UserDefinedType.Comparator):
+                def __getitem__(self, index):
+                    return self.op("->")(index)
+
+        assert_raises_message(
+            NotImplementedError,
+            "Class <class 'sqlalchemy.schema.Column'> is not iterable",
+            list, Column('x', MyType())
+        )
+
     def test_lshift(self):
         class MyType(UserDefinedType):
             class comparator_factory(UserDefinedType.Comparator):