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

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:

* The examples in WhatsNew don't work because the implementation uses a 
different method name to fetch to context (this is a shallow error 
except that the name in WhatsNew is better and we don't really want to 
have a new method for this).  It doesn't bode well that none of the 
release candidate end users noticed this discrepancy -- it means they 
are not trying out the examples.

* 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.


Raymond



More information about the Python-Dev mailing list