diff --git a/tgpy/_core/eval_message.py b/tgpy/_core/eval_message.py index eb59703..525c032 100644 --- a/tgpy/_core/eval_message.py +++ b/tgpy/_core/eval_message.py @@ -1,5 +1,6 @@ import asyncio from asyncio import Task +from contextvars import copy_context from telethon.errors import MessageIdInvalidError from telethon.tl.custom import Message @@ -13,7 +14,8 @@ async def eval_message(code: str, message: Message) -> Message | None: - task = asyncio.create_task(tgpy_eval(code, message, filename=None)) + eval_ctx = copy_context() + task = asyncio.create_task(tgpy_eval(code, message, filename=None), context=eval_ctx) running_messages[(message.chat_id, message.id)] = task # noinspection PyBroadException try: @@ -27,7 +29,7 @@ async def eval_message(code: str, message: Message) -> Message | None: output = '' exc, constants['exc'] = format_traceback() else: - if app.ctx.is_manual_output: + if eval_ctx.run(lambda: app.ctx.is_manual_output): return result = convert_result(eval_result.result) output = eval_result.output diff --git a/tgpy/api/tgpy_eval.py b/tgpy/api/tgpy_eval.py index cb9d0fd..9e1114a 100644 --- a/tgpy/api/tgpy_eval.py +++ b/tgpy/api/tgpy_eval.py @@ -47,7 +47,7 @@ async def _wait_and_flush(self): self._flush_timer = None def flush_handler(self): - if not self._message or self._finished: + if not self._message or self._finished or app.ctx.is_manual_output: return # noinspection PyProtectedMember self._flushed_output = app.ctx._output