Skip to content

Commit 27c762c

Browse files
committed
Fix bugs, improve logic, allow returns in code
1 parent 3183b9f commit 27c762c

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

meval/__init__.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ async def meval(code, globs, **kwargs):
4242
if not code:
4343
return None
4444

45-
for i in range(len(code)):
46-
if isinstance(code[i], ast.Expr):
47-
if i == len(code) - 1 or not isinstance(code[i].value, ast.Call):
48-
code[i] = ast.copy_location(ast.Expr(ast.Call(func=ast.Attribute(value=ast.Name(id=ret_name,
49-
ctx=ast.Load()),
50-
attr="append", ctx=ast.Load()),
51-
args=[code[i].value], keywords=[])), code[-1])
52-
# if isinstance(code[-1], ast.Expr): # If we can use it as a lambda return (but multiline)
53-
# code[-1] = ast.copy_location(ast.Return(code[-1].value), code[-1]) # Change it to a return statement
45+
if not any(isinstance(node, ast.Return) for node in code):
46+
for i in range(len(code)):
47+
if isinstance(code[i], ast.Expr):
48+
if i == len(code) - 1 or not isinstance(code[i].value, ast.Call):
49+
code[i] = ast.copy_location(ast.Expr(ast.Call(func=ast.Attribute(value=ast.Name(id=ret_name,
50+
ctx=ast.Load()),
51+
attr="append", ctx=ast.Load()),
52+
args=[code[i].value], keywords=[])), code[-1])
53+
else:
54+
for node in code:
55+
if isinstance(node, ast.Return):
56+
node.value = ast.List(elts=[node.value], ctx=ast.Load())
57+
5458
code.append(ast.copy_location(ast.Return(value=ast.Name(id=ret_name, ctx=ast.Load())), code[-1]))
5559

5660
# globals().update(**<global_args>)
@@ -70,8 +74,7 @@ async def meval(code, globs, **kwargs):
7074
args += [a]
7175
args = ast.arguments(args=[], vararg=None, kwonlyargs=args, kwarg=None, defaults=[],
7276
kw_defaults=[None for i in range(len(args))])
73-
if int.from_bytes(importlib.util.MAGIC_NUMBER[:-2], 'little') >= 3410:
74-
args.posonlyargs = []
77+
args.posonlyargs = []
7578
fun = ast.AsyncFunctionDef(name="tmp", args=args, body=code, decorator_list=[], returns=None)
7679
ast.fix_missing_locations(fun)
7780
mod = ast.parse("")
@@ -82,7 +85,7 @@ async def meval(code, globs, **kwargs):
8285

8386
r = await locs["tmp"](**kwargs)
8487
for i in range(len(r)):
85-
if isinstance(r[i], types.CoroutineType):
88+
if hasattr(r[i], "__await__"):
8689
r[i] = await r[i] # workaround for 3.5
8790
i = 0
8891
while i < len(r) - 1:
@@ -92,4 +95,6 @@ async def meval(code, globs, **kwargs):
9295
i += 1
9396
if len(r) == 1:
9497
[r] = r
98+
elif not r:
99+
r = None
95100
return r

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="meval",
8-
version="2.4",
8+
version="2.5",
99
author="Hackintosh 5",
1010
author_email="hackintoshfive@gmail.com",
1111
description="Performs async evaluations of strings",

0 commit comments

Comments
 (0)