[libclang/python] Properly report errors when test fails (#142371)
test_cdb.py's test_create_fail captures stderr to suppress output but
did not release it in case the test fails.
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index 757a14f..5abe56f 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -21,13 +21,16 @@
# 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)