Checking function calls

Peter Otten __peter__ at web.de
Wed Mar 8 21:44:23 CET 2006


Fredrik Tolf wrote:

> # ...
> if callable(f):
>     try:
>         f(*cv[1:])
>     except TypeError:
>         self.reply("err", "argno")
> # ...
> 
> However, this results in bugs in the server code that would cause
> TypeError to reply to the client that it had the wrong number of args,
> and I can't see what the real cause is without turning off that check
> while debugging. Therefore, I'd like to check in advance whether cv[1:]
> would raise a TypeError upon calling f with it or not. And of course,
> I'd like to be able to use cmd_* functions with variable arglists as
> well.

If you must, just check the error message, not just the type of exception.

I'd just drop the cryptical error codes like 'argno' and 'unk' and pass on
the actual error message:

try:
    f(*cv[1:])
except:
    tp, exc, traceback = sys.exc_info()
    self.reply("err", str(exc))

Note that the check whether f is callable is also gone.

Peter



More information about the Python-list mailing list