# Buggy code
def format_std_prepost(template, env=None):
    """Formats a template prefix/postfix string for a standard buffer.
    Returns a string suitable for prepending or appending.
    """
    if not template:
        return ""
    env = builtins.__xonsh__.env if env is None else env
    shell = builtins.__xonsh__.shell.shell
    try:
        s = shell.prompt_formatter(template)
    except Exception:
        print_exception()
    # \001\002 is there to fool pygments into not returning an empty string
    # for potentially empty input. This happens when the template is just a
    # color code with no visible text.
    invis = "\001\002"
    s = shell.format_color(invis + s + invis, force_string=True)
    s = s.replace(invis, "")
    return s


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/user/.local/lib/python3.6/site-packages/xonsh/environ.py", line 1642, in xonsh_script_run_control
run_script_with_cache(filename, execer, ctx)
File "/home/user/.local/lib/python3.6/site-packages/xonsh/codecache.py", line 162, in run_script_with_cache
run_compiled_code(ccode, glb, loc, mode)
File "/home/user/.local/lib/python3.6/site-packages/xonsh/codecache.py", line 67, in run_compiled_code
func(code, glb, loc)
File "xonsh_repro", line 4, in <module>
ls /non_existant
File "/home/user/.local/lib/python3.6/site-packages/xonsh/built_ins.py", line 987, in subproc_captured_hiddenobject
return run_subproc(cmds, captured="hiddenobject")
File "/home/user/.local/lib/python3.6/site-packages/xonsh/built_ins.py", line 950, in run_subproc
command.end()
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2117, in end
self._end(tee_output=tee_output)
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2125, in _end
for _ in self.tee_stdout():
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2038, in tee_stdout
for line in self.iterraw():
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2002, in iterraw
self.stream_stderr(safe_readlines(stderr))
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2065, in stream_stderr
if self.stderr_prefix:
File "/home/user/.local/lib/python3.6/site-packages/xonsh/proc.py", line 2384, in stderr_prefix
s = format_std_prepost(t, env=env)
File "/home/user/.local/lib/python3.6/site-packages/xonsh/tools.py", line 1945, in format_std_prepost
shell = builtins.__xonsh__.shell.shell
AttributeError: 'NoneType' object has no attribute 'shell'
error running xonsh run control file '/home/user/xonsh_repro': 'NoneType' object has no attribute 'shell'

# Fixed code
def format_std_prepost(template, env=None):
    """Formats a template prefix/postfix string for a standard buffer.
    Returns a string suitable for prepending or appending.
    """
    if not template:
        return ""
    env = builtins.__xonsh__.env if env is None else env
    invis = "\001\002"
    if builtins.__xonsh__.shell is None:
        # shell hasn't fully started up (probably still in xonshrc)
        from xonsh.prompt.base import PromptFormatter
        from xonsh.ansi_colors import ansi_partial_color_format

        pf = PromptFormatter()
        s = pf(template)
        style = env.get("XONSH_COLOR_STYLE")
        s = ansi_partial_color_format(invis + s + invis, hide=False, style=style)
    else:
        # shell has fully started. do the normal thing
        shell = builtins.__xonsh__.shell.shell
        try:
            s = shell.prompt_formatter(template)
        except Exception:
            print_exception()
        # \001\002 is there to fool pygments into not returning an empty string
        # for potentially empty input. This happens when the template is just a
        # color code with no visible text.
        s = shell.format_color(invis + s + invis, force_string=True)
    s = s.replace(invis, "")
    return s