Showing the method's class in expection's traceback
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue May 20 12:10:46 EDT 2008
En Mon, 19 May 2008 10:54:05 -0300, Agustin Villena
<agustin.villena at gmail.com> escribió:
> On May 18, 4:31 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Agustin Villena schrieb:
>>
>> > is there anyway to show the class of amethodin an exception's
>> > traceback?
>> > I want to improve the line
>> > File "G:\dev\exceptions\sample.py", line 3, in foo
>> > to
>> > File "G:\dev\exceptions\sample.py", line 3, in Some.foo
>>
>> It should be. You can get a dictionary of the locals of an exception
>> stack frame, of which you could extract the self-parameter's class.
>
> I digged on sys.exc_info() object and the traceback module and I
> could't figure how I can get the locals() of the exception stackframe
Put this function in traceback.py, and replace the lines
name = co.co_name
with
name = guess_full_method_name(f)
everywhere.
Please read the docstring!!!
def guess_full_method_name(frame):
"""Guess classname.methodname for a given frame.
Only a guess!
Assumes the frame belongs to an instancemethod
whose first argument is "self", or a classmethod
whose first argument is "cls".
Doesn't handle correctly any other situation.
Returns the class name of the object on which
the method was called, not the class where
the method was actually defined.
"""
cls_name = None
fun_name = frame.f_code.co_name
self = frame.f_locals.get('self', None)
if self is None:
cls = frame.f_locals.get('cls', None)
else:
cls = getattr(self, '__class__', None)
if cls is not None:
cls_name = getattr(cls, '__name__', None)
if cls_name is not None:
return '%s.%s' % (cls_name, fun_name)
return fun_name
--
Gabriel Genellina
More information about the Python-list
mailing list