[Python-Dev] Py2.5 issue: decimal context manager misimplemented, misdesigned, and misdocumented (original) (raw)

Raymond Hettinger rhettinger at ewtllc.com
Wed Aug 30 02:20:34 CEST 2006


I would like to see the changes to the decimal module reverted for the Py2.5 release.

Currently, the code in the decimal module implements the context manager as a separate class instead of incorporating it directly in decimal.Context. This makes the API unnecessarily complex and is not pretty compared to the code it was intended to replace.

Worse still, the implementation saves a reference to the context instead of making a copy of it. Remember decimal.Context objects are mutable -- the current implementation does not fulfill its contract to restore the context to its original state at the conclusion of the with-statement.

The right way to do it was presented in PEP343. The implementation was correct and the API was simple.

Additionally:

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.

Raymond



More information about the Python-Dev mailing list