Logging messages with dictionary args in Python 2.4b1 leads to exception
Stefan Behnel
behnel_ml at dvs1.informatik.tu-darmstadt.de
Wed Oct 20 12:52:24 EDT 2004
Steve Holden schrieb:
> A possible enhancement which might meet your arguments would allow the
> user to provide EITHER a tuple of arguments OR a set of keyword
> arguments. LogRecord could then say:
>
> if args:
> msg = msg % args
> else:
> msg = msg % kwargs
>
> and you would then be able to make a call like
>
> logging.log(logging.FATAL, 'Test is %(test)s', **values)
>
> But with the code you presented, it's no use bleating that "values *is*
> a mapping". While this is certainly true, values was unfortunately just
> the only element in the args tuple, and it's the args tuple that's
> currently used for substituion. I presume the patch you sent Vijay will
> do something similar?
What it does is: check if there is only one element in args and then
unpack it.
This leads to three different cases how
msg = msg % args
is evaluated:
1) the tuple is longer than one, i.e. args actually is a tuple
=> current beahaviour
2) the tuple contained one element, but not a dict, i.e. args is a value
=> replace the single template in msg with that value
=> current behaviour
3) the tuple contained one element, which was a dict, i.e. args is a dict
=> replace the dictionary templates in msg with the values in args
I find this very much the expected behaviour, which when used by actual
code looks like this:
log("message %s %d", 1, 2)
or
log("message %(a) %(b)", {'a':1, 'b':2})
I find it absolutely wrong if the second raises an exception. That would
differ from any other common use case of dictionaries in Python.
Even this works:
log("message %s", {'a':1, 'b':2})
So the only (!) change is the additional support for dictionary arguments.
No drawbacks.
I think, this is totally the right thing to do.
Stefan
More information about the Python-list
mailing list