# Buggy code
def find_parent(self, level, h):
    """
    Return the parent at the indicated level, allocating
    place-holder nodes as necessary.
    """
    trace = False and g.unitTesting
    trace_stack = False
    assert level >= 0
    if trace:
        g.trace("=====", level, len(self.stack), h)
    while level < len(self.stack):
        p = self.stack.pop()
        if trace:
            g.trace("POP", len(self.get_lines(p)), p.h)
            if trace and trace_stack:
                self.print_list(self.get_lines(p))
    top = self.stack[-1]
    if trace:
        g.trace("TOP", top.h)
    child = self.create_child_node(
        parent=top,
        body=None,
        headline=h,  # Leave the headline alone
    )
    self.stack.append(child)
    if trace and trace_stack:
        self.print_stack(self.stack)
    return self.stack[level]


# Traceback (IndexError)
reading: @auto-md Software.md
Exception running Markdown_Importer
Traceback (most recent call last):
File "D:\Synced\github repos\leo\leo\core\leoApp.py", line 1468, in scanner_for_at_auto_cb
return scanner.run(s, parent)
File "D:\Synced\github repos\leo\leo\plugins\importers\linescanner.py", line 463, in run
self.generate_nodes(s, parent)
File "D:\Synced\github repos\leo\leo\plugins\importers\linescanner.py", line 530, in generate_nodes
self.gen_lines(s, parent)
File "D:\Synced\github repos\leo\leo\plugins\importers\markdown.py", line 48, in gen_lines
self.make_node(level, name)
File "D:\Synced\github repos\leo\leo\plugins\importers\markdown.py", line 146, in make_node
self.find_parent(level=level, h=name)
File "D:\Synced\github repos\leo\leo\plugins\importers\markdown.py", line 85, in find_parent
return self.stack[level]
IndexError: list index out of range
read 21 files in 1.10 seconds
read outline in 1.14 seconds

# Fixed code
def find_parent(self, level, h):
    """
    Return the parent at the indicated level, allocating
    place-holder nodes as necessary.
    """
    trace = False and not g.unitTesting
    assert level >= 0
    if trace:
        print("")
        g.trace("===== level: %s, len(stack): %s h: %s" % (level, len(self.stack), h))
    while level < len(self.stack):
        p = self.stack.pop()
        if trace:
            g.trace("POP", len(self.get_lines(p)), p.h)
            # self.print_list(self.get_lines(p))
    top = self.stack[-1]
    if trace:
        g.trace("TOP", top.h)
    if 1:  # Experimental fix for #877.
        if level > len(self.stack):
            print("")
            g.trace("Unexpected markdown level for: %s" % h)
            print("")
        while level > len(self.stack):
            child = self.create_child_node(
                parent=top, body=None, headline="INSERTED NODE"
            )
            self.stack.append(child)
    assert level == len(self.stack), (level, len(self.stack))
    child = self.create_child_node(
        parent=top,
        body=None,
        headline=h,  # Leave the headline alone
    )
    self.stack.append(child)
    if trace:
        g.trace("level", level)
        self.print_stack(self.stack)
    assert self.stack
    assert 0 <= level < len(self.stack), (level, len(self.stack))
    return self.stack[level]