Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_executor_basic(code, expected):
return_data = []
async for result in AsyncCodeExecutor(code):
return_data.append(result)
assert len(return_data) == len(expected)
for a, b in zip(return_data, expected):
assert a == b
async def test_executor_advanced(code, expected, arg_dict, scope):
return_data = []
async for result in AsyncCodeExecutor(code, scope, arg_dict=arg_dict):
return_data.append(result)
assert len(return_data) == len(expected)
for a, b in zip(return_data, expected):
assert a == b
if arg_dict:
scope.clear_intersection(arg_dict)
async def test_executor_builtins(scope):
codeblock = inspect.cleandoc("""
def ensure_builtins():
return ValueError
""")
return_data = []
async for result in AsyncCodeExecutor(codeblock, scope):
return_data.append(result)
assert len(return_data) == 1
assert return_data[0] is None
assert 'ensure_builtins' in scope.globals, "Checking function remains defined"
assert callable(scope.globals['ensure_builtins']), "Checking defined is callable"
assert scope.globals['ensure_builtins']() == ValueError, "Checking defined return consistent"
async def jsk_python(self, ctx: commands.Context, *, argument: codeblock_converter):
"""
Direct evaluation of Python code.
"""
arg_dict = get_var_dict_from_ctx(ctx, SCOPE_PREFIX)
arg_dict["_"] = self.last_result
scope = self.scope
try:
async with ReplResponseReactor(ctx.message):
with self.submit(ctx):
executor = AsyncCodeExecutor(argument.content, scope, arg_dict=arg_dict)
async for send, result in AsyncSender(executor):
if result is None:
continue
self.last_result = result
if isinstance(result, discord.File):
send(await ctx.send(file=result))
elif isinstance(result, discord.Embed):
send(await ctx.send(embed=result))
elif isinstance(result, PaginatorInterface):
send(await result.send_to(ctx))
else:
if not isinstance(result, str):
# repr all non-strings
result = repr(result)