The dict, set, and deque iterators do not implement tp_traverse although they reference container objects. This can lead to reference cycles which will not be collected. The attached cycle.py script from Amaury demonstrates the problem for dict iterators. The attached patch addresses the issue for the above mentioned types. The method applied in the demonstration script was used for test cases. This is my first excursion into cyclic garbage collector implementations, so please review carefully. Also, I am not sure about tp_traverse for the deque type. Must the block member also be considered or is the deque member sufficient? Finally, do you consider this a show stopper?
> This is my first excursion into cyclic garbage collector > implementations, so please review carefully. If you don't implement a tp_clear function, you should leave a comment why not. I think it's ok, since the underlying containers will get cleared, thus breaking the cycle. > Also, I am not sure about > tp_traverse for the deque type. Must the block member also be considered > or is the deque member sufficient? It is fine as-is. The iterator doesn't own the reference to the block objects, and traversing the deque will also traverse all contained objects. > Finally, do you consider this a show stopper? Not me. As-is, this bug doesn't cause crashes, and can be worked-around in applications (by explicitly breaking the cycle).
> I think it's ok, since the underlying containers will get cleared, thus > breaking the cycle. What about the dictiter object which references a tuple (di_result)? Tuple does not implement tp_clear, but OTOH tuples are immutable and di_result cannot be assigned.