Skip to content

Commit 9dd4bcc

Browse files
committed
Make MIB objects resolution more forgiving
Added optional `ignoreErrors` parameter to `ObjectType.resolveWithMib()` to control that behaviour.
1 parent 5cefacc commit 9dd4bcc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Revision 4.4.10, released 2019-07-29
1616

1717
- Rebased MIB importing code onto `importlib` because `imp` is long
1818
deprecated
19+
- MIB objects resolution made more forgiving to errors, added optional
20+
`ignoreErrors` parameter to `ObjectType.resolveWithMib()` to control
21+
that behaviour.
1922
- Fixed asyncore main loop to respect non-default timer resolution
2023
- Fixed `.setTimerResolution()` behaviour of abstract main loop dispatcher
2124
to update call intervals of the existing periodic dispatcher jobs

pysnmp/smi/rfc1902.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,14 +805,20 @@ def loadMibs(self, *modNames):
805805
self.__args[0].loadMibs(*modNames)
806806
return self
807807

808-
def resolveWithMib(self, mibViewController):
808+
def resolveWithMib(self, mibViewController, ignoreErrors=True):
809809
"""Perform MIB variable ID and associated value conversion.
810810
811811
Parameters
812812
----------
813813
mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
814814
class instance representing MIB browsing functionality.
815815
816+
Other Parameters
817+
----------------
818+
ignoreErrors: :py:class:`bool`
819+
If `True` (default), ignore MIB object name or value casting
820+
failures if possible.
821+
816822
Returns
817823
-------
818824
: :py:class:`~pysnmp.smi.rfc1902.ObjectType`
@@ -851,7 +857,8 @@ class instance representing MIB browsing functionality.
851857

852858
if not isinstance(self.__args[0].getMibNode(),
853859
(MibScalar, MibTableColumn)):
854-
if not isinstance(self.__args[1], AbstractSimpleAsn1Item):
860+
if (ignoreErrors and
861+
not isinstance(self.__args[1], AbstractSimpleAsn1Item)):
855862
raise SmiError('MIB object %r is not OBJECT-TYPE (MIB not loaded?)' % (self.__args[0],))
856863
self.__state |= self.stClean
857864
return self
@@ -866,9 +873,15 @@ class instance representing MIB browsing functionality.
866873
try:
867874
self.__args[1] = self.__args[0].getMibNode().getSyntax().clone(self.__args[1])
868875
except PyAsn1Error:
869-
raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % (
870-
self.__args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class__.__name__, self.__args[1],
871-
sys.exc_info()[1]))
876+
err = ('MIB object %r having type %r failed to cast value '
877+
'%r: %s' % (self.__args[0].prettyPrint(),
878+
self.__args[0].getMibNode().getSyntax().__class__.__name__,
879+
self.__args[1],
880+
sys.exc_info()[1]))
881+
882+
if (not ignoreErrors or
883+
not isinstance(self.__args[1], AbstractSimpleAsn1Item)):
884+
raise SmiError(err)
872885

873886
if rfc1902.ObjectIdentifier().isSuperTypeOf(self.__args[1], matchConstraints=False):
874887
self.__args[1] = ObjectIdentity(self.__args[1]).resolveWithMib(mibViewController)

0 commit comments

Comments
 (0)