From 3b1d8897390fc38a964d7a82f2a4cb0588aa4461 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Wed, 11 Dec 2024 22:37:52 -0500 Subject: [PATCH] temporal: Fix bare except statements with specific error handling (#4805) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Edouard Choinière <27212526+echoix@users.noreply.github.com> --- .flake8 | 3 --- python/grass/temporal/abstract_space_time_dataset.py | 5 ++++- python/grass/temporal/core.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.flake8 b/.flake8 index 36ab602783e..6f7ca25c163 100644 --- a/.flake8 +++ b/.flake8 @@ -48,9 +48,6 @@ per-file-ignores = # TODO: Is this really needed? python/grass/pygrass/vector/__init__.py: E402 python/grass/pygrass/vector/__init__.py: E402 - python/grass/temporal/abstract_space_time_dataset.py: E722 - python/grass/temporal/c_libraries_interface.py: E722 - python/grass/temporal/core.py: E722 python/grass/temporal/datetime_math.py: E722 python/grass/temporal/spatial_topology_dataset_connector.py: E722 python/grass/temporal/temporal_algebra.py: E722 diff --git a/python/grass/temporal/abstract_space_time_dataset.py b/python/grass/temporal/abstract_space_time_dataset.py index 74e38c12e6e..541fc8dd564 100644 --- a/python/grass/temporal/abstract_space_time_dataset.py +++ b/python/grass/temporal/abstract_space_time_dataset.py @@ -19,6 +19,7 @@ class that is the base class for all space time datasets. from abc import ABCMeta, abstractmethod from datetime import datetime from pathlib import Path +from grass.exceptions import FatalError from .abstract_dataset import AbstractDataset, AbstractDatasetComparisonKeyStartTime from .core import ( @@ -2463,7 +2464,9 @@ def is_map_registered(self, map_id, dbif=None): try: dbif.execute(sql, (map_id,), mapset=self.base.mapset) row = dbif.fetchone(mapset=self.base.mapset) - except: + except FatalError: + raise + except Exception: self.msgr.warning(_("Error in register table request")) raise diff --git a/python/grass/temporal/core.py b/python/grass/temporal/core.py index 73cba84b6d3..c350c4fa458 100644 --- a/python/grass/temporal/core.py +++ b/python/grass/temporal/core.py @@ -52,7 +52,7 @@ try: import psycopg2 import psycopg2.extras -except: +except ImportError: pass import atexit @@ -414,7 +414,7 @@ def get_tgis_metadata(dbif=None): statement = "SELECT * FROM tgis_metadata;\n" dbif.execute(statement) rows = dbif.fetchall() - except: + except Exception: rows = None if connection_state_changed: @@ -1501,7 +1501,7 @@ def execute(self, statement, args=None): self.cursor.execute(statement, args) else: self.cursor.execute(statement) - except: + except (sqlite3.Error, psycopg2.Error): if connected: self.close() self.msgr.error(_("Unable to execute :\n %(sql)s") % {"sql": statement}) @@ -1544,7 +1544,7 @@ def execute_transaction(self, statement, mapset=None): else: self.cursor.execute(statement) self.connection.commit() - except: + except (sqlite3.Error, psycopg2.Error): if connected: self.close() self.msgr.error(