# Buggy code
def convert_macro_arg(raw_arg, kind, glbs, locs, *, name="<arg>", macroname="<macro>"):
    """Converts a string macro argument based on the requested kind.

    Parameters
    ----------
    raw_arg : str
        The str representation of the macro argument.
    kind : object
        A flag or type representing how to convert the argument.
    glbs : Mapping
        The globals from the call site.
    locs : Mapping or None
        The locals from the call site.
    name : str, optional
        The macro argument name.
    macroname : str, optional
        The name of the macro itself.

    Returns
    -------
    The converted argument.
    """
    # munge kind and mode to start
    mode = None
    if isinstance(kind, cabc.Sequence) and not isinstance(kind, str):
        # have (kind, mode) tuple
        kind, mode = kind
    if isinstance(kind, str):
        kind = _convert_kind_flag(kind)
    if kind is str or kind is None:
        return raw_arg  # short circuit since there is nothing else to do
    # select from kind and convert
    execer = builtins.__xonsh__.execer
    filename = macroname + "(" + name + ")"
    if kind is AST:
        ctx = set(dir(builtins)) | set(glbs.keys())
        if locs is not None:
            ctx |= set(locs.keys())
        mode = mode or "eval"
        arg = execer.parse(raw_arg, ctx, mode=mode, filename=filename)
    elif kind is types.CodeType or kind is compile:  # NOQA
        mode = mode or "eval"
        arg = execer.compile(
            raw_arg, mode=mode, glbs=glbs, locs=locs, filename=filename
        )
    elif kind is eval:
        arg = execer.eval(raw_arg, glbs=glbs, locs=locs, filename=filename)
    elif kind is exec:
        mode = mode or "exec"
        if not raw_arg.endswith("\n"):
            raw_arg += "\n"
        arg = execer.exec(raw_arg, mode=mode, glbs=glbs, locs=locs, filename=filename)
    elif kind is type:
        arg = type(execer.eval(raw_arg, glbs=glbs, locs=locs, filename=filename))
    else:
        msg = "kind={0!r} and mode={1!r} was not recognized for macro argument {2!r}"
        raise TypeError(msg.format(kind, mode, name))
    return arg


# Traceback (SyntaxError)
Traceback (most recent call last):
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/base_shell.py", line 360, in default
run_compiled_code(code, self.ctx, None, "single")
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/codecache.py", line 67, in run_compiled_code
func(code, glb, loc)
File "<xonsh-code>", line 1, in <module>
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/built_ins.py", line 1144, in call_macro
raw_arg, kind, glbs, locs, name=key, macroname=macroname
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/built_ins.py", line 1065, in convert_macro_arg
arg = execer.parse(raw_arg, ctx, mode=mode, filename=filename)
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/execer.py", line 95, in parse
tree, input = self._parse_ctx_free(input, mode=mode, filename=filename)
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/execer.py", line 231, in _parse_ctx_free
raise original_error from None
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/execer.py", line 216, in _parse_ctx_free
debug_level=(self.debug_level > 2),
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/parsers/base.py", line 546, in parse
tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
File "/nix/store/zak9a1w12r66j8pdfsjabzdnd3b64h6k-python3.6-ply-3.11/lib/python3.6/site-packages/ply/yacc.py", line 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "/nix/store/zak9a1w12r66j8pdfsjabzdnd3b64h6k-python3.6-ply-3.11/lib/python3.6/site-packages/ply/yacc.py", line 1201, in parseopt_notrack
tok = call_errorfunc(self.errorfunc, errtoken, self)
File "/nix/store/zak9a1w12r66j8pdfsjabzdnd3b64h6k-python3.6-ply-3.11/lib/python3.6/site-packages/ply/yacc.py", line 192, in call_errorfunc
r = errorfunc(token)
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/parsers/base.py", line 3275, in p_error
self._parse_error("no further code", None)
File "/nix/store/d9sdzqk041dlz2q4sm7wb2fyxplyrnh9-xonsh-0.8.3/lib/python3.6/site-packages/xonsh/parsers/base.py", line 679, in _parse_error
raise err
File "<string>", line None
SyntaxError: None: no further code

# Fixed code
def convert_macro_arg(raw_arg, kind, glbs, locs, *, name="<arg>", macroname="<macro>"):
    """Converts a string macro argument based on the requested kind.

    Parameters
    ----------
    raw_arg : str
        The str representation of the macro argument.
    kind : object
        A flag or type representing how to convert the argument.
    glbs : Mapping
        The globals from the call site.
    locs : Mapping or None
        The locals from the call site.
    name : str, optional
        The macro argument name.
    macroname : str, optional
        The name of the macro itself.

    Returns
    -------
    The converted argument.
    """
    # munge kind and mode to start
    mode = None
    if isinstance(kind, cabc.Sequence) and not isinstance(kind, str):
        # have (kind, mode) tuple
        kind, mode = kind
    if isinstance(kind, str):
        kind = _convert_kind_flag(kind)
    if kind is str or kind is None:
        return raw_arg  # short circuit since there is nothing else to do
    # select from kind and convert
    execer = builtins.__xonsh__.execer
    filename = macroname + "(" + name + ")"
    if kind is AST:
        ctx = set(dir(builtins)) | set(glbs.keys())
        if locs is not None:
            ctx |= set(locs.keys())
        mode = mode or "eval"
        if mode != "eval" and not raw_arg.endswith("\n"):
            raw_arg += "\n"
        arg = execer.parse(raw_arg, ctx, mode=mode, filename=filename)
    elif kind is types.CodeType or kind is compile:  # NOQA
        mode = mode or "eval"
        arg = execer.compile(
            raw_arg, mode=mode, glbs=glbs, locs=locs, filename=filename
        )
    elif kind is eval:
        arg = execer.eval(raw_arg, glbs=glbs, locs=locs, filename=filename)
    elif kind is exec:
        mode = mode or "exec"
        if not raw_arg.endswith("\n"):
            raw_arg += "\n"
        arg = execer.exec(raw_arg, mode=mode, glbs=glbs, locs=locs, filename=filename)
    elif kind is type:
        arg = type(execer.eval(raw_arg, glbs=glbs, locs=locs, filename=filename))
    else:
        msg = "kind={0!r} and mode={1!r} was not recognized for macro argument {2!r}"
        raise TypeError(msg.format(kind, mode, name))
    return arg