Hello,

2008/8/25 Robert Schuppenies <[EMAIL PROTECTED]>:
> Hi.
>
> Could you please explain to me why some iterators have a tp_traverse
> implementation and others do not? For example tupleiterator has one,
> but none of the dict iterators. Same for set iterators (and possibly
> others). It shows in Python when you use the get_referents function.
>
>>>> t = (1,2,3)
>>>> gc.get_referents(iter(t))
> [(1, 2, 3)]
>>>> s = set([1,2,3])
>>>> gc.get_referents(iter(s))
> []
>>>> d = {1:1, 2:2}
>>>> gc.get_referents(iter(d))
> []
>
> And is it correct that you can rephrase the question to 'why some
> iterators are seen as container objects and others are not'?

Yes, this can lead to some object cycle that are not collected.
See the attached script: a cycle involving a list iterator is
collected by the garbage collector, but a cycle with a dict iterator
is not.
This is worth a bug report IMO.

-- 
Amaury Forgeot d'Arc
import gc, weakref

def test_container_iterator(i):
    class C(object):
        pass
    obj = C()
    ref = weakref.ref(obj)

    if i == 1:
        container = (obj,)
    elif i==2:
        container = [obj,]
    elif i==3:
        container = {obj: 1}
    elif i==4:
        container = {1: obj}

    print "Container:", container,

    obj.x = iter(container)

    del obj, container
    gc.collect()

    if ref() is None:
        print "OK"
    else:
        print "Cycle was not collected!!"
        # Break the cycle
        del ref().x

    # Now it is.
    assert ref() is None
    
test_container_iterator(1)
test_container_iterator(2)
test_container_iterator(3)
test_container_iterator(4)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to