Smart help again
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sun Apr 10 13:41:13 EDT 2005
Hello, here I extend the idea of the smart help I've discussed
recently.
When I receive an error like:
TypeError: fun() takes exactly 2 arguments (1 given)
I'd also like to see that method/function parameters (or the first line
of its docstring).
>From a discussion with gentle programmers in another newsgroup, I've
see that this problem can probably be solved with something like:
. import sys, exceptions, difflib, inspect
.
. def excepthook(type, value, tb):
. extra = ""
. if sys.last_type == exceptions.TypeError:
. # extra = string of function/method +
. # parameters inspect.getargspec(function or method)
. import traceback
. tblines = traceback.format_exception(type, value, tb)
. tblines.append(extra)
. print "".join( map(str, tblines) )
. sys.exit(1)
.
. sys.excepthook = excepthook
inspect.getargspec is useful, but how can I find there the class+method
or (function name) of the raised error? I can find the function name
with something like this, but it doesn't look like a nice solution, and
it cannot be used to find the class of the method:
extra = str(sys.last_value)
extra = extra[:extra.find("()")]
Thank you,
Bearophile
More information about the Python-list
mailing list