Cleanup: check for "BaseException" instead of "Exception"

Prefer the more generic exception type as it's possible exceptions
derive from this and not "Exception".

Also use the name 'ex' for exceptions instead of 'e'.
This commit is contained in:
Campbell Barton
2023-07-30 16:14:13 +10:00
parent 9f5be2a861
commit 53e6803977
20 changed files with 62 additions and 62 deletions
+5 -5
View File
@@ -309,7 +309,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
# in most cases the caller should 'check()' first.
try:
mod.unregister()
except Exception as ex:
except BaseException as ex:
print(
"Exception in module unregister():",
repr(getattr(mod, "__file__", module_name)),
@@ -325,7 +325,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
try:
importlib.reload(mod)
except Exception as ex:
except BaseException as ex:
handle_error(ex)
del sys.modules[module_name]
return None
@@ -354,7 +354,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
raise ImportError(name=module_name)
mod.__time__ = os.path.getmtime(mod.__file__)
mod.__addon_enabled__ = False
except Exception as ex:
except BaseException as ex:
# if the addon doesn't exist, don't print full traceback
if type(ex) is ImportError and ex.name == module_name:
print("addon not loaded:", repr(module_name))
@@ -384,7 +384,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
# 3) Try run the modules register function.
try:
mod.register()
except Exception as ex:
except BaseException as ex:
print(
"Exception in module register():",
getattr(mod, "__file__", module_name),
@@ -436,7 +436,7 @@ def disable(module_name, *, default_set=False, handle_error=None):
try:
mod.unregister()
except Exception as ex:
except BaseException as ex:
mod_path = getattr(mod, "__file__", module_name)
print("Exception in module unregister():", repr(mod_path))
del mod_path
+3 -3
View File
@@ -52,7 +52,7 @@ def _enable(template_id, *, handle_error=None, ignore_not_found=False):
# 1) try import
try:
mod = import_from_id(template_id, ignore_not_found=ignore_not_found)
except Exception as ex:
except BaseException as ex:
handle_error(ex)
return None
@@ -64,7 +64,7 @@ def _enable(template_id, *, handle_error=None, ignore_not_found=False):
# 2) try run the modules register function
try:
mod.register()
except Exception as ex:
except BaseException as ex:
print("Exception in module register(): %r" %
getattr(mod, "__file__", template_id))
handle_error(ex)
@@ -106,7 +106,7 @@ def _disable(template_id, *, handle_error=None):
try:
mod.unregister()
except Exception as ex:
except BaseException as ex:
print("Exception in module unregister(): %r" %
getattr(mod, "__file__", template_id))
handle_error(ex)
@@ -158,7 +158,7 @@ def complete(line, cursor, namespace):
func_word = match.group(2)
try:
func = eval(func_word, namespace)
except Exception:
except BaseException:
func = None
if func:
@@ -75,7 +75,7 @@ def complete_indices(word, namespace, *, obj=None, base=None):
if obj is None:
try:
obj = eval(base, namespace)
except Exception:
except BaseException:
return []
if not hasattr(obj, '__getitem__'):
# obj is not a list or dictionary
@@ -148,7 +148,7 @@ def complete(word, namespace, *, private=True):
try:
# do not run the obj expression in the console
namespace[TEMP] = eval(obj, namespace)
except Exception:
except BaseException:
return []
matches = complete_names(TEMP + '.' + attr, namespace)
matches = [obj + match[TEMP_N:] for match in matches]
@@ -167,7 +167,7 @@ def complete(word, namespace, *, private=True):
# try to retrieve the object
try:
obj = eval(word, namespace)
except Exception:
except BaseException:
return []
# ignore basic types
if type(obj) in {bool, float, int, str}:
@@ -28,8 +28,8 @@ def init_spell_check(settings, lang="en_US"):
try:
from bl_i18n_utils import utils_spell_check
return utils_spell_check.SpellChecker(settings, lang)
except Exception as e:
print("Failed to import utils_spell_check ({})".format(str(e)))
except BaseException as ex:
print("Failed to import utils_spell_check ({})".format(str(ex)))
return None
@@ -560,9 +560,9 @@ def dump_py_messages_from_files(msgs, reports, files, settings):
op = getattr(op, n)
try:
return op.get_rna_type().translation_context
except Exception as e:
except BaseException as ex:
default_op_context = i18n_contexts.operator_default
print("ERROR: ", str(e))
print("ERROR: ", str(ex))
print(" Assuming default operator context '{}'".format(default_op_context))
return default_op_context
+2 -2
View File
@@ -237,8 +237,8 @@ def enable_addons(addons=None, support=None, disable=False, check_only=False):
continue
print(" Enabling module ", module_name)
bpy.ops.preferences.addon_enable(module=module_name)
except Exception as e: # XXX TEMP WORKAROUND
print(e)
except BaseException as ex: # XXX TEMP WORKAROUND
print(ex)
# XXX There are currently some problems with bpy/rna...
# *Very* tricky to solve!
+1 -1
View File
@@ -242,7 +242,7 @@ def _init_properties_from_data(base_props, base_value):
setattr(base_props, attr, value)
except AttributeError:
print(f"Warning: property '{attr}' not found in item '{base_props.__class__.__name__}'")
except Exception as ex:
except BaseException as ex:
print(f"Warning: {ex!r}")
@@ -153,8 +153,8 @@ def do_previews(do_objects, do_collections, do_scenes, do_data_intern):
scene = None
else:
rna_backup_restore(scene, render_context.backup_scene)
except Exception as e:
print("ERROR:", e)
except BaseException as ex:
print("ERROR:", ex)
success = False
if render_context.world is not None:
@@ -167,8 +167,8 @@ def do_previews(do_objects, do_collections, do_scenes, do_data_intern):
bpy.data.worlds.remove(world)
else:
rna_backup_restore(world, render_context.backup_world)
except Exception as e:
print("ERROR:", e)
except BaseException as ex:
print("ERROR:", ex)
success = False
if render_context.camera:
@@ -185,8 +185,8 @@ def do_previews(do_objects, do_collections, do_scenes, do_data_intern):
rna_backup_restore(camera, render_context.backup_camera)
rna_backup_restore(bpy.data.cameras[render_context.camera_data, None],
render_context.backup_camera_data)
except Exception as e:
print("ERROR:", e)
except BaseException as ex:
print("ERROR:", ex)
success = False
if render_context.light:
@@ -202,16 +202,16 @@ def do_previews(do_objects, do_collections, do_scenes, do_data_intern):
rna_backup_restore(light, render_context.backup_light)
rna_backup_restore(bpy.data.lights[render_context.light_data,
None], render_context.backup_light_data)
except Exception as e:
print("ERROR:", e)
except BaseException as ex:
print("ERROR:", ex)
success = False
try:
image = bpy.data.images[render_context.image, None]
image.user_clear()
bpy.data.images.remove(image)
except Exception as e:
print("ERROR:", e)
except BaseException as ex:
print("ERROR:", ex)
success = False
return success
@@ -425,10 +425,10 @@ def do_previews(do_objects, do_collections, do_scenes, do_data_intern):
print("Saving %s..." % bpy.data.filepath)
try:
bpy.ops.wm.save_mainfile()
except Exception as e:
except BaseException as ex:
# Might fail in some odd cases, like e.g. in regression files we have glsl/ram_glsl.blend which
# references an inexistent texture... Better not break in this case, just spit error to console.
print("ERROR:", e)
print("ERROR:", ex)
else:
print("*NOT* Saving %s, because some error(s) happened while deleting temp render data..." % bpy.data.filepath)
+1 -1
View File
@@ -436,7 +436,7 @@ def draw_keymaps(context, layout):
# Defined by user preset, may contain mistakes out of our control.
try:
kc_prefs.draw(box)
except Exception:
except BaseException:
import traceback
traceback.print_exc()
del box
+1 -1
View File
@@ -347,7 +347,7 @@ def xml2rna(
def _get_context_val(context, path):
try:
value = context.path_resolve(path)
except Exception as ex:
except BaseException as ex:
print("Error: %r, path %r not found" % (ex, path))
value = Ellipsis
+4 -4
View File
@@ -80,8 +80,8 @@ def write_sysinfo(filepath):
sys.executable,
"--version",
]).strip())
except Exception as e:
py_ver = str(e)
except BaseException as ex:
py_ver = str(ex)
output.write("version: %s\n" % py_ver)
del py_ver
@@ -232,5 +232,5 @@ def write_sysinfo(filepath):
else:
output.write("%s (version: %s, path: %s)\n" %
(addon, addon_mod.bl_info.get('version', "UNKNOWN"), addon_mod.__file__))
except Exception as e:
output.write("ERROR: %s\n" % e)
except BaseException as ex:
output.write("ERROR: %s\n" % ex)
+4 -4
View File
@@ -119,8 +119,8 @@ class ProjectEdit(Operator):
# opengl buffer may fail, we can't help this, but best report it.
try:
bpy.ops.paint.image_from_view()
except RuntimeError as err:
self.report({'ERROR'}, str(err))
except RuntimeError as ex:
self.report({'ERROR'}, str(ex))
return {'CANCELLED'}
image_new = None
@@ -166,8 +166,8 @@ class ProjectEdit(Operator):
try:
bpy.ops.image.external_edit(filepath=filepath_final)
except RuntimeError as re:
self.report({'ERROR'}, str(re))
except RuntimeError as ex:
self.report({'ERROR'}, str(ex))
return {'FINISHED'}
+4 -4
View File
@@ -74,8 +74,8 @@ class NodeAddOperator:
try:
node = tree.nodes.new(type=node_type)
except RuntimeError as e:
self.report({'ERROR'}, str(e))
except RuntimeError as ex:
self.report({'ERROR'}, str(ex))
return None
for setting in self.settings:
@@ -91,11 +91,11 @@ class NodeAddOperator:
try:
setattr(node_data, node_attr_name, value)
except AttributeError as e:
except AttributeError as ex:
self.report(
{'ERROR_INVALID_INPUT'},
tip_("Node has no attribute %s") % setting.name)
print(str(e))
print(str(ex))
# Continue despite invalid attribute
node.select = True
+3 -3
View File
@@ -190,8 +190,8 @@ class AddPresetBase:
self.remove(context, filepath)
else:
os.remove(filepath)
except Exception as e:
self.report({'ERROR'}, tip_("Unable to remove preset: %r") % e)
except BaseException as ex:
self.report({'ERROR'}, tip_("Unable to remove preset: %r") % ex)
import traceback
traceback.print_exc()
return {'CANCELLED'}
@@ -250,7 +250,7 @@ class ExecutePreset(Operator):
if ext == ".py":
try:
bpy.utils.execfile(filepath)
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Failed to execute the preset: " + repr(ex))
elif ext == ".xml":
@@ -174,8 +174,8 @@ class PlayRenderedAnim(Operator):
try:
subprocess.Popen(cmd)
except Exception as e:
err_msg = tip_("Couldn't run external animation player with command %r\n%s") % (cmd, e)
except BaseException as ex:
err_msg = tip_("Couldn't run external animation player with command %r\n%s") % (cmd, ex)
self.report(
{'ERROR'},
err_msg,
+1 -1
View File
@@ -299,7 +299,7 @@ class SequencerFadesAdd(Operator):
try:
if fade.start.x < keyframe.co[0] <= fade.end.x:
keyframe_points.remove(keyframe, fast=True)
except Exception:
except BaseException:
pass
fade_fcurve.update()
+2 -2
View File
@@ -66,14 +66,14 @@ class TEXT_OT_jump_to_file_at_point(Operator):
try:
args.extend([Template(arg).substitute(**template_vars) for arg in shlex.split(text_editor_args)])
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Exception parsing template: %r" % ex)
return {'CANCELLED'}
try:
# With `check=True` if `process.returncode != 0` an exception will be raised.
subprocess.run(args, check=True)
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Exception running external editor: %r" % ex)
return {'CANCELLED'}
+1 -1
View File
@@ -228,7 +228,7 @@ class PREFERENCES_OT_keyconfig_import(Operator):
shutil.copy(self.filepath, path)
else:
shutil.move(self.filepath, path)
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, tip_("Installing keymap failed: %s") % ex)
return {'CANCELLED'}
+7 -7
View File
@@ -1825,12 +1825,12 @@ class WM_OT_properties_edit(Operator):
if prop_type_new == 'PYTHON':
try:
new_value = eval(self.eval_string)
except Exception as ex:
except BaseException as ex:
self.report({'WARNING'}, "Python evaluation failed: " + str(ex))
return {'CANCELLED'}
try:
item[name] = new_value
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Failed to assign value: " + str(ex))
return {'CANCELLED'}
if name_old != name:
@@ -2023,7 +2023,7 @@ class WM_OT_properties_edit_value(Operator):
rna_item = eval("context.%s" % self.data_path)
try:
new_value = eval(self.eval_string)
except Exception as ex:
except BaseException as ex:
self.report({'WARNING'}, "Python evaluation failed: " + str(ex))
return {'CANCELLED'}
rna_item[self.property_name] = new_value
@@ -2992,7 +2992,7 @@ class WM_OT_batch_rename(Operator):
if action.use_replace_regex_src:
try:
re.compile(action.replace_src)
except Exception as ex:
except BaseException as ex:
re_error_src = str(ex)
row.alert = True
@@ -3018,7 +3018,7 @@ class WM_OT_batch_rename(Operator):
if re_error_src is None:
try:
re.sub(action.replace_src, action.replace_dst, "")
except Exception as ex:
except BaseException as ex:
re_error_dst = str(ex)
row.alert = True
@@ -3094,14 +3094,14 @@ class WM_OT_batch_rename(Operator):
if action.use_replace_regex_src:
try:
re.compile(action.replace_src)
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Invalid regular expression (find): " + str(ex))
return {'CANCELLED'}
if action.use_replace_regex_dst:
try:
re.sub(action.replace_src, action.replace_dst, "")
except Exception as ex:
except BaseException as ex:
self.report({'ERROR'}, "Invalid regular expression (replace): " + str(ex))
return {'CANCELLED'}
@@ -242,7 +242,7 @@ class ToolSelectPanelHelper:
filepath = os.path.join(dirname, icon_name + ".dat")
try:
icon_value = bpy.app.icons.new_triangles_from_file(filepath)
except Exception as ex:
except BaseException as ex:
if not os.path.exists(filepath):
print("Missing icons:", filepath, ex)
else: