From addfe0c2e5e61c40e246b81128e90e2671deff3c Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Thu, 12 Feb 2026 19:54:31 -0500 Subject: [PATCH] Replace bare except with except Exception --- art.py | 5 +---- art_export.py | 2 +- art_import.py | 2 +- camera.py | 1 - charset.py | 4 +--- cursor.py | 1 - formats/in_bitmap.py | 4 ++-- formats/in_bitmap_sequence.py | 2 +- formats/in_edscii.py | 2 +- formats/out_png.py | 2 +- formats/out_png_set.py | 2 +- game_object.py | 2 -- game_world.py | 2 +- games/wildflowers/scripts/flower.py | 1 - image_convert.py | 2 +- image_export.py | 2 -- palette.py | 6 +++--- playscii.py | 30 ++++++++++++++--------------- renderable.py | 1 + renderable_line.py | 2 +- shader.py | 2 +- ui.py | 1 + ui_art_dialog.py | 22 ++++++++++----------- ui_console.py | 14 +++++++------- ui_dialog.py | 2 +- ui_edit_panel.py | 2 +- ui_element.py | 2 -- ui_file_chooser_dialog.py | 4 ++-- ui_game_menu_pulldown_item.py | 2 +- ui_object_panel.py | 2 +- ui_popup.py | 2 +- vector.py | 2 +- 32 files changed, 60 insertions(+), 72 deletions(-) diff --git a/art.py b/art.py index dfdd3a5..b367943 100644 --- a/art.py +++ b/art.py @@ -195,7 +195,6 @@ class Art: "Add a blank frame at the specified index (len+1 to add to end)." self.frames += 1 self.frame_delays.insert(index, delay) - tiles = self.layers * self.width * self.height shape = (self.layers, self.height, self.width, 4) fg, bg = 0, 0 if self.app.ui: @@ -821,7 +820,6 @@ class Art: self.app.documents_dir + ART_DIR + os.path.basename(self.filename) ) self.set_filename(new_path) - start_time = time.time() # cursor might be hovering, undo any preview changes for edit in self.app.cursor.preview_edits: edit.undo() @@ -870,7 +868,6 @@ class Art: # MAYBE-TODO: below gives not-so-pretty-printing, find out way to control # formatting for better output json.dump(d, open(self.filename, "w"), sort_keys=True, indent=1) - end_time = time.time() self.set_unsaved_changes(False) # self.app.log('saved %s to disk in %.5f seconds' % (self.filename, end_time - start_time)) self.app.log("saved {}".format(self.filename)) @@ -1205,7 +1202,7 @@ class ArtFromDisk(Art): self.valid = False try: d = json.load(open(filename)) - except: + except Exception: return width = d["width"] height = d["height"] diff --git a/art_export.py b/art_export.py index 9ca0c42..19d9251 100644 --- a/art_export.py +++ b/art_export.py @@ -47,7 +47,7 @@ class ArtExporter: ) self.app.log(line) self.app.ui.message_line.post_line(line, hold_time=10, error=True) - except: + except Exception: for line in traceback.format_exc().split("\n"): self.app.log(line) # store last used export options for "Export last" diff --git a/art_import.py b/art_import.py index 029deb6..4eeee01 100644 --- a/art_import.py +++ b/art_import.py @@ -57,7 +57,7 @@ class ArtImporter: try: if self.run_import(in_filename, options): self.success = True - except: + except Exception: for line in traceback.format_exc().split("\n"): self.app.log(line) if not self.success: diff --git a/camera.py b/camera.py index 34098af..e3027b9 100644 --- a/camera.py +++ b/camera.py @@ -90,7 +90,6 @@ class Camera: m = np.eye(4, 4, dtype=np.float32) left, bottom = 0, 0 right, top = width, height - far_z, near_z = -1, 1 x = 2 / (right - left) y = 2 / (top - bottom) z = -2 / (self.far_z - self.near_z) diff --git a/charset.py b/charset.py index 3522504..dbb301e 100644 --- a/charset.py +++ b/charset.py @@ -25,10 +25,8 @@ class CharacterSetLord: ): return self.last_check = self.app.get_elapsed_time() - changed = None for charset in self.app.charsets: if charset.has_updated(): - changed = charset.filename # reload data and image even if only one changed try: success = charset.load_char_data() @@ -45,7 +43,7 @@ class CharacterSetLord: ), True, ) - except: + except Exception: self.app.log( "CharacterSetLord: failed reloading {}".format( charset.filename diff --git a/cursor.py b/cursor.py index b9f21da..4cd2776 100644 --- a/cursor.py +++ b/cursor.py @@ -387,7 +387,6 @@ class Cursor: else: self.tool_sprite.texture = ui.selected_tool.get_icon_texture() # scale same regardless of screen resolution - aspect = self.app.window_height / self.app.window_width scale_x = self.tool_sprite.texture.width / self.app.window_width scale_x *= self.icon_scale_factor * self.app.ui.scale self.tool_sprite.scale_x = scale_x diff --git a/formats/in_bitmap.py b/formats/in_bitmap.py index a6ba1b3..08902ef 100644 --- a/formats/in_bitmap.py +++ b/formats/in_bitmap.py @@ -132,7 +132,7 @@ class ConvertImageOptionsDialog(ImportOptionsDialog): # colors: int between 2 and 256 try: int(self.field_texts[3]) - except: + except Exception: return False, self.invalid_color_error colors = int(self.field_texts[3]) if colors < 2 or colors > 256: @@ -140,7 +140,7 @@ class ConvertImageOptionsDialog(ImportOptionsDialog): # % scale: >0 float try: float(self.field_texts[8]) - except: + except Exception: return False, self.invalid_scale_error if float(self.field_texts[8]) <= 0: return False, self.invalid_scale_error diff --git a/formats/in_bitmap_sequence.py b/formats/in_bitmap_sequence.py index 06324ad..c91a3d2 100644 --- a/formats/in_bitmap_sequence.py +++ b/formats/in_bitmap_sequence.py @@ -39,7 +39,7 @@ class ImageSequenceConverter: self.current_frame_converter = image_convert.ImageConverter( self.app, self.image_filenames[0], self.art, self.bicubic_scale, self ) - except: + except Exception: self.fail() return if not self.current_frame_converter.init_success: diff --git a/formats/in_edscii.py b/formats/in_edscii.py index 69fc022..ffb4ad3 100644 --- a/formats/in_edscii.py +++ b/formats/in_edscii.py @@ -19,7 +19,7 @@ class EDSCIIImportOptionsDialog(ImportOptionsDialog): # valid widths: any >=0 int try: int(self.field_texts[0]) - except: + except Exception: return False, self.invalid_width_error if int(self.field_texts[0]) < 0: return False, self.invalid_width_error diff --git a/formats/out_png.py b/formats/out_png.py index 10fea82..e6695da 100644 --- a/formats/out_png.py +++ b/formats/out_png.py @@ -44,7 +44,7 @@ class PNGExportOptionsDialog(ExportOptionsDialog): # scale factor: >0 int try: int(self.field_texts[0]) - except: + except Exception: return False, self.invalid_scale_error if int(self.field_texts[0]) <= 0: return False, self.invalid_scale_error diff --git a/formats/out_png_set.py b/formats/out_png_set.py index c5c5102..8683c75 100644 --- a/formats/out_png_set.py +++ b/formats/out_png_set.py @@ -113,7 +113,7 @@ class PNGSetExportOptionsDialog(ExportOptionsDialog): # scale factor: >0 int try: int(self.field_texts[0]) - except: + except Exception: return False, self.invalid_scale_error if int(self.field_texts[0]) <= 0: return False, self.invalid_scale_error diff --git a/game_object.py b/game_object.py index 337c8c5..404f6ed 100644 --- a/game_object.py +++ b/game_object.py @@ -701,7 +701,6 @@ class GameObject: front_name = "{}_{}".format(art_state_name, FACINGS[GOF_FRONT]) left_name = "{}_{}".format(art_state_name, FACINGS[GOF_LEFT]) right_name = "{}_{}".format(art_state_name, FACINGS[GOF_RIGHT]) - back_name = "{}_{}".format(art_state_name, FACINGS[GOF_BACK]) has_front = front_name in self.arts has_left = left_name in self.arts has_right = right_name in self.arts @@ -859,7 +858,6 @@ class GameObject: force_z += grav_z * self.mass # friction / drag friction = self.get_friction() - speed = math.sqrt(vel_x**2 + vel_y**2 + vel_z**2) force_x -= friction * self.mass * vel_x force_y -= friction * self.mass * vel_y force_z -= friction * self.mass * vel_z diff --git a/game_world.py b/game_world.py index 45e19d4..675ed0a 100644 --- a/game_world.py +++ b/game_world.py @@ -1123,7 +1123,7 @@ class GameWorld: try: d = json.load(open(filename)) # self.app.log('Loading game state %s...' % filename) - except: + except Exception: self.app.log("Couldn't load game state from {}".format(filename)) # self.app.log(sys.exc_info()) return diff --git a/games/wildflowers/scripts/flower.py b/games/wildflowers/scripts/flower.py index a1dfc31..1c143b3 100644 --- a/games/wildflowers/scripts/flower.py +++ b/games/wildflowers/scripts/flower.py @@ -39,7 +39,6 @@ class FlowerObject(GameObject): # set random seed based on date, a different flower each day t = time.localtime() year, month, day = t.tm_year, t.tm_mon, t.tm_mday - weekday = t.tm_wday # 0 = monday date = year * 10000 + month * 100 + day if self.seed_includes_time: date += t.tm_hour * 0.01 + t.tm_min * 0.0001 + t.tm_sec * 0.000001 diff --git a/image_convert.py b/image_convert.py index 739d35a..a721057 100644 --- a/image_convert.py +++ b/image_convert.py @@ -50,7 +50,7 @@ class ImageConverter: self.sequence_converter = sequence_converter try: self.src_img = Image.open(self.image_filename).convert("RGB") - except: + except Exception: return # if we're part of a sequence, app doesn't need handle directly to us if not self.sequence_converter: diff --git a/image_export.py b/image_export.py index c7cf815..a14ee90 100644 --- a/image_export.py +++ b/image_export.py @@ -129,7 +129,6 @@ def export_animation(app, art, out_filename, bg_color=None, loop=True): output_img.write(b) output_img.write(b";") output_img.close() - output_format = "Animated GIF" # app.log('%s exported (%s)' % (out_filename, output_format)) @@ -155,7 +154,6 @@ def export_still_image(app, art, out_filename, crt=True, scale=1, bg_color=None) return False output_img = art.palette.get_palettized_image(src_img, i_transp[:3]) output_img.save(out_filename, "PNG", transparency=0) - output_format = "8-bit palettized w/ transparency" # app.log('%s exported (%s)' % (out_filename, output_format)) return True diff --git a/palette.py b/palette.py index 1bdc46d..f470563 100644 --- a/palette.py +++ b/palette.py @@ -37,7 +37,7 @@ class PaletteLord: self.app.log( "PaletteLord: success reloading {}".format(palette.filename) ) - except: + except Exception: self.app.log( "PaletteLord: failed reloading {}".format(palette.filename), True, @@ -170,7 +170,7 @@ class Palette: out_img = src_img.convert("RGB") # Image.putpalette needs a flat tuple :/ colors = [] - for i, color in enumerate(self.colors): + for color in self.colors: # ignore alpha for palettized image output for channel in color[:-1]: colors.append(channel) @@ -179,7 +179,7 @@ class Palette: colors[0:3] = transparent_color # PIL will fill out <256 color palettes with bogus values :/ while len(colors) < MAX_COLORS * 3: - for i in range(3): + for _ in range(3): colors.append(0) # palette for PIL must be exactly 256 colors colors = colors[: 256 * 3] diff --git a/playscii.py b/playscii.py index 6d79732..e343752 100755 --- a/playscii.py +++ b/playscii.py @@ -49,7 +49,7 @@ if version.parse(Image.__version__) > version.parse("10.0.0"): pdoc_available = False try: pdoc_available = True -except: +except Exception: pass # submodules - set here so cfg file can modify them all easily @@ -222,11 +222,11 @@ class Application: try: sdl2.SDL_SetHint(sdl2.SDL_HINT_VIDEODRIVER, b"wayland") sdl2.ext.init() - except: + except Exception: try: sdl2.SDL_SetHint(sdl2.SDL_HINT_VIDEODRIVER, b"x11") sdl2.ext.init() - except: + except Exception: sdl2.ext.init() winpos = sdl2.SDL_WINDOWPOS_UNDEFINED screen_width, screen_height = self.get_desktop_resolution() @@ -278,11 +278,11 @@ class Application: # report GL vendor, version, GLSL version etc try: gpu_vendor = GL.glGetString(GL.GL_VENDOR).decode("utf-8") - except: + except Exception: gpu_vendor = "[couldn't detect vendor]" try: gpu_renderer = GL.glGetString(GL.GL_RENDERER).decode("utf-8") - except: + except Exception: gpu_renderer = "[couldn't detect renderer]" self.log(" GPU: {} - {}".format(gpu_vendor, gpu_renderer)) try: @@ -291,7 +291,7 @@ class Application: if not gl_ver: gl_ver = GL.glGetString(GL.GL_VERSION, ctypes.c_int(0)) gl_ver = gl_ver.decode("utf-8") - except: + except Exception: gl_ver = "[couldn't detect GL version]" self.log(" OpenGL detected: {}".format(gl_ver)) # GL 1.1 doesn't even habla shaders, quit if we fail GLSL version check @@ -301,7 +301,7 @@ class Application: glsl_ver = GL.glGetString( GL.GL_SHADING_LANGUAGE_VERSION, ctypes.c_int(0) ) - except: + except Exception: self.log("GLSL support not detected, " + self.compat_fail_message) self.should_quit = True return @@ -335,7 +335,7 @@ class Application: if not self.run_if_opengl_incompatible: self.should_quit = True return - except: + except Exception: # can't get a firm number out of reported GLSL version string :/ pass # detect max texture size @@ -441,10 +441,10 @@ class Application: # textured background renderable self.bg_texture = UIBGTextureRenderable(self) # init onion skin - for i in range(self.onion_show_frames): + for _ in range(self.onion_show_frames): renderable = OnionTileRenderable(self, self.ui.active_art) self.onion_renderables_prev.append(renderable) - for i in range(self.onion_show_frames): + for _ in range(self.onion_show_frames): renderable = OnionTileRenderable(self, self.ui.active_art) self.onion_renderables_next.append(renderable) # set camera bounds based on art size @@ -731,7 +731,7 @@ class Application: self.converter_modules[basename] = m except Exception as e: self.log_import_exception(e, basename) - for k, v in m.__dict__.items(): + for v in m.__dict__.values(): if type(v) is not type: continue # don't add duplicates @@ -924,7 +924,7 @@ class Application: if self.get_elapsed_time() - self.last_time > 1000: self.last_time = self.get_elapsed_time() updates = (self.get_elapsed_time() - self.last_time) / self.timestep - for i in range(int(updates)): + for _ in range(int(updates)): if self.game_mode: self.gw.pre_update() self.gw.update() @@ -1152,7 +1152,7 @@ class Application: # fail gracefully if pdoc not found try: import pdoc - except: + except Exception: self.log("pdoc module needed for documentation generation not found.") return for module_name in AUTOGEN_DOC_MODULES: @@ -1223,7 +1223,7 @@ def get_paths(): # so just try and fail :[ try: os.mkdir(new_dir) - except: + except Exception: pass return config_dir, documents_dir, cache_dir @@ -1283,7 +1283,7 @@ def get_app(): try: exec(cfg_line) new_cfg_lines.append(cfg_line + "\n") - except: + except Exception: # find line with "Error", ie the exception name, log that error_lines = traceback.format_exc().split("\n") error = "[an unknown error]" diff --git a/renderable.py b/renderable.py index 9fc82ac..95d907a 100644 --- a/renderable.py +++ b/renderable.py @@ -101,6 +101,7 @@ class TileRenderable: def __str__(self): "for debug purposes, return a concise unique name" + i = 0 for i, r in enumerate(self.art.renderables): if r is self: break diff --git a/renderable_line.py b/renderable_line.py index 7598297..f0ce1e7 100644 --- a/renderable_line.py +++ b/renderable_line.py @@ -492,7 +492,7 @@ class CollisionRenderable(WorldLineRenderable): def get_circle_points(radius, steps=24): angle = 0 points = [(radius, 0)] - for i in range(steps): + for _ in range(steps): angle += math.radians(360 / steps) x = math.cos(angle) * radius y = math.sin(angle) * radius diff --git a/shader.py b/shader.py index c0cf2ee..5c62172 100644 --- a/shader.py +++ b/shader.py @@ -149,7 +149,7 @@ class Shader: new_shader = shaders.compileShader(new_shader_source, shader_type) # TODO: use try_compile_shader instead here, make sure exception passes thru ok self.sl.app.log("ShaderLord: success reloading {}".format(file_to_reload)) - except: + except Exception: self.sl.app.log("ShaderLord: failed reloading {}".format(file_to_reload)) return # recompile program with new shader diff --git a/ui.py b/ui.py index 10490b8..31348ea 100644 --- a/ui.py +++ b/ui.py @@ -287,6 +287,7 @@ class UI: ) def set_active_art_by_filename(self, art_filename): + i = 0 for i, art in enumerate(self.app.art_loaded_for_edit): if art_filename == art.filename: break diff --git a/ui_art_dialog.py b/ui_art_dialog.py index d100e72..bd8ac3f 100644 --- a/ui_art_dialog.py +++ b/ui_art_dialog.py @@ -105,7 +105,7 @@ class NewArtDialog(BaseFileDialog): def is_valid_dimension(self, dimension, max_dimension): try: dimension = int(dimension) - except: + except Exception: return False return 0 < dimension <= max_dimension @@ -420,13 +420,13 @@ class ResizeArtDialog(UIDialog): return False, self.invalid_height_error try: int(self.field_texts[2]) - except: + except Exception: return False, self.invalid_start_error if not 0 <= int(self.field_texts[2]) < self.ui.active_art.width: return False, self.invalid_start_error try: int(self.field_texts[3]) - except: + except Exception: return False, self.invalid_start_error if not 0 <= int(self.field_texts[3]) < self.ui.active_art.height: return False, self.invalid_start_error @@ -435,7 +435,7 @@ class ResizeArtDialog(UIDialog): def is_valid_dimension(self, dimension, max_dimension): try: dimension = int(dimension) - except: + except Exception: return False return 0 < dimension <= max_dimension @@ -477,7 +477,7 @@ class AddFrameDialog(UIDialog): def is_valid_frame_index(self, index): try: index = int(index) - except: + except Exception: return False if index < 1 or index > self.ui.active_art.frames + 1: return False @@ -486,7 +486,7 @@ class AddFrameDialog(UIDialog): def is_valid_frame_delay(self, delay): try: delay = float(delay) - except: + except Exception: return False return delay > 0 @@ -627,7 +627,7 @@ class AddLayerDialog(UIDialog): return False, self.name_exists_error try: z = float(self.field_texts[1]) - except: + except Exception: return False, self.invalid_z_error return True, None @@ -685,7 +685,7 @@ class SetLayerZDialog(UIDialog): def is_input_valid(self): try: z = float(self.field_texts[0]) - except: + except Exception: return False, self.invalid_z_error return True, None @@ -726,7 +726,7 @@ class PaletteFromFileDialog(UIDialog): def valid_colors(self, colors): try: c = int(colors) - except: + except Exception: return False return 2 <= c <= 256 @@ -767,7 +767,7 @@ class SetCameraZoomDialog(UIDialog): def is_input_valid(self): try: zoom = float(self.field_texts[0]) - except: + except Exception: return False, self.invalid_zoom_error if zoom <= 0: return False, self.invalid_zoom_error @@ -799,7 +799,7 @@ class OverlayImageOpacityDialog(UIDialog): def is_input_valid(self): try: opacity = float(self.field_texts[0]) - except: + except Exception: return False, self.invalid_opacity_error if opacity <= 0 or opacity > 100: return False, self.invalid_opacity_error diff --git a/ui_console.py b/ui_console.py index dd756fc..c15275c 100644 --- a/ui_console.py +++ b/ui_console.py @@ -326,7 +326,7 @@ class ConsoleUI(UIElement): self.history_file = open(self.history_filename) try: self.command_history = self.history_file.readlines() - except: + except Exception: self.command_history = [] self.history_file = open(self.history_filename, "a") else: @@ -562,16 +562,16 @@ class ConsoleUI(UIElement): # set some locals for easy access from eval ui = self.ui app = ui.app - camera = app.camera - art = ui.active_art - player = app.gw.player - sel = ( + _camera = app.camera + _art = ui.active_art + _player = app.gw.player + _sel = ( None if len(app.gw.selected_objects) == 0 else app.gw.selected_objects[0] ) - world = app.gw - hud = app.gw.hud + _world = app.gw + _hud = app.gw.hud # special handling of assignment statements, eg x = 3: # detect strings that pattern-match, send them to exec(), # send all other strings to eval() diff --git a/ui_dialog.py b/ui_dialog.py index d8fca14..1ef19d4 100644 --- a/ui_dialog.py +++ b/ui_dialog.py @@ -108,7 +108,7 @@ class UIDialog(UIElement): self.buttons = [self.confirm_button, self.other_button, self.cancel_button] # populate fields with text self.field_texts = [] - for i, field in enumerate(self.fields): + for i in range(len(self.fields)): self.field_texts.append(self.get_initial_field_text(i)) # field cursor starts on self.active_field = 0 diff --git a/ui_edit_panel.py b/ui_edit_panel.py index 930f74c..b807d2a 100644 --- a/ui_edit_panel.py +++ b/ui_edit_panel.py @@ -474,7 +474,7 @@ class EditListPanel(GamePanel): def list_rooms_and_objects(self): items = self.list_rooms() # prefix room names with "ROOM:" - for i, item in enumerate(items): + for item in items: item.name = "ROOM: {}".format(item.name) items += self.list_objects() return items diff --git a/ui_element.py b/ui_element.py index b62088f..cfdc4ee 100644 --- a/ui_element.py +++ b/ui_element.py @@ -177,8 +177,6 @@ class UIElement: elif move_x > 0: self.ui.menu_bar.next_menu() return - old_idx = self.keyboard_nav_index - new_idx = self.keyboard_nav_index + move_y self.keyboard_nav_index += move_y if not self.support_scrolling: # if button list starts at >0 Y, use an offset diff --git a/ui_file_chooser_dialog.py b/ui_file_chooser_dialog.py index 72cf259..8c5d28e 100644 --- a/ui_file_chooser_dialog.py +++ b/ui_file_chooser_dialog.py @@ -264,11 +264,11 @@ class ImageChooserItem(BaseFileChooserItem): # may not be a valid image file try: img = Image.open(self.name) - except: + except Exception: return try: img = img.convert("RGBA") - except: + except Exception: # (probably) PIL bug: some images just crash! return None return img = img.transpose(Image.FLIP_TOP_BOTTOM) diff --git a/ui_game_menu_pulldown_item.py b/ui_game_menu_pulldown_item.py index 1907f88..b92d54b 100644 --- a/ui_game_menu_pulldown_item.py +++ b/ui_game_menu_pulldown_item.py @@ -436,7 +436,7 @@ class GameRoomMenuData(PulldownMenuData): if len(item.label) + 1 > longest_line: longest_line = len(item.label) + 1 # cap at max allowed line length - for room_name, room in app.gw.rooms.items(): + for room_name in app.gw.rooms: class TempMenuItemClass(GameModePulldownMenuItem): pass diff --git a/ui_object_panel.py b/ui_object_panel.py index ab2d65f..c2ca0c0 100644 --- a/ui_object_panel.py +++ b/ui_object_panel.py @@ -31,7 +31,7 @@ class EditObjectPropertyDialog(UIDialog): def is_input_valid(self): try: self.fields[0].type(self.field_texts[0]) - except: + except Exception: return False, "" return True, None diff --git a/ui_popup.py b/ui_popup.py index 18878ed..9414bce 100644 --- a/ui_popup.py +++ b/ui_popup.py @@ -485,7 +485,7 @@ class ToolPopup(UIElement): try: if button.tool_name == tool.name: tool_button = button - except: + except Exception: pass tool_button.y = y + i if tool == self.ui.selected_tool: diff --git a/vector.py b/vector.py index 56b80e4..b369c9b 100644 --- a/vector.py +++ b/vector.py @@ -217,7 +217,7 @@ def world_to_screen(app, world_x, world_y, world_z): viewport = (0, 0, app.window_width, app.window_height) try: x, y, z = GLU.gluProject(world_x, world_y, world_z, vm, pjm, viewport) - except: + except Exception: x, y, z = 0, 0, 0 app.log("GLU.gluProject failed!") # does Z mean anything here?