def print_cycles(objects, outstream=sys.stdout, show_progress=False):
import gc<EOL>from types import FrameType<EOL>def print_path(path):<EOL><INDENT>for i, step in enumerate(path):<EOL><INDENT>next = path[(i + <NUM_LIT:1>) % len(path)]<EOL>outstream.write("<STR_LIT>" % str(type(step)))<EOL>if isinstance(step, dict):<EOL><INDENT>for key, val in list(step.items()):<EOL><INDENT>if val is next:<EOL><INDENT>outstream.write("<STR_LIT>" % repr(key))<EOL>break<EOL><DEDENT>if key is next:<EOL><INDENT>outstream.write("<STR_LIT>" % repr(val))<EOL>break<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(step, list):<EOL><INDENT>outstream.write("<STR_LIT>" % step.index(next))<EOL><DEDENT>elif isinstance(step, tuple):<EOL><INDENT>outstream.write("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>outstream.write(repr(step))<EOL><DEDENT>outstream.write("<STR_LIT>")<EOL><DEDENT>outstream.write("<STR_LIT:\n>")<EOL><DEDENT>def recurse(obj, start, all, current_path):<EOL><INDENT>if show_progress:<EOL><INDENT>outstream.write("<STR_LIT>" % len(all))<EOL><DEDENT>all[id(obj)] = None<EOL>referents = gc.get_referents(obj)<EOL>for referent in referents:<EOL><INDENT>if referent is start:<EOL><INDENT>print_path(current_path)<EOL><DEDENT>elif referent is objects or isinstance(referent, FrameType):<EOL><INDENT>continue<EOL><DEDENT>elif id(referent) not in all:<EOL><INDENT>recurse(referent, start, all, current_path + [obj])<EOL><DEDENT><DEDENT><DEDENT>for obj in objects:<EOL><INDENT>outstream.write("<STR_LIT>" % (obj,))<EOL>recurse(obj, obj, { }, [])<EOL><DEDENT>
*objects*
    A list of objects to find cycles in.  It is often useful to
    pass in gc.garbage to find the cycles that are preventing some
    objects from being garbage collected.

*outstream*
    The stream for output.

*show_progress*
    If True, print the number of objects reached as they are found.