2012-03-06 12:33:44 -05:00
|
|
|
import inspect
|
|
|
|
from opcode import opname
|
2012-09-09 11:31:35 -04:00
|
|
|
from collections import defaultdict
|
2012-03-06 12:33:44 -05:00
|
|
|
|
|
|
|
def get_var_name(frame):
|
|
|
|
code = frame.f_code
|
|
|
|
call_index = frame.f_lasti
|
2013-03-12 08:48:09 -04:00
|
|
|
call_opc = opname[code.co_code[call_index]]
|
|
|
|
if call_opc != "CALL_FUNCTION" and call_opc != "CALL_FUNCTION_VAR":
|
2012-03-06 12:33:44 -05:00
|
|
|
return None
|
|
|
|
index = call_index+3
|
|
|
|
while True:
|
|
|
|
opc = opname[code.co_code[index]]
|
|
|
|
if opc == "STORE_NAME" or opc == "STORE_ATTR":
|
|
|
|
name_index = int(code.co_code[index+1])
|
|
|
|
return code.co_names[name_index]
|
|
|
|
elif opc == "STORE_FAST":
|
|
|
|
name_index = int(code.co_code[index+1])
|
|
|
|
return code.co_varnames[name_index]
|
|
|
|
elif opc == "STORE_DEREF":
|
|
|
|
name_index = int(code.co_code[index+1])
|
|
|
|
return code.co_cellvars[name_index]
|
2013-03-12 05:31:56 -04:00
|
|
|
elif opc == "LOAD_GLOBAL" or opc == "LOAD_ATTR" or opc == "LOAD_FAST" or opc == "LOAD_DEREF":
|
2012-03-06 12:33:44 -05:00
|
|
|
index += 3
|
|
|
|
elif opc == "DUP_TOP":
|
|
|
|
index += 1
|
2012-09-09 12:53:24 -04:00
|
|
|
elif opc == "BUILD_LIST":
|
|
|
|
index += 3
|
2012-03-06 12:33:44 -05:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-03-11 17:21:58 -04:00
|
|
|
def remove_underscore(s):
|
|
|
|
if len(s) > 2 and s[0] == "_" and s[1] != "_":
|
|
|
|
s = s[1:]
|
|
|
|
return s
|
|
|
|
|
2013-03-12 10:45:24 -04:00
|
|
|
def get_obj_var_name(override=None, default=None):
|
|
|
|
if override:
|
|
|
|
return override
|
|
|
|
|
|
|
|
frame = inspect.currentframe().f_back
|
|
|
|
# We can be called via derived classes. Go back the stack frames
|
|
|
|
# until we reach the first class that does not inherit from us.
|
|
|
|
ourclass = frame.f_locals["self"].__class__
|
|
|
|
while "self" in frame.f_locals and isinstance(frame.f_locals["self"], ourclass):
|
|
|
|
frame = frame.f_back
|
|
|
|
|
|
|
|
vn = get_var_name(frame)
|
|
|
|
if vn is None:
|
|
|
|
vn = default
|
|
|
|
else:
|
|
|
|
vn = remove_underscore(vn)
|
|
|
|
return vn
|
|
|
|
|
2012-09-09 11:31:35 -04:00
|
|
|
name_to_idx = defaultdict(int)
|
2012-09-09 06:27:32 -04:00
|
|
|
classname_to_objs = dict()
|
|
|
|
|
|
|
|
def index_id(l, obj):
|
|
|
|
for n, e in enumerate(l):
|
|
|
|
if id(e) == id(obj):
|
|
|
|
return n
|
|
|
|
raise ValueError
|
|
|
|
|
2012-09-09 11:31:35 -04:00
|
|
|
def trace_back(varname=None):
|
2012-03-06 12:33:44 -05:00
|
|
|
l = []
|
|
|
|
frame = inspect.currentframe().f_back.f_back
|
|
|
|
while frame is not None:
|
2012-09-09 11:31:35 -04:00
|
|
|
if varname is None:
|
|
|
|
varname = get_var_name(frame)
|
|
|
|
if varname is not None:
|
2013-03-11 17:21:58 -04:00
|
|
|
varname = remove_underscore(varname)
|
2012-09-09 11:31:35 -04:00
|
|
|
l.insert(0, (varname, name_to_idx[varname]))
|
|
|
|
name_to_idx[varname] += 1
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2012-03-06 12:33:44 -05:00
|
|
|
try:
|
|
|
|
obj = frame.f_locals["self"]
|
|
|
|
except KeyError:
|
|
|
|
obj = None
|
2012-09-09 06:27:32 -04:00
|
|
|
if hasattr(obj, "__del__"):
|
2012-03-06 12:33:44 -05:00
|
|
|
obj = None
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2012-03-06 12:33:44 -05:00
|
|
|
if obj is None:
|
2012-09-09 11:31:35 -04:00
|
|
|
if varname is not None:
|
|
|
|
coname = frame.f_code.co_name
|
|
|
|
if coname == "<module>":
|
|
|
|
modules = frame.f_globals["__name__"]
|
|
|
|
modules = modules.split(".")
|
|
|
|
coname = modules[len(modules)-1]
|
2013-03-11 17:21:58 -04:00
|
|
|
coname = remove_underscore(coname)
|
2012-09-09 13:33:55 -04:00
|
|
|
l.insert(0, (coname, name_to_idx[coname]))
|
|
|
|
name_to_idx[coname] += 1
|
2012-09-09 06:27:32 -04:00
|
|
|
else:
|
|
|
|
classname = obj.__class__.__name__.lower()
|
|
|
|
try:
|
|
|
|
objs = classname_to_objs[classname]
|
|
|
|
except KeyError:
|
|
|
|
classname_to_objs[classname] = [obj]
|
|
|
|
idx = 0
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
idx = index_id(objs, obj)
|
|
|
|
except ValueError:
|
|
|
|
idx = len(objs)
|
|
|
|
objs.append(obj)
|
2013-03-11 17:21:58 -04:00
|
|
|
classname = remove_underscore(classname)
|
2012-09-09 11:31:35 -04:00
|
|
|
l.insert(0, (classname, idx))
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2012-09-09 11:31:35 -04:00
|
|
|
varname = None
|
2012-03-06 12:33:44 -05:00
|
|
|
frame = frame.f_back
|
|
|
|
return l
|