Skip to content

Clean-up try-except-pass patterns #2553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

spec = pkgutil.get_loader(__name__)
USER_DATA_PATH = os.path.dirname(spec.get_filename(__name__))
# Handle the case of ansys-dpf-core loaded in dpf-site.zip for Python custom operators
if "dpf-site.zip" in USER_DATA_PATH:
from tempfile import mkdtemp
USER_DATA_PATH = mkdtemp(prefix="PyDPF-Core_")
if not os.path.exists(USER_DATA_PATH): # pragma: no cover
os.makedirs(USER_DATA_PATH)

LOCAL_DOWNLOADED_EXAMPLES_PATH = os.path.join(USER_DATA_PATH, "examples")
if not os.path.exists(LOCAL_DOWNLOADED_EXAMPLES_PATH): # pragma: no cover
os.makedirs(LOCAL_DOWNLOADED_EXAMPLES_PATH)
except: # pragma: no cover
pass
except Exception as e: # pragma: no cover
raise e

installed = [d.metadata["Name"] for d in importlib_metadata.distributions()]
check_for = ["ansys-dpf-gatebin", "ansys-dpf-gate", "ansys-grpc-dpf"]
Expand Down
24 changes: 16 additions & 8 deletions src/ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,27 @@ def _deep_copy(dpf_entity, server=None):
core.Field, core.FieldsContainer, core.MeshedRegion...
"""
from ansys.dpf.core.common import types, types_enum_to_types
from ansys.dpf.core.operators.serialization import serializer_to_string, string_deserializer
from ansys.dpf.core.operators.serialization.serializer_to_string import serializer_to_string
from ansys.dpf.core.operators.serialization.string_deserializer import string_deserializer

entity_server = dpf_entity._server if hasattr(dpf_entity, "_server") else None
serializer = serializer_to_string(server=entity_server)
serializer.connect(1, dpf_entity)
deserializer = string_deserializer(server=server)
stream_type = 1 if server_meet_version("8.0", serializer._server) else 0
serializer.connect(-1, stream_type)
# Set stream_type to 1 (binary) if available, else use 0 (string)
stream_type = 1 if server_meet_version(required_version="8.0", server=entity_server) else 0

serializer = serializer_to_string(
stream_type=stream_type,
server=entity_server,
)
serializer.connect(0, dpf_entity)
if stream_type == 1:
out = serializer.get_output(0, types.bytes)
else:
out = serializer.outputs.serialized_string # Required for retro with 241
deserializer.connect(-1, stream_type)
out = serializer.outputs.serialized_string1() # Required for retro with 241

deserializer = string_deserializer(
stream_type=stream_type,
server=server,
)
deserializer.connect(0, out)
type_map = types_enum_to_types()
output_type = list(type_map.keys())[list(type_map.values()).index(dpf_entity.__class__)]
Expand Down
34 changes: 26 additions & 8 deletions src/ansys/dpf/core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,16 +941,34 @@ def deep_copy(self, server=None):
f.field_definition = self.field_definition.deep_copy(server)
try:
f._data_pointer = self._data_pointer
except:
pass
except Exception as e:
raise e
try:
f.meshed_region = self.meshed_region.deep_copy(server=server)
except:
pass
if self.meshed_region:
f.meshed_region = self.meshed_region.deep_copy(server=server)
except DPFServerException as e:
if "the field doesn't have this support type" in str(e):
pass
else:
raise e
except RuntimeError as e:
if "The field's support is not a mesh." in str(e):
pass
else:
raise e
try:
f.time_freq_support = self.time_freq_support.deep_copy(server=server)
except:
pass
if self.time_freq_support:
f.time_freq_support = self.time_freq_support.deep_copy(server=server)
except DPFServerException as e:
if "the field doesn't have this support type" in str(e):
pass
else:
raise e
except RuntimeError as e:
if "The field's support is not a timefreqsupport." in str(e):
pass
else:
raise e

return f

Expand Down
4 changes: 2 additions & 2 deletions src/ansys/dpf/core/fields_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ def deep_copy(self, server=None):
fc.add_field(self.get_label_space(i), f.deep_copy(server))
try:
fc.time_freq_support = self.time_freq_support.deep_copy(server)
except:
pass
except Exception as e:
raise e
return fc

def get_time_scoping(self):
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/dpf/core/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def _clearRepeatedMessage(message):
try:
while True:
message.pop(len(message) - 1)
except:
pass
except Exception as e:
raise e


def _make_printable_type(type):
Expand Down
7 changes: 2 additions & 5 deletions src/ansys/dpf/core/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,8 @@ def _add_split_on_property_type(self, prop):
self._mesh_scoping.inputs.requested_location(self._result_info.native_scoping_location)
self._mesh_scoping.inputs.mesh(self._connector.mesh_provider)
self._mesh_scoping.inputs.label1(prop)
if previous_mesh_scoping:
try:
self._mesh_scoping.inputs.mesh_scoping(previous_mesh_scoping)
except:
pass
if isinstance(previous_mesh_scoping, Scoping):
self._mesh_scoping.inputs.mesh_scoping(previous_mesh_scoping)
return self

def on_mesh_scoping(self, mesh_scoping):
Expand Down
3 changes: 2 additions & 1 deletion src/ansys/dpf/core/time_freq_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ def deep_copy(self, server=None):
tf_copy : TimeFreqSupport
"""
tf = TimeFreqSupport(server=server)
tf.time_frequencies = self.time_frequencies.deep_copy(server=server)
if self.time_frequencies:
tf.time_frequencies = self.time_frequencies.deep_copy(server=server)
if self.complex_frequencies:
tf.complex_frequencies = self.complex_frequencies.deep_copy(server=server)
if self.rpms:
Expand Down
20 changes: 10 additions & 10 deletions src/ansys/dpf/gate/data_processing_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def file_chunk_yielder(file_path, to_server_file_path, use_tmp_dir=False):
if need_progress_bar:
try:
bar.update(min(i, tot_size))
except:
pass
except Exception as e:
raise e

