If the name of the formal parameter starts with dot, it's a tuple parameter, like this: # def MyFunc(xx, (a,b,c), yy): # print a, b*2, c*42 In byte-code, the whole tuple is assigned to parameter '.1' and then the tuple gets u
(self, ast, name)
| 996 | |
| 997 | # This code is only for Python 1.x - 2.1 ish! |
| 998 | def get_tuple_parameter(self, ast, name): |
| 999 | """ |
| 1000 | If the name of the formal parameter starts with dot, |
| 1001 | it's a tuple parameter, like this: |
| 1002 | # def MyFunc(xx, (a,b,c), yy): |
| 1003 | # print a, b*2, c*42 |
| 1004 | In byte-code, the whole tuple is assigned to parameter '.1' and |
| 1005 | then the tuple gets unpacked to 'a', 'b' and 'c'. |
| 1006 | |
| 1007 | Since identifiers starting with a dot are illegal in Python, |
| 1008 | we can search for the byte-code equivalent to '(a,b,c) = .1' |
| 1009 | """ |
| 1010 | |
| 1011 | assert ast == "stmts" |
| 1012 | for i in range(len(ast)): |
| 1013 | # search for an assign-statement |
| 1014 | if ast[i] == "sstmt": |
| 1015 | node = ast[i][0] |
| 1016 | else: |
| 1017 | node = ast[i] |
| 1018 | if node == "assign" and node[0] == ASSIGN_TUPLE_PARAM(name): |
| 1019 | # okay, this assigns '.n' to something |
| 1020 | del ast[i] |
| 1021 | # walk lhs; this |
| 1022 | # returns a tuple of identifiers as used |
| 1023 | # within the function definition |
| 1024 | assert node[1] == "store" |
| 1025 | # if lhs is not a UNPACK_TUPLE (or equiv.), |
| 1026 | # add parentheses to make this a tuple |
| 1027 | # if node[1][0] not in ('unpack', 'unpack_list'): |
| 1028 | result = self.traverse(node[1]) |
| 1029 | if not (result.startswith("(") and result.endswith(")")): |
| 1030 | result = "(%s)" % result |
| 1031 | return result |
| 1032 | # return self.traverse(node[1]) |
| 1033 | return f"({name}" |
| 1034 | |
| 1035 | def build_class(self, code): |
| 1036 | """Dump class definition, doc string and class body.""" |