[Python-Dev] Py2.5 issue: decimal context manager misimplemented, misdesigned, and misdocumented
Phillip J. Eby
pje at telecommunity.com
Wed Aug 30 05:59:00 CEST 2006
At 05:57 PM 8/29/2006 -0700, Raymond Hettinger wrote:
>Phillip J. Eby wrote:
>
>>At 05:20 PM 8/29/2006 -0700, Raymond Hettinger wrote:
>>
>>>* The implementation's doc string examples were not tested and don't
>>>work (this is a deep error). One reads:
>>>
>>> with decimal.getcontext() as ctx:
>>> ctx.prec += 2
>>> s = ...
>>> return +s
>>>
>>>
>>>To get this to work with the current implementation, it should read
>>>
>>> with decimal.getcontext().copy().get_manager() as ctx:
>>> ctx.prec += 2
>>> s = ...
>>> return +s
>>>
>>>This is horrid. Please either revert the patch or fix it to match PEP-343.
>>
>>
>>Actually, as I read the code, that would be:
>>
>> with decimal.getcontext().get_manager() as ctx:
>
>Given the current mis-implementation, the copy() step is absolutely
>necessary. Since context objects are mutable, the current context would
>never get it precision and flags restored.
>
>Try running the example and printing out the current context precision
>before and after the with-suite. You'll see that the context has changed
>(which defeats the whole purpose).
No need; now that you've explained the problem I see why the code is
wrong. This is definitely a bug in the decimal module. It looks like the
code is correct at first glance, but the .copy() definitely needs to be in
the ContextManager class, not the get_manager() method. Yuck.
More information about the Python-Dev
mailing list