From 7c7ef60b73ae8213723a7cb08d7b682d41baf71c Mon Sep 17 00:00:00 2001 From: Pragyaan Gaur Date: Wed, 12 Aug 2026 13:09:52 +0530 Subject: [PATCH] Fix broken append and var_names paths in SU2.io.data save_data(append=True) called load(), which does not exist; the function was renamed to load_data() and this call site was missed. Calling load_data() directly is not sufficient because filelock is not reentrant and save_data already holds the lock, so the read body is extracted into a private _read_data() helper used by both. load_data(var_names=...) deleted from the dictionary while iterating .keys(), which raises RuntimeError on Python 3. --- SU2_PY/SU2/io/data.py | 94 ++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/SU2_PY/SU2/io/data.py b/SU2_PY/SU2/io/data.py index a2265c11984..810e901786e 100644 --- a/SU2_PY/SU2/io/data.py +++ b/SU2_PY/SU2/io/data.py @@ -67,13 +67,6 @@ def load_data(file_name, var_names=None, file_format="infer", core_name="python_ ('.mat','.pkl') """ - try: - import scipy.io - - scipy_loaded = True - except ImportError: - scipy_loaded = False - if not os.path.exists(file_name): raise Exception("File does not exist: %s" % file_name) @@ -87,31 +80,7 @@ def load_data(file_name, var_names=None, file_format="infer", core_name="python_ # get filelock with filelock(file_name): - - # LOAD MATLAB - if file_format == "matlab" and scipy_loaded: - input_data = scipy.io.loadmat( - file_name=file_name, - squeeze_me=False, - chars_as_strings=True, - struct_as_record=True, - ) - # pull core variable - assert core_name in input_data, "core data not found" - input_data = input_data[core_name] - - # convert recarray to dictionary - input_data = rec2dict(input_data) - - # LOAD PICKLE - elif file_format == "pickle": - input_data = load_pickle(file_name) - # pull core variable - assert core_name in input_data, "core data not found" - input_data = input_data[core_name] - - #: if file_format - + input_data = _read_data(file_name, file_format, core_name) #: with filelock # load specified varname into dictionary @@ -121,7 +90,8 @@ def load_data(file_name, var_names=None, file_format="infer", core_name="python_ var_names = [ var_names, ] - for key in input_data.keys(): + # iterate a copy of the keys, the dictionary is modified in the loop + for key in list(input_data.keys()): if not key in var_names: del input_data[key] #: for key @@ -130,14 +100,59 @@ def load_data(file_name, var_names=None, file_format="infer", core_name="python_ return input_data -#: def load() +#: def load_data() # ------------------------------------------------------------------- -# Save a Dictionary of Data +# Read a Dictionary of Data, without locking # ------------------------------------------------------------------- +def _read_data(file_name, file_format, core_name): + """data = _read_data( file_name, file_format, core_name ) + + Reads the data dictionary from file, assuming the caller already + holds the filelock for file_name. filelock is not reentrant, so + this must never acquire the lock itself. + """ + + try: + import scipy.io + + scipy_loaded = True + except ImportError: + scipy_loaded = False + + # LOAD MATLAB + if file_format == "matlab" and scipy_loaded: + input_data = scipy.io.loadmat( + file_name=file_name, + squeeze_me=False, + chars_as_strings=True, + struct_as_record=True, + ) + # pull core variable + assert core_name in input_data, "core data not found" + input_data = input_data[core_name] + + # convert recarray to dictionary + input_data = rec2dict(input_data) + + # LOAD PICKLE + elif file_format == "pickle": + input_data = load_pickle(file_name) + # pull core variable + assert core_name in input_data, "core data not found" + input_data = input_data[core_name] + + #: if file_format + + return input_data + + +#: def _read_data() + + def save_data( file_name, data_dict, append=False, file_format="infer", core_name="python_data" ): @@ -189,12 +204,7 @@ def save_data( if not os.path.exists(file_name): raise Exception("Cannot append, file does not exist: %s" % file_name) # load old data - data_dict_old = load( - file_name=file_name, - var_names=None, - file_format=file_format, - core_name=core_name, - ) + data_dict_old = _read_data(file_name, file_format, core_name) # check for keys not in new data for key, value in data_dict_old.items(): if not (key in data_dict): @@ -227,7 +237,7 @@ def save_data( return -#: def save() +#: def save_data() # -------------------------------------------------------------------