]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- util.flatten_iterator() func doesn't interpret strings with
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 Oct 2008 21:44:34 +0000 (21:44 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 Oct 2008 21:44:34 +0000 (21:44 +0000)
__iter__() methods as iterators, such as in pypy [ticket:1077].

CHANGES
lib/sqlalchemy/util.py
test/base/utils.py

diff --git a/CHANGES b/CHANGES
index b022671dc6e45c3a8e525c1dfda3525a456f08e2..54409f31b8023015fb7ae89e0a15f6b8642ba23a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -82,7 +82,11 @@ CHANGES
 
     - No longer expects include_columns in table reflection to be
       lower case.
-
+      
+- misc
+    - util.flatten_iterator() func doesn't interpret strings with
+      __iter__() methods as iterators, such as in pypy [ticket:1077].
+      
 0.5.0rc2
 ========
 - orm
index 237772a4079f36927381a0b8b9cca2b33816cd3e..01d2c738d23e3931bd9361f71c6de854c2a08307 100644 (file)
@@ -211,10 +211,10 @@ else:
 def flatten_iterator(x):
     """Given an iterator of which further sub-elements may also be
     iterators, flatten the sub-elements into a single iterator.
-    """
 
+    """
     for elem in x:
-        if hasattr(elem, '__iter__'):
+        if not isinstance(elem, basestring) and hasattr(elem, '__iter__'):
             for y in flatten_iterator(elem):
                 yield y
         else:
index a28af90ed5d2a89c9346c8d641704cdc5a388cab..c3d026f04513f479e3e047694bb08fe7e4976a78 100644 (file)
@@ -92,6 +92,21 @@ class ColumnCollectionTest(TestBase):
 class ImmutableSubclass(str):
     pass
 
+class FlattenIteratorTest(TestBase):
+    def test_flatten(self):
+        assert list(util.flatten_iterator([[1,2,3], [4,5,6], 7, 8])) == [1,2,3,4,5,6,7,8]
+
+    def test_str_with_iter(self):
+        """ensure that a str object with an __iter__ method (like in PyPy) is not interpreted
+        as an iterable.
+        
+        """
+        class IterString(str):
+            def __iter__(self):
+                return iter(self + "")
+
+        assert list(util.flatten_iterator([IterString("asdf"), [IterString("x"), IterString("y")]])) == ["asdf", "x", "y"]
+                
 class HashOverride(object):
     def __init__(self, value=None):
         self.value = value