Dump function definition, doc string, and function body in Python version 3.0 and above
(self, node, is_lambda, nested=1, code_node=None)
| 299 | |
| 300 | |
| 301 | def make_function3(self, node, is_lambda, nested=1, code_node=None): |
| 302 | """Dump function definition, doc string, and function body in |
| 303 | Python version 3.0 and above |
| 304 | """ |
| 305 | |
| 306 | # For Python 3.3, the evaluation stack in MAKE_FUNCTION is: |
| 307 | |
| 308 | # * default argument objects in positional order |
| 309 | # * pairs of name and default argument, with the name just below |
| 310 | # the object on the stack, for keyword-only parameters |
| 311 | # * parameter annotation objects |
| 312 | # * a tuple listing the parameter names for the annotations |
| 313 | # (only if there are only annotation objects) |
| 314 | # * the code associated with the function (at TOS1) |
| 315 | # * the qualified name of the function (at TOS) |
| 316 | |
| 317 | # For Python 3.0 .. 3.2 the evaluation stack is: |
| 318 | # The function object is defined to have argc default parameters, |
| 319 | # which are found below TOS. |
| 320 | # * first come positional args in the order they are given in the source, |
| 321 | # * next come the keyword args in the order they given in the source, |
| 322 | # * finally is the code associated with the function (at TOS) |
| 323 | # |
| 324 | # Note: There is no qualified name at TOS |
| 325 | |
| 326 | # MAKE_CLOSURE adds an additional closure slot |
| 327 | |
| 328 | # In Python 3.6 stack entries change again. I understand |
| 329 | # 3.7 changes some of those changes. Yes, it is hard to follow |
| 330 | # and I am sure I haven't been able to keep up. |
| 331 | |
| 332 | # Thank you, Python. |
| 333 | |
| 334 | def build_param(ast, name, default, annotation=None): |
| 335 | """build parameters: |
| 336 | - handle defaults |
| 337 | - handle format tuple parameters |
| 338 | """ |
| 339 | value = self.traverse(default, indent="") |
| 340 | if annotation: |
| 341 | result = "%s: %s=%s" % (name, annotation, value) |
| 342 | else: |
| 343 | result = "%s=%s" % (name, value) |
| 344 | |
| 345 | # The below can probably be removed. This is probably |
| 346 | # a holdover from days when LOAD_CONST erroneously |
| 347 | # didn't handle LOAD_CONST None properly |
| 348 | if result[-2:] == "= ": # default was 'LOAD_CONST None' |
| 349 | result += "None" |
| 350 | |
| 351 | return result |
| 352 | |
| 353 | # MAKE_FUNCTION_... or MAKE_CLOSURE_... |
| 354 | assert node[-1].kind.startswith("MAKE_") |
| 355 | |
| 356 | # Python 3.3+ adds a qualified name at TOS (-1) |
| 357 | # moving down the LOAD_LAMBDA instruction |
| 358 | if (3, 0) <= self.version <= (3, 2): |
no test coverage detected