1
- # Copyright (c) 2019-2021 by Rocky Bernstein
1
+ # Copyright (c) 2019-2022 by Rocky Bernstein
2
2
#
3
3
# This program is free software: you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
@@ -156,7 +156,7 @@ def build_param(ast, name, default, annotation=None):
156
156
defparams .reverse ()
157
157
158
158
try :
159
- ast = self .build_ast (
159
+ tree = self .build_ast (
160
160
scanner_code ._tokens ,
161
161
scanner_code ._customize ,
162
162
scanner_code ,
@@ -176,7 +176,9 @@ def build_param(ast, name, default, annotation=None):
176
176
if defparams :
177
177
for i , defparam in enumerate (defparams ):
178
178
params .append (
179
- build_param (paramnames [i ], defparam , annotate_dict .get (paramnames [i ]))
179
+ build_param (
180
+ tree , paramnames [i ], defparam , annotate_dict .get (paramnames [i ])
181
+ )
180
182
)
181
183
182
184
for param in paramnames [i + 1 :]:
@@ -204,24 +206,30 @@ def build_param(ast, name, default, annotation=None):
204
206
205
207
# dump parameter list (with default values)
206
208
if is_lambda :
207
- self .write ("lambda " , ", " .join (params ))
209
+ self .write ("lambda" )
210
+ if len (params ):
211
+ self .write (" " , ", " .join (params ))
212
+ elif kwonlyargcount > 0 and not (4 & code .co_flags ):
213
+ assert argc == 0
214
+ self .write (" " )
215
+
208
216
# If the last statement is None (which is the
209
217
# same thing as "return None" in a lambda) and the
210
218
# next to last statement is a "yield". Then we want to
211
219
# drop the (return) None since that was just put there
212
220
# to have something to after the yield finishes.
213
221
# FIXME: this is a bit hoaky and not general
214
222
if (
215
- len (ast ) > 1
216
- and self .traverse (ast [- 1 ]) == "None"
217
- and self .traverse (ast [- 2 ]).strip ().startswith ("yield" )
223
+ len (tree ) > 1
224
+ and self .traverse (tree [- 1 ]) == "None"
225
+ and self .traverse (tree [- 2 ]).strip ().startswith ("yield" )
218
226
):
219
- del ast [- 1 ]
227
+ del tree [- 1 ]
220
228
# Now pick out the expr part of the last statement
221
- ast_expr = ast [- 1 ]
222
- while ast_expr .kind != "expr" :
223
- ast_expr = ast_expr [0 ]
224
- ast [- 1 ] = ast_expr
229
+ tree_expr = tree [- 1 ]
230
+ while tree_expr .kind != "expr" :
231
+ tree_expr = tree_expr [0 ]
232
+ tree [- 1 ] = tree_expr
225
233
pass
226
234
else :
227
235
self .write ("(" , ", " .join (params ))
@@ -331,11 +339,11 @@ def build_param(ast, name, default, annotation=None):
331
339
# docstring exists, dump it
332
340
self .println (self .traverse (node [- 2 ]))
333
341
334
- assert ast == "stmts"
342
+ assert tree in ( "stmts" , "lambda_start" )
335
343
336
- all_globals = find_all_globals (ast , set ())
344
+ all_globals = find_all_globals (tree , set ())
337
345
globals , nonlocals = find_globals_and_nonlocals (
338
- ast , set (), set (), code , self .version
346
+ tree , set (), set (), code , self .version
339
347
)
340
348
341
349
for g in sorted ((all_globals & self .mod_globs ) | globals ):
@@ -346,9 +354,9 @@ def build_param(ast, name, default, annotation=None):
346
354
347
355
self .mod_globs -= all_globals
348
356
has_none = "None" in code .co_names
349
- rn = has_none and not find_none (ast )
357
+ rn = has_none and not find_none (tree )
350
358
self .gen_source (
351
- ast ,
359
+ tree ,
352
360
code .co_name ,
353
361
scanner_code ._customize ,
354
362
is_lambda = is_lambda ,
0 commit comments