Dump function definition, doc string, and function body. This code is specialied for Python 2.
(self, node, is_lambda, nested=1, code_node=None)
| 30 | from xdis import iscode |
| 31 | |
| 32 | def make_function1(self, node, is_lambda, nested=1, code_node=None): |
| 33 | """ |
| 34 | Dump function definition, doc string, and function body. |
| 35 | This code is specialied for Python 2. |
| 36 | """ |
| 37 | |
| 38 | def build_param(tree, param_names: List[str]) -> Tuple[bool, List[str]]: |
| 39 | """build parameters: |
| 40 | - handle defaults |
| 41 | - handle format tuple parameters |
| 42 | """ |
| 43 | # if formal parameter is a tuple, the parameter name |
| 44 | # starts with a dot (eg. '.1', '.2') |
| 45 | args = tree[0] |
| 46 | del tree[0] |
| 47 | params = [] |
| 48 | assert args.kind in ("star_args", "args", "varargs") |
| 49 | has_star_arg = args.kind in ("star_args", "varargs") |
| 50 | args_store = args[2] |
| 51 | if args_store == "args_store": |
| 52 | for arg in args_store: |
| 53 | params.append(param_names[arg.attr]) |
| 54 | return has_star_arg, params |
| 55 | |
| 56 | # MAKE_FUNCTION_... or MAKE_CLOSURE_... |
| 57 | assert node[-1].kind.startswith("BUILD_") |
| 58 | |
| 59 | defparams = [] |
| 60 | # args_node = node[-1] |
| 61 | # if isinstance(args_node.attr, tuple): |
| 62 | # # positional args are after kwargs |
| 63 | # defparams = node[1 : args_node.attr[0] + 1] |
| 64 | # pos_args, kw_args, annotate_argc = args_node.attr |
| 65 | # else: |
| 66 | # defparams = node[: args_node.attr] |
| 67 | # kw_args = 0 |
| 68 | # pass |
| 69 | |
| 70 | lambda_index = None |
| 71 | |
| 72 | if lambda_index and is_lambda and iscode(node[lambda_index].attr): |
| 73 | assert node[lambda_index].kind == "LOAD_LAMBDA" |
| 74 | code = node[lambda_index].attr |
| 75 | else: |
| 76 | code = code_node.attr |
| 77 | |
| 78 | assert iscode(code) |
| 79 | code = Code(code, self.scanner, self.currentclass) |
| 80 | |
| 81 | # add defaults values to parameter names |
| 82 | argc = code.co_argcount |
| 83 | paramnames = list(code.co_varnames[:argc]) |
| 84 | |
| 85 | # defaults are for last n parameters, thus reverse |
| 86 | paramnames.reverse() |
| 87 | defparams.reverse() |
| 88 | |
| 89 | try: |
no test coverage detected