New submission from Mark Summerfield:

In the attached file there are two tiny functions, flatten1() and
flatten2(). There is only one difference between them:
flatten1() has the line:
    if isinstance(x, list):
and flatten2() has this line instead:
    if isinstance(x, collections.Sequence):

flatten1() works perfectly in Python 2.5.1 and Python 30a2 (just comment
out the flatten2() code).
But flatten2() goes into "infinite" recursion in Python 30a2 when trying
to flatten list "c".

----------
components: Interpreter Core
files: bug.py
messages: 58536
nosy: mark
severity: normal
status: open
title: infinite recursion when using collections.Sequence in a recursive 
function (Py30a2)
type: behavior
versions: Python 3.0
Added file: http://bugs.python.org/file8939/bug.py

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1612>
__________________________________
import collections

def flatten1(seq):
    """Returns a list containing all the items in seq with no nested
    sequences

    >>> a = [[1, 2], [3, [4, [5, 6], 7], 8], 9, [10, 11], [12]]
    >>> b =flatten1(a)
    >>> b
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    >>> flatten1(b) == b
    True
    >>> c = [["S"] * 4, ["M"] * 2, "R", "H", ["D"] * 3, "A", "B", "E"]
    >>> flatten1(c)
    ['S', 'S', 'S', 'S', 'M', 'M', 'R', 'H', 'D', 'D', 'D', 'A', 'B', 'E']
    """
    result = []
    for x in seq:
        if isinstance(x, list):
            result.extend(flatten1(x))
        else:
            result.append(x)
    return result


def flatten2(seq):
    """Returns a list containing all the items in seq with no nested
    sequences

    >>> a = [[1, 2], [3, [4, [5, 6], 7], 8], 9, [10, 11], [12]]
    >>> b =flatten2(a)
    >>> b
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    >>> flatten2(b) == b
    True
    >>> c = [["S"] * 4, ["M"] * 2, "R", "H", ["D"] * 3, "A", "B", "E"]
    >>> flatten2(c)
    ['S', 'S', 'S', 'S', 'M', 'M', 'R', 'H', 'D', 'D', 'D', 'A', 'B', 'E']
    """
    result = []
    for x in seq:
        if isinstance(x, collections.Sequence):
            result.extend(flatten2(x))
        else:
            result.append(x)
    return result


if __name__ == "__main__":
    import doctest
    doctest.testmod()
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to