[clang] [libclang/python] Properly report errors when test fails (PR #142371)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 2 05:01:48 PDT 2025


https://github.com/DeinAlptraum created https://github.com/llvm/llvm-project/pull/142371

test_cdb.py's test_create_fail captures stderr to suppress output but did not release it in case the test fails.

>From 4b16005e56a631c4459bca55fb396483eeea3ac6 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Mon, 2 Jun 2025 20:58:09 +0900
Subject: [PATCH] [libclang/python] Properly report errors when test fails
 test_cdb.py's test_create_fail captures stderr to suppress output but did not
 release it in case the test fails.

---
 clang/bindings/python/tests/cindex/test_cdb.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index 757a14fbaef2c..5abe56f0d65f8 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -21,13 +21,16 @@ def test_create_fail(self):
 
         # clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
         # Suppress its output.
-        stderr = os.dup(2)
-        with open(os.devnull, "wb") as null:
-            os.dup2(null.fileno(), 2)
-        with self.assertRaises(CompilationDatabaseError) as cm:
-            CompilationDatabase.fromDirectory(path)
-        os.dup2(stderr, 2)
-        os.close(stderr)
+        try:
+            stderr = os.dup(2)
+            with open(os.devnull, "wb") as null:
+                os.dup2(null.fileno(), 2)
+            with self.assertRaises(CompilationDatabaseError) as cm:
+                CompilationDatabase.fromDirectory(path)
+        # Ensures that stderr is reset even if the above code crashes
+        finally:
+            os.dup2(stderr, 2)
+            os.close(stderr)
 
         e = cm.exception
         self.assertEqual(e.cdb_error, CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)



More information about the cfe-commits mailing list