[issue15783] decimal.localcontext(None) fails when using the C accelerator module
Stefan Krah
report at bugs.python.org
Tue Aug 28 12:28:42 CEST 2012
Stefan Krah added the comment:
Nick Coghlan <report at bugs.python.org> wrote:
> Any third party Decimal manipulating function that accepts an
> optional context and passes it down to the standard Decimal API
> will be confronted with the same problem in 3.3: passing None
> as the context no longer means the same thing as omitting the
> context argument entirely.
I agree, but for me the issue is: What is the standard API?
If you look at the Context methods in decimal.py, you see this pattern:
def add(self, a, b):
...
r = a.__add__(b, context=self)
So I think it's reasonably safe to say that all Decimal methods only
take a context=None argument because it happens to be a convenient way
of implementing the Context methods.
With that reasoning, most of the list in msg169144 would be eliminated
already. I sort of regret that the Decimal methods of the C version take
a context argument at all, but the arguments are documented.
Now to localcontext(ctx=None). Yes, code might exist that does the
following:
def f(a, b, context=None):
with localcontext(context):
return a / b
It is, however, a strange function: If I explicitly pass a context to
a function, I'd expect that it is also used for recording any status
that accumulates in the function (or that the function actually *can*
accumulate status at all).
If I'm only interested in the precision, I'd write:
def f(a, b, prec=None):
with localcontext() as c:
c.prec = 9 if prec is None else prec
return Decimal(a) / b
[This is along the lines of Raymond's original suggestion in #15136.]
But there are other examples of unexpected behavior, such as
Decimal.to_eng_string(context) taking a context purely to determine
the value of context.capitals, i.e. no status flags can possibly be
set. Here I'd also prefer:
to_eng_string(capitals=1)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15783>
_______________________________________
More information about the Python-bugs-list
mailing list