# Buggy code
def compile_pillar(self, ext=True):
    """
    Render the pillar data and return
    """
    top, top_errors = self.get_top()
    if ext:
        if self.opts.get("ext_pillar_first", False):
            self.opts["pillar"], errors = self.ext_pillar(self.pillar_override)
            self.rend = salt.loader.render(self.opts, self.functions)
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches, errors=errors)
            pillar = merge(
                self.opts["pillar"],
                pillar,
                self.merge_strategy,
                self.opts.get("renderer", "yaml"),
                self.opts.get("pillar_merge_lists", False),
            )
        else:
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar, errors = self.ext_pillar(pillar, errors=errors)
    else:
        matches = self.top_matches(top)
        pillar, errors = self.render_pillar(matches)
    errors.extend(top_errors)
    if self.opts.get("pillar_opts", False):
        mopts = dict(self.opts)
        if "grains" in mopts:
            mopts.pop("grains")
        mopts["saltversion"] = __version__
        pillar["master"] = mopts
    if "pillar" in self.opts and self.opts.get("ssh_merge_pillar", False):
        pillar = merge(
            self.opts["pillar"],
            pillar,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )
    if errors:
        for error in errors:
            log.critical("Pillar render error: %s", error)
        pillar["_errors"] = errors

    if self.pillar_override:
        pillar = merge(
            pillar,
            self.pillar_override,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )

    decrypt_errors = self.decrypt_pillar(pillar)
    if decrypt_errors:
        pillar.setdefault("_errors", []).extend(decrypt_errors)

    return pillar


# Traceback (KeyError)
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/master.py", line 1838, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/site-packages/salt/master.py", line 1543, in _pillar
data = pillar.compile_pillar()
File "/usr/lib/python2.7/site-packages/salt/pillar/__init__.py", line 1005, in compile_pillar
matches = self.top_matches(top)
File "/usr/lib/python2.7/site-packages/salt/pillar/__init__.py", line 675, in top_matches
self.opts.get('nodegroups', {}),
File "/usr/lib/python2.7/site-packages/salt/matchers/confirm_top.py", line 36, in confirm_top
return m(match)
File "/usr/lib/python2.7/site-packages/salt/matchers/pillar_match.py", line 24, in match
__opts__['pillar'], tgt, delimiter=delimiter
KeyError: u'pillar'

# Fixed code
def compile_pillar(self, ext=True):
    """
    Render the pillar data and return
    """
    top, top_errors = self.get_top()
    if ext:
        if self.opts.get("ext_pillar_first", False):
            self.opts["pillar"], errors = self.ext_pillar(self.pillar_override)
            self.rend = salt.loader.render(self.opts, self.functions)
            matches = self.top_matches(top, reload=True)
            pillar, errors = self.render_pillar(matches, errors=errors)
            pillar = merge(
                self.opts["pillar"],
                pillar,
                self.merge_strategy,
                self.opts.get("renderer", "yaml"),
                self.opts.get("pillar_merge_lists", False),
            )
        else:
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar, errors = self.ext_pillar(pillar, errors=errors)
    else:
        matches = self.top_matches(top)
        pillar, errors = self.render_pillar(matches)
    errors.extend(top_errors)
    if self.opts.get("pillar_opts", False):
        mopts = dict(self.opts)
        if "grains" in mopts:
            mopts.pop("grains")
        mopts["saltversion"] = __version__
        pillar["master"] = mopts
    if "pillar" in self.opts and self.opts.get("ssh_merge_pillar", False):
        pillar = merge(
            self.opts["pillar"],
            pillar,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )
    if errors:
        for error in errors:
            log.critical("Pillar render error: %s", error)
        pillar["_errors"] = errors

    if self.pillar_override:
        pillar = merge(
            pillar,
            self.pillar_override,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )

    decrypt_errors = self.decrypt_pillar(pillar)
    if decrypt_errors:
        pillar.setdefault("_errors", []).extend(decrypt_errors)

    return pillar