Skip to content

Commit

Permalink
Make MIB objects resolution more forgiving
Browse files Browse the repository at this point in the history
Added optional `ignoreErrors` parameter to `ObjectType.resolveWithMib()`
to control that behaviour.
  • Loading branch information
etingof committed Jul 30, 2019
1 parent 5cefacc commit 9dd4bcc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Revision 4.4.10, released 2019-07-29

- Rebased MIB importing code onto `importlib` because `imp` is long
deprecated
- MIB objects resolution made more forgiving to errors, added optional
`ignoreErrors` parameter to `ObjectType.resolveWithMib()` to control
that behaviour.
- Fixed asyncore main loop to respect non-default timer resolution
- Fixed `.setTimerResolution()` behaviour of abstract main loop dispatcher
to update call intervals of the existing periodic dispatcher jobs
Expand Down
23 changes: 18 additions & 5 deletions pysnmp/smi/rfc1902.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,20 @@ def loadMibs(self, *modNames):
self.__args[0].loadMibs(*modNames)
return self

def resolveWithMib(self, mibViewController):
def resolveWithMib(self, mibViewController, ignoreErrors=True):
"""Perform MIB variable ID and associated value conversion.
Parameters
----------
mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
class instance representing MIB browsing functionality.
Other Parameters
----------------
ignoreErrors: :py:class:`bool`
If `True` (default), ignore MIB object name or value casting
failures if possible.
Returns
-------
: :py:class:`~pysnmp.smi.rfc1902.ObjectType`
Expand Down Expand Up @@ -851,7 +857,8 @@ class instance representing MIB browsing functionality.

if not isinstance(self.__args[0].getMibNode(),
(MibScalar, MibTableColumn)):
if not isinstance(self.__args[1], AbstractSimpleAsn1Item):
if (ignoreErrors and
not isinstance(self.__args[1], AbstractSimpleAsn1Item)):
raise SmiError('MIB object %r is not OBJECT-TYPE (MIB not loaded?)' % (self.__args[0],))
self.__state |= self.stClean
return self
Expand All @@ -866,9 +873,15 @@ class instance representing MIB browsing functionality.
try:
self.__args[1] = self.__args[0].getMibNode().getSyntax().clone(self.__args[1])
except PyAsn1Error:
raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % (
self.__args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class__.__name__, self.__args[1],
sys.exc_info()[1]))
err = ('MIB object %r having type %r failed to cast value '
'%r: %s' % (self.__args[0].prettyPrint(),
self.__args[0].getMibNode().getSyntax().__class__.__name__,
self.__args[1],
sys.exc_info()[1]))

if (not ignoreErrors or
not isinstance(self.__args[1], AbstractSimpleAsn1Item)):
raise SmiError(err)

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

0 comments on commit 9dd4bcc

Please sign in to comment.