]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python: improved try/exception code
authorAleš Mrázek <ales.mrazek@nic.cz>
Sat, 10 Jan 2026 21:41:39 +0000 (22:41 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Mon, 12 Jan 2026 13:47:51 +0000 (14:47 +0100)
python/knot_resolver/client/command.py
python/knot_resolver/controller/interface.py
python/knot_resolver/controller/supervisord/__init__.py
python/knot_resolver/utils/__init__.py
python/knot_resolver/utils/modeling/base_schema.py

index 62e5e0c8cc823a0f45c5e1d83b8cad6405116123..378e86b7fc00dea0beaaa58f1420bd8518bd5821 100644 (file)
@@ -173,12 +173,13 @@ def get_socket_from_config(config: Path, optional_file: bool) -> Optional[Socket
                     f"http://{ip.addr}:{ip.port}",
                     f'Key "/management/interface" in "{config}" file',
                 )
-        return None
     except ValueError as e:
         raise DataValidationError(*e.args) from e  # pylint: disable=no-value-for-parameter
-    except OSError as e:
+    except OSError:
         if not optional_file:
-            raise e
+            raise
+        return None
+    else:
         return None
 
 
index 2685a771c19a7559a042bb3c245929bc40fefa28..848e5749a1824b367b399d80cfafbdd5cfe5d0d0 100644 (file)
@@ -136,9 +136,9 @@ class Subprocess(ABC):
             if self.type is SubprocessType.KRESD:
                 register_worker(self)
                 self._registered_worker = True
-        except KresSubprocessControllerError as e:
+        except KresSubprocessControllerError:
             self._unlink_config()
-            raise e
+            raise
 
     async def apply_new_config(self, new_config: KresConfig) -> None:
         self._config = new_config
index 6fecc1889788507c1e2d7abf6213f1b31a0f6990..c330d5fba581fa26d1a11f55e553543d50d9434f 100644 (file)
@@ -103,9 +103,10 @@ def _is_process_runinng(pid: int) -> bool:
     try:
         # kill with signal 0 is a safe way to test that a process exists
         kill(pid, 0)
-        return True
     except ProcessLookupError:
         return False
+    else:
+        return True
 
 
 async def _is_supervisord_running(config: KresConfig) -> bool:
index 6d8757473888d4b1769df63bd165558578aec9ad..bdf2b464d691a249529419ee0a452feb748f5fd1 100644 (file)
@@ -23,7 +23,7 @@ def ignore_exceptions_optional(
             except BaseException as e:
                 if isinstance(e, exceptions):
                     return default
-                raise e
+                raise
 
         return f
 
index 29080999a7fe7174e658ae53697c4e15e75bccb0..a7a9e9a059c52d9661f4dbe41fc3076f4c46585d 100644 (file)
@@ -278,11 +278,12 @@ class ObjectMapper:
                 raise errs[0]
             if len(errs) > 1:
                 raise AggregateDataValidationError(object_path, child_exceptions=errs)
-            return res
         except AttributeError as e:
             raise DataValidationError(
                 f"Expected dict-like object, but failed to access its .items() method. Value was {obj}", object_path
             ) from e
+        else:
+            return res
 
     def _create_list(self, tp: Type[Any], obj: List[Any], object_path: str) -> List[Any]:
         if isinstance(obj, str):
@@ -502,9 +503,10 @@ class ObjectMapper:
         """Runtime type checking. Validate, that a given object is of a given type."""
         try:
             self.map_object(tp, obj)
-            return True
         except (DataValidationError, ValueError):
             return False
+        else:
+            return True
 
     def _assign_default(self, obj: Any, name: str, python_type: Any, object_path: str) -> None:
         cls = obj.__class__