From a613588b29d132b2f26359cbe09a5f8ea6f4320f Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 2 Nov 2023 15:16:42 +0100 Subject: [PATCH] Protect against closing non-existant SNMP handles Snmp.__init__() can now raise exceptions before `self.handle` is ever created, thereby causing errors to be logged from Snmp.__del_ everytime the garbage collector deletes such defunct object. This protects against that slight problem. --- python/nav/Snmp/pynetsnmp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/nav/Snmp/pynetsnmp.py b/python/nav/Snmp/pynetsnmp.py index da619a7cbc..43b25e086b 100644 --- a/python/nav/Snmp/pynetsnmp.py +++ b/python/nav/Snmp/pynetsnmp.py @@ -116,7 +116,7 @@ def __init__( :param priv_password: SNMPv3 privacy password """ - + self.handle = None self.host = host self.community = str(community) self.version = str(version) @@ -197,7 +197,8 @@ def _build_cmdline(self): return tuple(params) def __del__(self): - self.handle.close() + if self.handle: + self.handle.close() def get(self, query="1.3.6.1.2.1.1.1.0"): """Performs an SNMP GET query.