# Buggy code
def assemble_doctree(self, indexfile, toctree_only, appendices):
    self.docnames = set([indexfile] + appendices)
    self.info(darkgreen(indexfile) + " ", nonl=1)
    tree = self.env.get_doctree(indexfile)
    tree["docname"] = indexfile
    if toctree_only:
        # extract toctree nodes from the tree and put them in a
        # fresh document
        new_tree = new_document("<latex output>")
        new_sect = nodes.section()
        new_sect += nodes.title("<Set title in conf.py>", "<Set title in conf.py>")
        new_tree += new_sect
        for node in tree.traverse(addnodes.toctree):
            new_sect += node
        tree = new_tree
    largetree = inline_all_toctrees(self, self.docnames, indexfile, tree, darkgreen)
    largetree["docname"] = indexfile
    for docname in appendices:
        appendix = self.env.get_doctree(docname)
        appendix["docname"] = docname
        largetree.append(appendix)
    self.info()
    self.info("resolving references...")
    self.env.resolve_references(largetree, indexfile, self)
    # resolve :ref:s to distant tex files -- we can't add a cross-reference,
    # but append the document name
    for pendingnode in largetree.traverse(addnodes.pending_xref):
        docname = pendingnode["refdocname"]
        sectname = pendingnode["refsectname"]
        newnodes = [nodes.emphasis(sectname, sectname)]
        for subdir, title in self.titles:
            if docname.startswith(subdir):
                newnodes.append(nodes.Text(_(" (in "), _(" (in ")))
                newnodes.append(nodes.emphasis(title, title))
                newnodes.append(nodes.Text(")", ")"))
                break
        else:
            pass
        pendingnode.replace_self(newnodes)
    return largetree


# 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 assemble_doctree(self, indexfile, toctree_only, appendices):
    self.docnames = set([indexfile] + appendices)
    self.info(darkgreen(indexfile) + " ", nonl=1)
    tree = self.env.get_doctree(indexfile)
    tree["docname"] = indexfile
    if toctree_only:
        # extract toctree nodes from the tree and put them in a
        # fresh document
        new_tree = new_document("<latex output>")
        new_sect = nodes.section()
        new_sect += nodes.title("<Set title in conf.py>", "<Set title in conf.py>")
        new_tree += new_sect
        for node in tree.traverse(addnodes.toctree):
            new_sect += node
        tree = new_tree
    largetree = inline_all_toctrees(
        self, self.docnames, indexfile, tree, darkgreen, [indexfile]
    )
    largetree["docname"] = indexfile
    for docname in appendices:
        appendix = self.env.get_doctree(docname)
        appendix["docname"] = docname
        largetree.append(appendix)
    self.info()
    self.info("resolving references...")
    self.env.resolve_references(largetree, indexfile, self)
    # resolve :ref:s to distant tex files -- we can't add a cross-reference,
    # but append the document name
    for pendingnode in largetree.traverse(addnodes.pending_xref):
        docname = pendingnode["refdocname"]
        sectname = pendingnode["refsectname"]
        newnodes = [nodes.emphasis(sectname, sectname)]
        for subdir, title in self.titles:
            if docname.startswith(subdir):
                newnodes.append(nodes.Text(_(" (in "), _(" (in ")))
                newnodes.append(nodes.emphasis(title, title))
                newnodes.append(nodes.Text(")", ")"))
                break
        else:
            pass
        pendingnode.replace_self(newnodes)
    return largetree