Spaces:
Paused
Paused
File size: 2,929 Bytes
c0e9d84 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import ast
import asyncio
import inspect
class DebugKit:
"""Async execution helpers for the eval/debug surfaces.
One instance (``debug_kit``); the module-level functions below are
backward-compatible aliases.
"""
async def aexec(self, code: str, kwargs: dict = {}) -> object:
"""Compile *code* as the body of an async function and run it.
``kwargs`` become the function's arguments (by name), so ``code`` can
reference them directly. A trailing bare expression is returned; if
that expression itself evaluates to an unawaited coroutine, it is
awaited too before returning.
"""
body = ast.parse(code, "exec").body
if body and isinstance(body[-1], ast.Expr):
body[-1] = ast.Return(value=body[-1].value)
name = "aexec"
node = ast.Module(
body=[
ast.AsyncFunctionDef(
name=name,
args=ast.arguments(
posonlyargs=[],
args=[ast.arg(arg=key) for key in kwargs],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
),
body=body,
decorator_list=[],
returns=None,
type_params=[],
)
],
type_ignores=[],
)
ast.fix_missing_locations(node)
temp = {}
exec(compile(node, "<string>", "exec"), temp)
func = await temp[name](*kwargs.values())
return await func if inspect.iscoroutine(func) else func
async def shell(self, cmd: str) -> str:
proc = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
try:
stdout, stderr = await proc.communicate()
return (stdout + stderr).decode()
finally:
try:
if not proc.returncode:
proc.terminate()
except ProcessLookupError:
pass
else:
await proc.wait()
debug_kit = DebugKit()
async def aexec(code: str, kwargs: dict = {}) -> object:
return await debug_kit.aexec(code, kwargs)
async def shell(cmd: str) -> str:
return await debug_kit.shell(cmd)
async def shell(cmd: str) -> str:
proc = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
try:
stdout, stderr = await proc.communicate()
return (stdout + stderr).decode()
finally:
try:
if not proc.returncode:
proc.terminate()
except ProcessLookupError:
pass
else:
await proc.wait()
|