'Assign a section number to each heading under a numbered toctree.'

def assign_section_numbers(self, env):

    rewrite_needed = []
    assigned = set()
    old_secnumbers = env.toc_secnumbers
    env.toc_secnumbers = {}
    def _walk_toc(node, secnums, depth, titlenode=None):
        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):
                _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')):
                continue
            elif (ref in assigned):
                logger.warning('%s    is    already    assigned    section    numbers    (nested    numbered    toctree?)', ref, location=toctreenode, type='toc', subtype='secnum')
            elif (ref in env.tocs):
                secnums = env.toc_secnumbers[ref] = {}
                assigned.add(ref)
                _walk_toc(env.tocs[ref], secnums, depth, env.titles.get(ref))
                if (secnums != old_secnumbers.get(ref)):
                    rewrite_needed.append(ref)
    for docname in env.numbered_toctrees:
        assigned.add(docname)
        doctree = env.get_doctree(docname)
        for toctreenode in doctree.traverse(addnodes.toctree):
            depth = toctreenode.get('numbered', 0)
            if depth:
                numstack = [0]
                _walk_toctree(toctreenode, depth)
    return rewrite_needed
