Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
diff --git a/Doc/lib/libdecimal.tex b/Doc/lib/libdecimal.tex
index a0c7bde..6f5dc2a 100644
--- a/Doc/lib/libdecimal.tex
+++ b/Doc/lib/libdecimal.tex
@@ -435,26 +435,34 @@
the \function{getcontext()} and \function{setcontext()} functions:
\begin{funcdesc}{getcontext}{}
- Return the current context for the active thread.
+ Return the current context for the active thread.
\end{funcdesc}
\begin{funcdesc}{setcontext}{c}
- Set the current context for the active thread to \var{c}.
+ Set the current context for the active thread to \var{c}.
\end{funcdesc}
Beginning with Python 2.5, you can also use the \keyword{with} statement
-to temporarily change the active context. For example the following code
-increases the current decimal precision by 2 places, performs a
-calculation, and then automatically restores the previous context:
+to temporarily change the active context.
+\begin{funcdesc}{localcontext}{\optional{c}}
+ Return a context manager that will set the current context for
+ the active thread to a copy of \var{c} on entry to the with statement
+ and restore the previous context when exiting the with statement.
+
+ For example the following code increases the current decimal precision
+ by 2 places, performs a calculation, and then automatically restores
+ the previous context:
\begin{verbatim}
-from __future__ import with_statement
-import decimal
+ from __future__ import with_statement
+ import decimal
-with decimal.getcontext() as ctx:
- ctx.prec += 2 # add 2 more digits of precision
- calculate_something()
+ with decimal.localcontext() as ctx:
+ ctx.prec += 2 # add 2 more digits of precision
+ s = calculate_something()
+ s = +s # Round the final result back to the default precision
\end{verbatim}
+\end{funcdesc}
The context that's active in the body of the \keyword{with} statement is
a \emph{copy} of the context you provided to the \keyword{with}