]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Restore decimal context after decimal doctests (GH-120149)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 6 Jun 2024 17:12:32 +0000 (20:12 +0300)
committerGitHub <noreply@github.com>
Thu, 6 Jun 2024 17:12:32 +0000 (20:12 +0300)
The modified context caused tests failures in several other tests.

Lib/test/test_decimal.py

index e927e24b582a5dd41bfe3d9950ca98ef30f23cb3..46755107de0102138ff6d39f824a4a1fdc7d0935 100644 (file)
@@ -5892,13 +5892,17 @@ def load_tests(loader, tests, pattern):
 
     if TODO_TESTS is None:
         from doctest import DocTestSuite, IGNORE_EXCEPTION_DETAIL
+        orig_context = orig_sys_decimal.getcontext().copy()
         for mod in C, P:
             if not mod:
                 continue
             def setUp(slf, mod=mod):
                 sys.modules['decimal'] = mod
-            def tearDown(slf):
+                init(mod)
+            def tearDown(slf, mod=mod):
                 sys.modules['decimal'] = orig_sys_decimal
+                mod.setcontext(ORIGINAL_CONTEXT[mod].copy())
+                orig_sys_decimal.setcontext(orig_context.copy())
             optionflags = IGNORE_EXCEPTION_DETAIL if mod is C else 0
             sys.modules['decimal'] = mod
             tests.addTest(DocTestSuite(mod, setUp=setUp, tearDown=tearDown,
@@ -5913,8 +5917,8 @@ def setUpModule():
     TEST_ALL = ARITH if ARITH is not None else is_resource_enabled('decimal')
 
 def tearDownModule():
-    if C: C.setcontext(ORIGINAL_CONTEXT[C])
-    P.setcontext(ORIGINAL_CONTEXT[P])
+    if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
+    P.setcontext(ORIGINAL_CONTEXT[P].copy())
     if not C:
         warnings.warn('C tests skipped: no module named _decimal.',
                       UserWarning)