if need_progress_bar:
try:
bar.finish()
except:
pass
except Exception as e:
raise e


@errors.protect_grpc_class
Expand Down Expand Up @@ -320,8 +320,8 @@ def data_processing_download_file(client, server_file_path, to_client_file_path)
try:
if bar is not None:
bar.update(min(i, tot_size))
except:
pass
except Exception as e:
raise e
if bar is not None:
bar.finish()

Expand Down Expand Up @@ -388,15 +388,15 @@ def data_processing_download_files(client, server_file_path, to_client_file_path
try:
if bar is not None:
bar.update(len(client_paths))
except:
pass
except Exception as e:
raise e
else:
f = None
if f is not None:
f.write(chunk.data.data)
try:
if bar is not None:
bar.finish()
except:
pass
except Exception as e:
raise e
return client_paths
29 changes: 15 additions & 14 deletions src/ansys/dpf/gate/dpf_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ def has_changed(self):
return self._modified and self.size > 0 # Updating is not necessary for an empty vector. Updating it can cause issue, see #2274

def __del__(self):
try:
self.dpf_vector_api.dpf_vector_delete(self)
except:
pass
if hasattr(self, "_internal_obj"):
try:
self.dpf_vector_api.dpf_vector_delete(self)
except Exception as e:
raise e


class DPFVectorInt(DPFVectorBase):
Expand All @@ -121,11 +122,11 @@ def commit(self) -> None:

def __del__(self):
try:
if self._array:
if hasattr(self, "_array") and self._array:
self.dpf_vector_api.dpf_vector_int_free(self, self.internal_data, self.internal_size,
self.has_changed())
except:
pass
except Exception as e:
raise e
super().__del__()


Expand Down Expand Up @@ -153,11 +154,11 @@ def commit(self) -> None:

def __del__(self):
try:
if self._array:
if hasattr(self, "_array") and self._array:
self.dpf_vector_api.dpf_vector_double_free(self, self.internal_data, self.internal_size,
self.has_changed())
except:
pass
except Exception as e:
raise e
super().__del__()


Expand Down Expand Up @@ -221,8 +222,8 @@ def __del__(self):
if self._array:
self.dpf_vector_api.dpf_vector_char_free(self, self.internal_data, self.size * self.type.itemsize,
self.has_changed())
except:
pass
except Exception as e:
raise e
super().__del__()


Expand All @@ -246,8 +247,8 @@ def __del__(self):
if self._array:
self.dpf_vector_api.dpf_vector_char_ptr_free(self, self.internal_data, self.internal_size,
self.has_changed())
except:
pass
except Exception as e:
raise e
super().__del__()

def __len__(self):
Expand Down
20 changes: 10 additions & 10 deletions src/ansys/dpf/gate/grpc_stream_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def _data_chunk_yielder(request, data, chunk_size=None, set_array=_set_array_to_
try:
if need_progress_bar:
bar.update(sent_length)
except:
pass
except Exception as e:
raise e
try:
if need_progress_bar:
bar.finish()
except:
pass
except Exception as e:
raise e


def dtype_to_array_type(dtype):
Expand Down Expand Up @@ -95,8 +95,8 @@ def _data_get_chunk_(dtype, service, np_array=True, get_array=lambda chunk: chun
try:
if need_progress_bar:
bar.update(i)
except:
pass
except Exception as e:
raise e

else:
arr = []
Expand All @@ -106,13 +106,13 @@ def _data_get_chunk_(dtype, service, np_array=True, get_array=lambda chunk: chun
try:
if need_progress_bar:
bar.update(len(arr))
except:
pass
except Exception as e:
raise e
try:
if need_progress_bar:
bar.finish()
except:
pass
except Exception as e:
raise e
return arr


Expand Down
2 changes: 1 addition & 1 deletion src/ansys/dpf/gate/object_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def __del__(self):
if hasattr(self, "_internal_obj") and not self.owned:
self.data_processing_api.data_processing_delete_shared_object(self)
except Exception as e:
pass
raise e
# print("Deletion failed:", e)

6 changes: 3 additions & 3 deletions src/ansys/dpf/gate/session_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def start_listening(session, bar, LOG):
if len(chunk.state.state):
LOG.warning(chunk.state.state)
except Exception as e:
pass
raise e
try:
bar.finish()
except:
pass
except Exception as e:
raise e

@staticmethod
def flush_workflows(session):
Expand Down
Loading