# Buggy code
def assign_section_numbers(self):
    """Assign a section number to each heading under a numbered toctree."""
    # a list of all docnames whose section numbers changed
    rewrite_needed = []

    old_secnumbers = self.toc_secnumbers
    self.toc_secnumbers = {}

    def _walk_toc(node, secnums, depth, titlenode=None):
        # titlenode is the title of the document, it will get assigned a
        # secnumber too, so that it shows up in next/prev/parent rellinks
        for subnode in node.children:
            if isinstance(subnode, nodes.bullet_list):
                numstack.append(0)
                _walk_toc(subnode, secnums, depth - 1, titlenode)
                numstack.pop()
                titlenode = None
            elif isinstance(subnode, nodes.list_item):
                _walk_toc(subnode, secnums, depth, titlenode)
                titlenode = None
            elif isinstance(subnode, addnodes.only):
                # at this stage we don't know yet which sections are going
                # to be included; just include all of them, even if it leads
                # to gaps in the numbering
                _walk_toc(subnode, secnums, depth, titlenode)
                titlenode = None
            elif isinstance(subnode, addnodes.compact_paragraph):
                numstack[-1] += 1
                if depth > 0:
                    number = tuple(numstack)
                else:
                    number = None
                secnums[subnode[0]["anchorname"]] = subnode[0]["secnumber"] = number
                if titlenode:
                    titlenode["secnumber"] = number
                    titlenode = None
            elif isinstance(subnode, addnodes.toctree):
                _walk_toctree(subnode, depth)

    def _walk_toctree(toctreenode, depth):
        if depth == 0:
            return
        for title, ref in toctreenode["entries"]:
            if url_re.match(ref) or ref == "self":
                # don't mess with those
                continue
            if ref in self.tocs:
                secnums = self.toc_secnumbers[ref] = {}
                _walk_toc(self.tocs[ref], secnums, depth, self.titles.get(ref))
                if secnums != old_secnumbers.get(ref):
                    rewrite_needed.append(ref)

    for docname in self.numbered_toctrees:
        doctree = self.get_doctree(docname)
        for toctreenode in doctree.traverse(addnodes.toctree):
            depth = toctreenode.get("numbered", 0)
            if depth:
                # every numbered toctree gets new numbering
                numstack = [0]
                _walk_toctree(toctreenode, depth)

    return rewrite_needed


# 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 214, in build_update
'out of date' &amp;#37; len(to_build))
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/builders/__init__.py", line 244, in build
for docname in self.env.check_dependents(updated_docnames):
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 490, in check_dependents
to_rewrite = self.assign_section_numbers()
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1501, in assign_section_numbers
_walk_toctree(toctreenode, depth)
...
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1490, in _walk_toctree
self.titles.get(ref))
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1458, in _walk_toc
_walk_toc(subnode, secnums, depth, titlenode)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1454, in _walk_toc
_walk_toc(subnode, secnums, depth-1, titlenode)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1478, in _walk_toc
_walk_toctree(subnode, depth)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1490, in _walk_toctree
self.titles.get(ref))
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1458, in _walk_toc
_walk_toc(subnode, secnums, depth, titlenode)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1454, in _walk_toc
_walk_toc(subnode, secnums, depth-1, titlenode)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1478, in _walk_toc
_walk_toctree(subnode, depth)
File "/Users/tkomiya/.buildout/buildout-eggs/Sphinx-1.2.2-py2.7.egg/sphinx/environment.py", line 1483, in _walk_toctree
for (title, ref) in toctreenode['entries']:
File "/Users/tkomiya/.buildout/buildout-eggs/docutils-0.12-py2.7.egg/docutils/nodes.py", line 566, in __getitem__
if isinstance(key, basestring):
RuntimeError: maximum recursion depth exceeded in __instancecheck__

# Fixed code
def assign_section_numbers(self):
    """Assign a section number to each heading under a numbered toctree."""
    # a list of all docnames whose section numbers changed
    rewrite_needed = []

    assigned = set()
    old_secnumbers = self.toc_secnumbers
    self.toc_secnumbers = {}

    def _walk_toc(node, secnums, depth, titlenode=None):
        # titlenode is the title of the document, it will get assigned a
        # secnumber too, so that it shows up in next/prev/parent rellinks
        for subnode in node.children:
            if isinstance(subnode, nodes.bullet_list):
                numstack.append(0)
                _walk_toc(subnode, secnums, depth - 1, titlenode)
                numstack.pop()
                titlenode = None
            elif isinstance(subnode, nodes.list_item):
                _walk_toc(subnode, secnums, depth, titlenode)
                titlenode = None
            elif isinstance(subnode, addnodes.only):
                # at this stage we don't know yet which sections are going
                # to be included; just include all of them, even if it leads
                # to gaps in the numbering
                _walk_toc(subnode, secnums, depth, titlenode)
                titlenode = None
            elif isinstance(subnode, addnodes.compact_paragraph):
                numstack[-1] += 1
                if depth > 0:
                    number = tuple(numstack)
                else:
                    number = None
                secnums[subnode[0]["anchorname"]] = subnode[0]["secnumber"] = number
                if titlenode:
                    titlenode["secnumber"] = number
                    titlenode = None
            elif isinstance(subnode, addnodes.toctree):
                _walk_toctree(subnode, depth)

    def _walk_toctree(toctreenode, depth):
        if depth == 0:
            return
        for title, ref in toctreenode["entries"]:
            if url_re.match(ref) or ref == "self" or ref in assigned:
                # don't mess with those
                continue
            if ref in self.tocs:
                secnums = self.toc_secnumbers[ref] = {}
                assigned.add(ref)
                _walk_toc(self.tocs[ref], secnums, depth, self.titles.get(ref))
                if secnums != old_secnumbers.get(ref):
                    rewrite_needed.append(ref)

    for docname in self.numbered_toctrees:
        assigned.add(docname)
        doctree = self.get_doctree(docname)
        for toctreenode in doctree.traverse(addnodes.toctree):
            depth = toctreenode.get("numbered", 0)
            if depth:
                # every numbered toctree gets new numbering
                numstack = [0]
                _walk_toctree(toctreenode, depth)

    return rewrite_needed