Skip to content

Commit

Permalink
Use catch_warnings instead of redirect_stderr to avoid odd behavi…
Browse files Browse the repository at this point in the history
…our on GitHub actions
  • Loading branch information
kavanase committed Nov 20, 2023
1 parent 29524d6 commit 7381a9e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
7 changes: 3 additions & 4 deletions doped/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
publication-quality outputs.
"""
import contextlib
import io
import itertools
import os
import warnings
Expand Down Expand Up @@ -1225,10 +1224,10 @@ def _multiprocess_parse_defect(self, defect_folder):
Process defect and catch warnings along the way, so we can print which
warnings came from which defect.
"""
str_io = io.StringIO() # Redirect stderr to capture warnings
with contextlib.redirect_stderr(str_io): # capture warnings
with warnings.catch_warnings(record=True) as captured_warnings:
parsed_defect_entry = self._parse_defect(defect_folder)
warnings_string = str_io.getvalue()

warnings_string = "\n".join(str(warning.message) for warning in captured_warnings)

return parsed_defect_entry, warnings_string

Expand Down
2 changes: 1 addition & 1 deletion examples/CdTe/CdTe_example_dpd.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/YTOS/YTOS_example_dpd.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/Sb2Se3/Sb2Se3_O_example_dpd.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/Sb2Se3/defect/Sb2Se3_defect_dict.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,22 @@ def test_DefectsParser_CdTe(self):
for warn in w
) # KPOINTS warning
assert any(
f"Warning(s) encountered when parsing Int_Te_3_Unperturbed_1 at "
f"{self.CdTe_EXAMPLE_DIR}/Int_Te_3_Unperturbed_1/vasp_ncl:\nThere are mismatching "
f"INCAR tags for your bulk and defect calculations which are likely to cause severe "
f"errors in the parsed results (energies). Found the following differences:\n(in the "
f"format: (INCAR tag, value in bulk calculation, value in defect calculation)):\n[("
f"'ADDGRID', True, False)]\nThe same INCAR settings should be used in both "
f"calculations for these tags which can affect energies!" in str(warn.message)
all(
i in str(warn.message)
for i in [
f"Warning(s) encountered when parsing Int_Te_3_Unperturbed_1 at "
f"{self.CdTe_EXAMPLE_DIR}/Int_Te_3_Unperturbed_1/vasp_ncl:",
"There are mismatching INCAR tags for your bulk and defect calculations",
]
)
for warn in w
) # INCAR warning
assert any(
all(
i in str(warn.message)
for i in [
f"Warning(s) encountered when parsing Int_Te_3_2 at"
f" {self.CdTe_EXAMPLE_DIR}/Int_Te_3_2/vasp_ncl",
f"Warning(s) encountered when parsing Int_Te_3_2 at "
f"{self.CdTe_EXAMPLE_DIR}/Int_Te_3_2/vasp_ncl",
"Multiple `OUTCAR` files found in defect directory",
]
)
Expand Down

0 comments on commit 7381a9e

Please sign in to comment.