Skip to content

Commit

Permalink
Fix crash when reading num_cpu from non-ascii files
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren authored and berland committed Dec 5, 2024
1 parent 7bb48c0 commit dd1c989
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ert/config/_get_num_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_num_cpu_from_data_file(data_file: str) -> Optional[int]:
try:
with open(data_file, "r", encoding="utf-8") as file:
return _get_num_cpu(iter(file), data_file)
except OSError as err:
except (OSError, UnicodeDecodeError) as err:
ConfigWarning.warn(
f"Failed to read from DATA_FILE {data_file}: {err}", data_file
)
Expand Down
22 changes: 21 additions & 1 deletion tests/ert/unit_tests/config/test_num_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,31 @@ def test_reading_num_cpu_from_data_file_does_not_crash(data_file_contents):
)


@pytest.mark.filterwarnings("ignore::ert.config.ConfigWarning")
@given(st.binary())
@pytest.mark.usefixtures("use_tmpdir")
def test_reading_num_cpu_from_binary_data_file_does_not_crash(data_file_contents):
data_file = "case.data"
Path(data_file).write_bytes(data_file_contents)
_ = ErtConfig.from_dict(
{
ConfigKeys.NUM_REALIZATIONS: 1,
ConfigKeys.DATA_FILE: data_file,
}
)


@pytest.mark.parametrize(
"parallelsuffix", [("/"), (" /"), (" DISTRIBUTED/"), (" DISTRIBUTED /")]
)
@pytest.mark.parametrize(
"casetitle", ["CASE", "-- A CASE --", "PARALLEL Tutorial Case"]
"casetitle",
[
"CASE",
"-- A CASE --",
"PARALLEL Tutorial Case",
"", # Not valid input in some reservoir simulators
],
)
def test_num_cpu_from_data_file_used_if_config_num_cpu_not_set(
parallelsuffix, casetitle
Expand Down

0 comments on commit dd1c989

Please sign in to comment.