@@ -42,15 +42,19 @@ async def meval(code, globs, **kwargs):
42
42
if not code :
43
43
return None
44
44
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
+
54
58
code .append (ast .copy_location (ast .Return (value = ast .Name (id = ret_name , ctx = ast .Load ())), code [- 1 ]))
55
59
56
60
# globals().update(**<global_args>)
@@ -70,8 +74,7 @@ async def meval(code, globs, **kwargs):
70
74
args += [a ]
71
75
args = ast .arguments (args = [], vararg = None , kwonlyargs = args , kwarg = None , defaults = [],
72
76
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 = []
75
78
fun = ast .AsyncFunctionDef (name = "tmp" , args = args , body = code , decorator_list = [], returns = None )
76
79
ast .fix_missing_locations (fun )
77
80
mod = ast .parse ("" )
@@ -82,7 +85,7 @@ async def meval(code, globs, **kwargs):
82
85
83
86
r = await locs ["tmp" ](** kwargs )
84
87
for i in range (len (r )):
85
- if isinstance (r [i ], types . CoroutineType ):
88
+ if hasattr (r [i ], "__await__" ):
86
89
r [i ] = await r [i ] # workaround for 3.5
87
90
i = 0
88
91
while i < len (r ) - 1 :
@@ -92,4 +95,6 @@ async def meval(code, globs, **kwargs):
92
95
i += 1
93
96
if len (r ) == 1 :
94
97
[r ] = r
98
+ elif not r :
99
+ r = None
95
100
return r
0 commit comments