diff --git a/src/ert/config/_get_num_cpu.py b/src/ert/config/_get_num_cpu.py index 3af553034a0..524170597b1 100644 --- a/src/ert/config/_get_num_cpu.py +++ b/src/ert/config/_get_num_cpu.py @@ -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 ) diff --git a/tests/ert/unit_tests/config/test_num_cpu.py b/tests/ert/unit_tests/config/test_num_cpu.py index 6c988e24e38..471ca80fff1 100644 --- a/tests/ert/unit_tests/config/test_num_cpu.py +++ b/tests/ert/unit_tests/config/test_num_cpu.py @@ -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