# Buggy code
async def _eval(self, ctx, *, body: str):
    """Execute asynchronous code.

    This command wraps code into the body of an async function and then
    calls and awaits it. The bot will respond with anything printed to
    stdout, as well as the return value of the function.

    The code can be within a codeblock, inline code or neither, as long
    as they are not mixed and they are formatted correctly.

    Environment Variables:
        ctx      - command invokation context
        bot      - bot object
        channel  - the current channel object
        author   - command author's member object
        message  - the command's message object
        discord  - discord.py library
        commands - redbot.core.commands
        _        - The result of the last dev command.
    """
    env = {
        "bot": ctx.bot,
        "ctx": ctx,
        "channel": ctx.channel,
        "author": ctx.author,
        "guild": ctx.guild,
        "message": ctx.message,
        "asyncio": asyncio,
        "aiohttp": aiohttp,
        "discord": discord,
        "commands": commands,
        "_": self._last_result,
    }

    body = self.cleanup_code(body)
    stdout = io.StringIO()

    to_compile = "async def func():\n%s" % textwrap.indent(body, "  ")

    try:
        compiled = self.async_compile(to_compile, "<string>", "exec")
        exec(compiled, env)
    except SyntaxError as e:
        return await ctx.send(self.get_syntax_error(e))

    func = env["func"]
    result = None
    try:
        with redirect_stdout(stdout):
            result = await func()
    except:
        printed = "{}{}".format(stdout.getvalue(), traceback.format_exc())
    else:
        printed = stdout.getvalue()
        await ctx.tick()

    if result is not None:
        self._last_result = result
        msg = "{}{}".format(printed, result)
    else:
        msg = printed
    msg = self.sanitize_output(ctx, msg)

    await ctx.send_interactive(self.get_pages(msg), box_lang="py")


# Traceback (KeyError)
Traceback (most recent call last):
File "/home/ubuntu/red-venv/lib/python3.8/site-packages/redbot/core/dev_commands.py", line 192, in _eval
result = await func()
File "<string>", line 3, in func
File "<string>", line 2, in <module>
File "/usr/lib/python3.8/typing.py", line 603, in __init__
def_mod = sys._getframe(1).f_globals['__name__']  # for pickling
KeyError: '__name__'

# Fixed code
async def _eval(self, ctx, *, body: str):
    """Execute asynchronous code.

    This command wraps code into the body of an async function and then
    calls and awaits it. The bot will respond with anything printed to
    stdout, as well as the return value of the function.

    The code can be within a codeblock, inline code or neither, as long
    as they are not mixed and they are formatted correctly.

    Environment Variables:
        ctx      - command invokation context
        bot      - bot object
        channel  - the current channel object
        author   - command author's member object
        message  - the command's message object
        discord  - discord.py library
        commands - redbot.core.commands
        _        - The result of the last dev command.
    """
    env = {
        "bot": ctx.bot,
        "ctx": ctx,
        "channel": ctx.channel,
        "author": ctx.author,
        "guild": ctx.guild,
        "message": ctx.message,
        "asyncio": asyncio,
        "aiohttp": aiohttp,
        "discord": discord,
        "commands": commands,
        "_": self._last_result,
        "__name__": "__main__",
    }

    body = self.cleanup_code(body)
    stdout = io.StringIO()

    to_compile = "async def func():\n%s" % textwrap.indent(body, "  ")

    try:
        compiled = self.async_compile(to_compile, "<string>", "exec")
        exec(compiled, env)
    except SyntaxError as e:
        return await ctx.send(self.get_syntax_error(e))

    func = env["func"]
    result = None
    try:
        with redirect_stdout(stdout):
            result = await func()
    except:
        printed = "{}{}".format(stdout.getvalue(), traceback.format_exc())
    else:
        printed = stdout.getvalue()
        await ctx.tick()

    if result is not None:
        self._last_result = result
        msg = "{}{}".format(printed, result)
    else:
        msg = printed
    msg = self.sanitize_output(ctx, msg)

    await ctx.send_interactive(self.get_pages(msg), box_lang="py")