# Buggy code
def build_devhelp(self, outdir, outname):
    self.info("dumping devhelp index...")

    # Basic info
    root = etree.Element(
        "book",
        title=self.config.html_title,
        name=self.config.project,
        link="index.html",
        version=self.config.version,
    )
    tree = etree.ElementTree(root)

    # TOC
    chapters = etree.SubElement(root, "chapters")

    tocdoc = self.env.get_and_resolve_doctree(
        self.config.master_doc, self, prune_toctrees=False
    )

    def write_toc(node, parent):
        if isinstance(node, addnodes.compact_paragraph) or isinstance(
            node, nodes.bullet_list
        ):
            for subnode in node:
                write_toc(subnode, parent)
        elif isinstance(node, nodes.list_item):
            item = etree.SubElement(parent, "sub")
            for subnode in node:
                write_toc(subnode, item)
        elif isinstance(node, nodes.reference):
            parent.attrib["link"] = node["refuri"]
            parent.attrib["name"] = node.astext()

    def istoctree(node):
        return isinstance(node, addnodes.compact_paragraph) and "toctree" in node

    for node in tocdoc.traverse(istoctree):
        write_toc(node, chapters)

    # Index
    functions = etree.SubElement(root, "functions")
    index = self.env.create_index(self)

    def write_index(title, refs, subitems):
        if len(refs) == 0:
            pass
        elif len(refs) == 1:
            etree.SubElement(functions, "function", name=title, link=refs[0][1])
        else:
            for i, ref in enumerate(refs):
                etree.SubElement(
                    functions, "function", name="[%d] %s" % (i, title), link=ref[1]
                )

        if subitems:
            parent_title = re.sub(r"\s*\(.*\)\s*$", "", title)
            for subitem in subitems:
                write_index("%s %s" % (parent_title, subitem[0]), subitem[1], [])

    for key, group in index:
        for title, (refs, subitems) in group:
            write_index(title, refs, subitems)

    # Dump the XML file
    f = comp_open(path.join(outdir, outname + ".devhelp"), "w")
    try:
        tree.write(f, "utf-8")
    finally:
        f.close()


# Traceback (ValueError)
# Sphinx version: 1.4.1
# Python version: 3.5.1 (CPython)
# Docutils version: 0.12 release
# Jinja2 version: 2.8
# Last messages:
#   copying images... [ 50%] using/win_installer.png
#   copying images... [ 66%] faq/python-video-icon.png
#   copying images... [ 83%] howto/logging_flow.png
#   copying images... [100%] library/tulip_coro.png
#
#   copying static files...
#   done
#   copying extra files...
#   done
#   dumping devhelp index...
# Loaded extensions:
#   sphinx.ext.coverage (1.4.1) from /usr/lib/python3.5/site-packages/sphinx/ext/coverage.py
#   alabaster (0.7.7) from /usr/lib/python3.5/site-packages/alabaster/__init__.py
#   c_annotations (1.0) from /home/jtojnar/tmp/sphinx-fail/Python-3.5.1/Doc/tools/extensions/c_annotations.py
#   pyspecific (1.0) from /home/jtojnar/tmp/sphinx-fail/Python-3.5.1/Doc/tools/extensions/pyspecific.py
#   sphinx.ext.doctest (1.4.1) from /usr/lib/python3.5/site-packages/sphinx/ext/doctest.py
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/sphinx/cmdline.py", line 244, in main
app.build(opts.force_all, filenames)
File "/usr/lib/python3.5/site-packages/sphinx/application.py", line 287, in build
self.builder.build_update()
File "/usr/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 251, in build_update
'out of date' % len(to_build))
File "/usr/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 325, in build
self.finish()
File "/usr/lib/python3.5/site-packages/sphinx/builders/html.py", line 471, in finish
self.handle_finish()
File "/usr/lib/python3.5/site-packages/sphinx/builders/devhelp.py", line 64, in handle_finish
self.build_devhelp(self.outdir, self.config.devhelp_basename)
File "/usr/lib/python3.5/site-packages/sphinx/builders/devhelp.py", line 126, in build_devhelp
for title, (refs, subitems) in group:
ValueError: too many values to unpack (expected 2)

# Fixed code
def build_devhelp(self, outdir, outname):
    self.info("dumping devhelp index...")

    # Basic info
    root = etree.Element(
        "book",
        title=self.config.html_title,
        name=self.config.project,
        link="index.html",
        version=self.config.version,
    )
    tree = etree.ElementTree(root)

    # TOC
    chapters = etree.SubElement(root, "chapters")

    tocdoc = self.env.get_and_resolve_doctree(
        self.config.master_doc, self, prune_toctrees=False
    )

    def write_toc(node, parent):
        if isinstance(node, addnodes.compact_paragraph) or isinstance(
            node, nodes.bullet_list
        ):
            for subnode in node:
                write_toc(subnode, parent)
        elif isinstance(node, nodes.list_item):
            item = etree.SubElement(parent, "sub")
            for subnode in node:
                write_toc(subnode, item)
        elif isinstance(node, nodes.reference):
            parent.attrib["link"] = node["refuri"]
            parent.attrib["name"] = node.astext()

    def istoctree(node):
        return isinstance(node, addnodes.compact_paragraph) and "toctree" in node

    for node in tocdoc.traverse(istoctree):
        write_toc(node, chapters)

    # Index
    functions = etree.SubElement(root, "functions")
    index = self.env.create_index(self)

    def write_index(title, refs, subitems):
        if len(refs) == 0:
            pass
        elif len(refs) == 1:
            etree.SubElement(functions, "function", name=title, link=refs[0][1])
        else:
            for i, ref in enumerate(refs):
                etree.SubElement(
                    functions, "function", name="[%d] %s" % (i, title), link=ref[1]
                )

        if subitems:
            parent_title = re.sub(r"\s*\(.*\)\s*$", "", title)
            for subitem in subitems:
                write_index("%s %s" % (parent_title, subitem[0]), subitem[1], [])

    for key, group in index:
        for title, (refs, subitems, key) in group:
            write_index(title, refs, subitems)

    # Dump the XML file
    f = comp_open(path.join(outdir, outname + ".devhelp"), "w")
    try:
        tree.write(f, "utf-8")
    finally:
        f.close()