Custom MibScalarInstance returns noSuchInstance¶
I subclassed
MibScalarInstanceand overridegetValue()to compute the value on every read, but my Agent returnsnoSuchInstance(orgenErrfor SNMPv1) andgetValue()is never called. Why?
The instance was constructed with a schema-only syntax object.
In pyasn1, a type constructed without an argument (for example
OctetString()orInteger32()) is a schema: itsisValueattribute isFalse.MibScalarInstance.readTestandreadGetshort-circuit and raiseNoSuchInstanceErrorwheneverself.syntax.isValueisFalse— they never callgetValue(). This guards against truly uninitialized instances leakingnoSuchInstancesemantics through the agent.The fix is to pass an initial value to the instance constructor, even if your
getValue()override is going to compute the real value at read time. Any value of the right type will do — an empty string, zero, etc. — its purpose is only to flipisValuetoTrue.class DynamicScalar(MibScalarInstance): def getValue(self, name, **context): return self.getSyntax().clone(compute_value_now()) # WRONG: schema-only syntax, getValue() is never called DynamicScalar((1, 3, 6, 1, 4, 1, 60069, 9, 1), (0,), OctetString()) # RIGHT: initialized syntax; getValue() is called on every read DynamicScalar((1, 3, 6, 1, 4, 1, 60069, 9, 1), (0,), OctetString(""))
The same applies to
Integer32(0),ObjectIdentifier((0, 0)), and any other SMI type used as thesyntaxargument of aMibScalarInstance.The schema/value distinction is intentional and is also what the companion
MibScalarobject relies on to advertise type information without claiming a value. Do not work around the check by stripping theisValuetest — change the fixture instead.