Merge stdout and stderr for pyexec

This commit is contained in:
blank X 2021-08-18 10:57:09 +07:00
parent feff9c4d8f
commit 0dad4fd028
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 2 additions and 7 deletions

View File

@ -54,10 +54,8 @@ async def pyexec(client, message):
stdout = sys.stdout stdout = sys.stdout
stderr = sys.stderr stderr = sys.stderr
wrapped_stdout = StringIO() wrapped_stdout = StringIO()
wrapped_stderr = StringIO()
try: try:
sys.stdout = wrapped_stdout sys.stdout = sys.stderr = wrapped_stdout
sys.stderr = wrapped_stderr
task = asyncio.create_task(async_obj()) task = asyncio.create_task(async_obj())
exec_tasks[rnd_id] = task exec_tasks[rnd_id] = task
returned = await task returned = await task
@ -74,10 +72,7 @@ async def pyexec(client, message):
wrapped_stderr.seek(0) wrapped_stderr.seek(0)
wrapped_stdout.seek(0) wrapped_stdout.seek(0)
output = '' output = ''
wrapped_stderr_text = wrapped_stderr.read().strip()
wrapped_stdout_text = wrapped_stdout.read().strip() wrapped_stdout_text = wrapped_stdout.read().strip()
if wrapped_stderr_text:
output += f'<code>{html.escape(wrapped_stderr_text)}</code>\n'
if wrapped_stdout_text: if wrapped_stdout_text:
output += f'<code>{html.escape(wrapped_stdout_text)}</code>\n' output += f'<code>{html.escape(wrapped_stdout_text)}</code>\n'
for i in returned: for i in returned:
@ -87,7 +82,7 @@ async def pyexec(client, message):
# send as a file if it's longer than 4096 bytes # send as a file if it's longer than 4096 bytes
if len(output) > 4096: if len(output) > 4096:
out = wrapped_stderr_text + "\n" + wrapped_stdout_text + "\n" out = wrapped_stdout_text + "\n"
for i in returned: for i in returned:
out += str(i).strip() + "\n" out += str(i).strip() + "\n"
f = BytesIO(out.strip().encode('utf-8')) f = BytesIO(out.strip().encode('utf-8'))