From c330f4da13d87ec78de5948127d2a9fb137ec846 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Fri, 22 Nov 2024 10:53:03 +0100 Subject: [PATCH 1/2] Remove unused error variables --- python/nav/bin/macwatch.py | 2 +- python/nav/bin/radiusparser.py | 2 +- python/nav/bin/snmptrapd.py | 4 ++-- python/nav/eventengine/severity.py | 2 +- python/nav/portadmin/snmp/base.py | 2 +- python/nav/portadmin/snmp/cisco.py | 8 ++++---- python/nav/smsd/gammudispatcher.py | 2 +- python/nav/statemon/checker/MysqlChecker.py | 2 +- python/nav/statemon/db.py | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/python/nav/bin/macwatch.py b/python/nav/bin/macwatch.py index 851a8b1b54..5f9f76ba0f 100755 --- a/python/nav/bin/macwatch.py +++ b/python/nav/bin/macwatch.py @@ -94,7 +94,7 @@ def post_event(mac_watch, cam): event['alerttype'] = 'macWarning' try: event.post() - except Exception as why: + except Exception: _logger.exception("Unhandled exception while posting event") return False return True diff --git a/python/nav/bin/radiusparser.py b/python/nav/bin/radiusparser.py index 2af7373f9c..693a1e9836 100755 --- a/python/nav/bin/radiusparser.py +++ b/python/nav/bin/radiusparser.py @@ -98,7 +98,7 @@ def main(args=None): # Check if the line is parseable try: row = Row(parse_line(line)) - except AttributeError as e: + except AttributeError: print("AttributeError: " + line) # We want to look for octals in the messages diff --git a/python/nav/bin/snmptrapd.py b/python/nav/bin/snmptrapd.py index 783ffdbbc1..3c852c25da 100755 --- a/python/nav/bin/snmptrapd.py +++ b/python/nav/bin/snmptrapd.py @@ -144,7 +144,7 @@ def main(): server.listen(opts.community, trap_handler) except SystemExit: raise - except Exception as why: + except Exception: _logger.critical("Fatal exception ocurred", exc_info=True) else: @@ -153,7 +153,7 @@ def main(): try: _logger.info("Listening on %s", addresses_text) server.listen(opts.community, trap_handler) - except KeyboardInterrupt as why: + except KeyboardInterrupt: _logger.error("Received keyboard interrupt, exiting.") server.close() diff --git a/python/nav/eventengine/severity.py b/python/nav/eventengine/severity.py index d9277aed2a..8b1c83059f 100644 --- a/python/nav/eventengine/severity.py +++ b/python/nav/eventengine/severity.py @@ -171,7 +171,7 @@ def _parse_rule_sublist( if attr == "severity": try: modifier = cls._parse_modifier(value) - except ValueError as error: + except ValueError: raise ValueError( f"{value!r} is an invalid severity modifier expression" ) from None diff --git a/python/nav/portadmin/snmp/base.py b/python/nav/portadmin/snmp/base.py index 076146b91b..70c65475fb 100644 --- a/python/nav/portadmin/snmp/base.py +++ b/python/nav/portadmin/snmp/base.py @@ -234,7 +234,7 @@ def test_read(self): try: handle.get(self.SYSOBJECTID) return True - except SnmpError as error: + except SnmpError: return False def test_write(self): diff --git a/python/nav/portadmin/snmp/cisco.py b/python/nav/portadmin/snmp/cisco.py index 3fc1ffd168..7d0b330e2a 100644 --- a/python/nav/portadmin/snmp/cisco.py +++ b/python/nav/portadmin/snmp/cisco.py @@ -148,7 +148,7 @@ def set_cisco_voice_vlan(self, interface, voice_vlan): ) except SnmpError as error: _logger.error('Error setting voice vlan: %s', error) - except ValueError as error: + except ValueError: _logger.error('%s is not a valid voice vlan', voice_vlan) raise @@ -159,7 +159,7 @@ def enable_cisco_cdp(self, interface): """Enable CDP using Cisco specific oid""" try: return self._set_netbox_value(self.cdp_oid, interface.ifindex, 'i', 1) - except ValueError as error: + except ValueError: _logger.error('%s is not a valid option for cdp', 1) raise @@ -173,7 +173,7 @@ def disable_cisco_cdp(self, interface): """Disable CDP using Cisco specific oid""" try: return self._set_netbox_value(self.cdp_oid, interface.ifindex, 'i', 2) - except ValueError as error: + except ValueError: _logger.error('%s is not a valid option for cdp', 2) raise @@ -339,7 +339,7 @@ def set_poe_state(self, interface: manage.Interface, state: PoeState): except SnmpError as error: _logger.error('Error setting poe state: %s', error) raise - except ValueError as error: + except ValueError: _logger.error('%s is not a valid option for poe state', state) raise diff --git a/python/nav/smsd/gammudispatcher.py b/python/nav/smsd/gammudispatcher.py index 22979f470a..d96971d6a4 100644 --- a/python/nav/smsd/gammudispatcher.py +++ b/python/nav/smsd/gammudispatcher.py @@ -29,7 +29,7 @@ try: import gammu -except ImportError as error: +except ImportError: raise PermanentDispatcherError('python-gammu not installed or misconfigured.') diff --git a/python/nav/statemon/checker/MysqlChecker.py b/python/nav/statemon/checker/MysqlChecker.py index 64c0e9926b..8ea4d2c675 100644 --- a/python/nav/statemon/checker/MysqlChecker.py +++ b/python/nav/statemon/checker/MysqlChecker.py @@ -53,7 +53,7 @@ def execute(self): conn.write_auth_packet('navmon') try: conn.read_packet() - except MysqlError as err: + except MysqlError: pass # Ignore login error return Event.UP, 'OK' diff --git a/python/nav/statemon/db.py b/python/nav/statemon/db.py index e4ca00c95d..eaa247abe5 100644 --- a/python/nav/statemon/db.py +++ b/python/nav/statemon/db.py @@ -129,7 +129,7 @@ def cursor(self): err.pgcode, ) raise - except Exception as err: + except Exception: if self.db is not None: _logger.critical( "Could not get cursor. Trying to reconnect...", exc_info=True From 09c4d7e2bfccba8cbe9a319cec37d4c7ea1e71c8 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Fri, 22 Nov 2024 10:53:25 +0100 Subject: [PATCH 2/2] Explicitely feed variables to log message Instead of using locals() --- python/nav/eventengine/topology.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/nav/eventengine/topology.py b/python/nav/eventengine/topology.py index 7c2297d55e..f09feca5a4 100644 --- a/python/nav/eventengine/topology.py +++ b/python/nav/eventengine/topology.py @@ -265,9 +265,9 @@ def get_source_address_for(dest): sock.connect(sockaddr) except socket.error as err: _logger.warning( - "Error when getting NAV's source address for " - "connecting to %(dest)s: %(err)s", - locals(), + "Error when getting NAV's source address for connecting to %s: %s", + dest, + err, ) return addrinfo = sock.getsockname()