This is an automated email from the ASF dual-hosted git repository.

gaogaotiantian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 11c1407582b4 [SPARK-60170][PYTHON][TEST] Cover GC state restoration in 
disable_gc
11c1407582b4 is described below

commit 11c1407582b4893c6958ba3778ec34e3c2802e5a
Author: Tim Hsiung <[email protected]>
AuthorDate: Thu Nov 5 11:14:00 2026 -0800

    [SPARK-60170][PYTHON][TEST] Cover GC state restoration in disable_gc
    
    ### What changes were proposed in this pull request?
    
    Extend `UtilTests.test_disable_gc` to cover already-disabled GC, nested 
contexts, and exception cleanup. Restore the caller's GC state after testing. 
No new test class.
    
    ### Why are the changes needed?
    
    The existing test covers only normal exit with GC initially enabled. The 
expanded test covers all four branch outcomes in `disable_gc`, up from two.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    The test body passed in isolation with GC initially enabled and disabled; 
helper branch coverage is 100%. Ruff lint/format checks passed. The 
Spark-backed fixture was not run locally.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: pi 1.1.0
    
    Closes #59371 from bearomorphism/test/trivial-helper-coverage.
    
    Authored-by: Tim Hsiung <[email protected]>
    Signed-off-by: Tian Gao <[email protected]>
---
 python/pyspark/tests/test_util.py | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/python/pyspark/tests/test_util.py 
b/python/pyspark/tests/test_util.py
index d387997a2e15..90310a5dafa7 100644
--- a/python/pyspark/tests/test_util.py
+++ b/python/pyspark/tests/test_util.py
@@ -150,10 +150,24 @@ class UtilTests(PySparkTestCase):
             _parse_memory("2gs")
 
     def test_disable_gc(self):
-        self.assertTrue(gc.isenabled())
-        with disable_gc():
-            self.assertFalse(gc.isenabled())
-        self.assertTrue(gc.isenabled())
+        # Restore the caller's GC state even if an assertion fails.
+        self.addCleanup(gc.enable if gc.isenabled() else gc.disable)
+        for enabled, set_gc in ((True, gc.enable), (False, gc.disable)):
+            with self.subTest(enabled=enabled):
+                set_gc()
+                # Exiting the inner context must not re-enable GC inside the 
outer context.
+                with disable_gc():
+                    with disable_gc():
+                        self.assertFalse(gc.isenabled())
+                    self.assertFalse(gc.isenabled())
+                self.assertEqual(gc.isenabled(), enabled)
+
+                # Exceptions must also restore the original state, including 
already-disabled GC.
+                with self.assertRaisesRegex(ValueError, "test error"):
+                    with disable_gc():
+                        self.assertFalse(gc.isenabled())
+                        raise ValueError("test error")
+                self.assertEqual(gc.isenabled(), enabled)
 
     @eventually(timeout=180, catch_timeout=True)
     @timeout(timeout=1)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to