diff --git a/src/fmu/dataio/_utils.py b/src/fmu/dataio/_utils.py index dd1f5909b..0dfcd9a89 100644 --- a/src/fmu/dataio/_utils.py +++ b/src/fmu/dataio/_utils.py @@ -130,7 +130,7 @@ def export_file( serialized_json = json.dumps(obj) if isinstance(file, Path): - with open(file, "w") as stream: + with open(file, "w", encoding="utf-8") as stream: stream.write(serialized_json) else: file.write(serialized_json.encode("utf-8")) @@ -220,7 +220,7 @@ def read_parameters_txt(pfile: Path | str) -> types.Parameters: res: types.Parameters = {} - with open(pfile) as f: + with open(pfile, encoding="utf-8") as f: for line in f: line_parts = shlex.split(line) if len(line_parts) == 2: @@ -346,7 +346,7 @@ def read_metadata_from_file(filename: str | Path) -> dict: metafilepath = Path(metafile) if not metafilepath.exists(): raise OSError(f"Cannot find requested metafile: {metafile}") - with open(metafilepath) as stream: + with open(metafilepath, encoding="utf-8") as stream: return yaml.safe_load(stream) diff --git a/tests/test_units/test_rms_context.py b/tests/test_units/test_rms_context.py index a365369d8..25f8c6f45 100644 --- a/tests/test_units/test_rms_context.py +++ b/tests/test_units/test_rms_context.py @@ -4,9 +4,11 @@ interactive or from ERT. Hence the rootpath will be ../../ """ +import builtins import logging import os import shutil +from copy import deepcopy from pathlib import Path import pandas as pd @@ -616,6 +618,41 @@ def test_gridproperty_export_with_geometry(inside_rms_setup, grid, gridproperty) assert "this_is_parent" in output +@inside_rms +def test_gridproperty_export_with_geometry_and_bad_character( + inside_rms_setup, grid, gridproperty, monkeypatch +): + """Ensures a non-ascii character in masterdata does not cause encoding parsing + failures""" + original_open = builtins.open + + def open_with_ansi(file, mode="r", *args, **kwargs): + if "r" in mode and "b" not in mode and "encoding" not in kwargs: + kwargs["encoding"] = "ANSI_X3.4-1968" + return original_open(file, mode, *args, **kwargs) + + monkeypatch.setattr(builtins, "open", open_with_ansi) + + cfg = deepcopy(inside_rms_setup["config"]) + + cfg["masterdata"]["smda"]["field"][0]["identifier"] = "Drogøn" + + grd_edata = dataio.ExportData( + config=cfg, + name="geogrid", + content={"property": {"is_discrete": False}}, + ) + outgrid = grd_edata.export(grid) + + dataio.ExportData( + config=cfg, + name="geogrid", + content={"property": {"is_discrete": False}}, + geometry=outgrid, + ).export(gridproperty) + # Will raise an exception if decoding fails + + # ====================================================================================== # Dataframe and PyArrow # ======================================================================================