# Buggy code
def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc):
    """Inline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    """
    tree = tree.deepcopy()
    for toctreenode in tree.traverse(addnodes.toctree):
        newnodes = []
        includefiles = map(text_type, toctreenode["includefiles"])
        for includefile in includefiles:
            try:
                builder.info(colorfunc(includefile) + " ", nonl=1)
                subtree = inline_all_toctrees(
                    builder,
                    docnameset,
                    includefile,
                    builder.env.get_doctree(includefile),
                    colorfunc,
                )
                docnameset.add(includefile)
            except Exception:
                builder.warn(
                    "toctree contains ref to nonexisting file %r" % includefile,
                    builder.env.doc2path(docname),
                )
            else:
                sof = addnodes.start_of_file(docname=includefile)
                sof.children = subtree.children
                for sectionnode in sof.traverse(nodes.section):
                    if "docname" not in sectionnode:
                        sectionnode["docname"] = includefile
                newnodes.append(sof)
        toctreenode.parent.replace(toctreenode, newnodes)
    return tree


# Traceback (RuntimeError)
# Sphinx version: 1.2.2
# Python version: 2.7.7
# Docutils version: 0.12 release
# Jinja2 version: 2.7.3
# Loaded extensions:
#   sphinx.ext.oldcmarkup from /Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/ext/oldcmarkup.pyc
Traceback (most recent call last):
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/cmdline.py", line 254, in main
app.build(force_all, filenames)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/application.py", line 212, in build
self.builder.build_update()
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/builders/__init__.py", line 209, in build_update
self.build(['__all__'], to_build)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/builders/__init__.py", line 276, in build
self.write(docnames, list(updated_docnames), method)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/builders/latex.py", line 97, in write
self.config.latex_appendices or []))
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/builders/latex.py", line 133, in assemble_doctree
self.env.resolve_references(largetree, indexfile, self)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1327, in resolve_references
for node in doctree.traverse(addnodes.pending_xref):
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 252, in traverse
return self._fast_traverse(condition)
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 196, in _fast_traverse
result.extend(child._fast_traverse(cls))
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 196, in _fast_traverse
result.extend(child._fast_traverse(cls))
...
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 196, in _fast_traverse
result.extend(child._fast_traverse(cls))
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 193, in _fast_traverse
if isinstance(self, cls):
RuntimeError: maximum recursion depth exceeded while calling a Python object

# Fixed code
def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc, traversed):
    """Inline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    """
    tree = tree.deepcopy()
    for toctreenode in tree.traverse(addnodes.toctree):
        newnodes = []
        includefiles = map(text_type, toctreenode["includefiles"])
        for includefile in includefiles:
            if includefile not in traversed:
                try:
                    traversed.append(includefile)
                    builder.info(colorfunc(includefile) + " ", nonl=1)
                    subtree = inline_all_toctrees(
                        builder,
                        docnameset,
                        includefile,
                        builder.env.get_doctree(includefile),
                        colorfunc,
                        traversed,
                    )
                    docnameset.add(includefile)
                except Exception:
                    builder.warn(
                        "toctree contains ref to nonexisting file %r" % includefile,
                        builder.env.doc2path(docname),
                    )
                else:
                    sof = addnodes.start_of_file(docname=includefile)
                    sof.children = subtree.children
                    for sectionnode in sof.traverse(nodes.section):
                        if "docname" not in sectionnode:
                            sectionnode["docname"] = includefile
                    newnodes.append(sof)
        toctreenode.parent.replace(toctreenode, newnodes)
    return tree