Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import linecache
- import traceback
- import sys
- import ast
- def do_evil(*args):
- # find the line that ends the function invocation
- srcfile, num, _, _ = traceback.extract_stack()[-2]
- # find the name of the function
- funcname = sys._getframe().f_code.co_name
- # seek backwards until we have the whole call
- line = ""
- while funcname not in line:
- line = linecache.getline(srcfile, num) + line
- num -= 1
- # trim off before the call
- invocation = line.rfind(funcname)
- line = line[invocation:]
- # find the paren that closes the call
- i = line.find('(') + 1
- stack = 1
- while stack > 0:
- if line[i] == ')': stack -= 1
- if line[i] == '(': stack += 1
- i += 1
- line = line[:i]
- # parse what's left into an ast
- argnames = []
- for arg in ast.parse(line).body[0].value.args:
- argnames.append(arg.id)
- # show off
- return zip(argnames, args)
- x = 1
- y = 2
- print do_evil(x, y)
- # prints [('x', 1), ('y', 2)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement