def getsource(obj, **kwargs): |
if hasattr(obj, 'func_code'): |
obj = obj.func_code |
elif hasattr(obj, 'f_code'): |
obj = obj.f_code |
try: |
fullsource = obj.co_filename.__source__ |
except AttributeError: |
try: |
strsrc = inspect.getsource(obj) |
except IndentationError: |
strsrc = "\"Buggy python version consider upgrading, cannot get source\"" |
assert isinstance(strsrc, str) |
return Source(strsrc, **kwargs) |
else: |
lineno = obj.co_firstlineno - 1 |
-> end = fullsource.getblockend(lineno) |
return fullsource[lineno:end+1] |