mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	Make decimal.ContextManager a private implementation detail of decimal.localcontext()
This commit is contained in:
		
							parent
							
								
									69e8897505
								
							
						
					
					
						commit
						ced1218dd1
					
				
					 2 changed files with 7 additions and 37 deletions
				
			
		|  | @ -130,9 +130,6 @@ | ||||||
|     'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', |     'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', | ||||||
|     'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', |     'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', | ||||||
| 
 | 
 | ||||||
|     # helper for context management |  | ||||||
|     'ContextManager', |  | ||||||
| 
 |  | ||||||
|     # Functions for manipulating contexts |     # Functions for manipulating contexts | ||||||
|     'setcontext', 'getcontext', 'localcontext' |     'setcontext', 'getcontext', 'localcontext' | ||||||
| ] | ] | ||||||
|  | @ -501,8 +498,8 @@ def sin(x): | ||||||
|     >>> print getcontext().prec |     >>> print getcontext().prec | ||||||
|     28 |     28 | ||||||
|     """ |     """ | ||||||
|     if ctx is None: ctx = getcontext().copy() |     if ctx is None: ctx = getcontext() | ||||||
|     return ContextManager(ctx.copy()) |     return _ContextManager(ctx) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ##### Decimal class ########################################### | ##### Decimal class ########################################### | ||||||
|  | @ -2219,30 +2216,14 @@ def __deepcopy__(self, memo): | ||||||
| 
 | 
 | ||||||
| del name, val, globalname, rounding_functions | del name, val, globalname, rounding_functions | ||||||
| 
 | 
 | ||||||
| class ContextManager(object): | class _ContextManager(object): | ||||||
|     """Context manager class to support localcontext(). |     """Context manager class to support localcontext(). | ||||||
| 
 | 
 | ||||||
|       Sets the supplied context in __enter__() and restores |       Sets a copy of the supplied context in __enter__() and restores | ||||||
|       the previous decimal context in __exit__() |       the previous decimal context in __exit__() | ||||||
| 
 |  | ||||||
|     """ |  | ||||||
|     # The below can't be included in the docstring until Python 2.6 |  | ||||||
|     # as the doctest module doesn't understand __future__ statements |  | ||||||
|     """ |  | ||||||
|     Sample usage: |  | ||||||
|     >>> from __future__ import with_statement |  | ||||||
|     >>> print getcontext().prec |  | ||||||
|     28 |  | ||||||
|     >>> ctx = Context(prec=15) |  | ||||||
|     >>> with ContextManager(ctx): |  | ||||||
|     ...     print getcontext().prec |  | ||||||
|     ... |  | ||||||
|     15 |  | ||||||
|     >>> print getcontext().prec |  | ||||||
|     28 |  | ||||||
|     """ |     """ | ||||||
|     def __init__(self, new_context): |     def __init__(self, new_context): | ||||||
|         self.new_context = new_context |         self.new_context = new_context.copy() | ||||||
|     def __enter__(self): |     def __enter__(self): | ||||||
|         self.saved_context = getcontext() |         self.saved_context = getcontext() | ||||||
|         setcontext(self.new_context) |         setcontext(self.new_context) | ||||||
|  |  | ||||||
|  | @ -1068,20 +1068,9 @@ def test_copy(self): | ||||||
| class WithStatementTest(unittest.TestCase): | class WithStatementTest(unittest.TestCase): | ||||||
|     # Can't do these as docstrings until Python 2.6 |     # Can't do these as docstrings until Python 2.6 | ||||||
|     # as doctest can't handle __future__ statements |     # as doctest can't handle __future__ statements | ||||||
|     def test_ContextManager(self): |  | ||||||
|         # The basic context manager uses the supplied context |  | ||||||
|         # without making a copy of it |  | ||||||
|         orig_ctx = getcontext() |  | ||||||
|         new_ctx = Context() |  | ||||||
|         with ContextManager(new_ctx) as enter_ctx: |  | ||||||
|             set_ctx = getcontext() |  | ||||||
|         final_ctx = getcontext() |  | ||||||
|         self.assert_(orig_ctx is final_ctx, 'did not restore context correctly') |  | ||||||
|         self.assert_(new_ctx is set_ctx, 'did not set correct context') |  | ||||||
|         self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context') |  | ||||||
| 
 | 
 | ||||||
|     def test_localcontext(self): |     def test_localcontext(self): | ||||||
|         # The helper function makes a copy of the supplied context |         # Use a copy of the current context in the block | ||||||
|         orig_ctx = getcontext() |         orig_ctx = getcontext() | ||||||
|         with localcontext() as enter_ctx: |         with localcontext() as enter_ctx: | ||||||
|             set_ctx = getcontext() |             set_ctx = getcontext() | ||||||
|  | @ -1091,7 +1080,7 @@ def test_localcontext(self): | ||||||
|         self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context') |         self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context') | ||||||
| 
 | 
 | ||||||
|     def test_localcontextarg(self): |     def test_localcontextarg(self): | ||||||
|         # The helper function makes a copy of the supplied context |         # Use a copy of the supplied context in the block | ||||||
|         orig_ctx = getcontext() |         orig_ctx = getcontext() | ||||||
|         new_ctx = Context(prec=42) |         new_ctx = Context(prec=42) | ||||||
|         with localcontext(new_ctx) as enter_ctx: |         with localcontext(new_ctx) as enter_ctx: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Nick Coghlan
						Nick Coghlan