Skip to content

Commit c5da471

Browse files
authored
[libclang/python] Properly report errors when test fails (llvm#142371)
test_cdb.py's test_create_fail captures stderr to suppress output but did not release it in case the test fails.
1 parent b8c4eea commit c5da471

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

clang/bindings/python/tests/cindex/test_cdb.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ def test_create_fail(self):
2121

2222
# clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
2323
# Suppress its output.
24-
stderr = os.dup(2)
25-
with open(os.devnull, "wb") as null:
26-
os.dup2(null.fileno(), 2)
27-
with self.assertRaises(CompilationDatabaseError) as cm:
28-
CompilationDatabase.fromDirectory(path)
29-
os.dup2(stderr, 2)
30-
os.close(stderr)
24+
try:
25+
stderr = os.dup(2)
26+
with open(os.devnull, "wb") as null:
27+
os.dup2(null.fileno(), 2)
28+
with self.assertRaises(CompilationDatabaseError) as cm:
29+
CompilationDatabase.fromDirectory(path)
30+
# Ensures that stderr is reset even if the above code crashes
31+
finally:
32+
os.dup2(stderr, 2)
33+
os.close(stderr)
3134

3235
e = cm.exception
3336
self.assertEqual(e.cdb_error, CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)

0 commit comments

Comments
 (0)