Skip to content

Commit

Permalink
Added a workaround for #139.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Nov 4, 2024
1 parent 78b8a2e commit b5a542e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pysnmp/smi/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import time
import traceback
from typing import Any
import warnings
from errno import ENOENT
from importlib.machinery import BYTECODE_SUFFIXES, SOURCE_SUFFIXES
Expand Down Expand Up @@ -447,7 +448,7 @@ def unload_modules(self, *modNames):

return self

def import_symbols(self, modName, *symNames, **userCtx):
def import_symbols(self, modName, *symNames, **userCtx) -> "tuple[Any, ...]":
"""Import MIB symbols."""
if not modName:
raise error.SmiError("importSymbols: empty MIB module name")
Expand Down
2 changes: 1 addition & 1 deletion pysnmp/smi/mibs/SNMPv2-TC.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def prettyIn(self, value): # override asn1 type method

if self.displayHint and (
self.__integer.isSuperTypeOf(self, matchConstraints=False)
and self.getNamedValues()
and not self.getNamedValues()
or self.__unsigned32.isSuperTypeOf(self, matchConstraints=False)
or self.__timeticks.isSuperTypeOf(self, matchConstraints=False)
):
Expand Down
2 changes: 2 additions & 0 deletions pysnmp/smi/rfc1902.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,11 @@ class instance representing MIB browsing functionality.
return self

try:
old_value = self.__args[1]._value
self.__args[1] = (
object_identity.get_mib_node().getSyntax().clone(self.__args[1])
)
self.__args[1]._value = old_value # force to keep the original value
except PyAsn1Error:
err = "MIB object %r having type %r failed to cast value " "%r: %s" % (
object_identity.prettyPrint(),
Expand Down
6 changes: 2 additions & 4 deletions tests/agent_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,11 @@ def get_fixed_date_and_time():
MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 7), v2c.Integer32()).setMaxAccess(
"read-write"
),
MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 7), (0,), v2c.Integer32(50)),
MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 7), (0,), v2c.Integer32(5)),
MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 8), v2c.Unsigned32()).setMaxAccess(
"read-write"
),
MibScalarInstance(
(1, 3, 6, 1, 4, 1, 60069, 9, 8), (0,), v2c.Unsigned32(50)
),
MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 8), (0,), v2c.Unsigned32(5)),
)

# --- end of Managed Object Instance initialization ----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async def test_v1_get_scaled_integer_object():
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == "LEXTUDIO-TEST-MIB::testScaledInteger.0"
assert (
varBinds[0][1].prettyPrint() == "5.0"
varBinds[0][1].prettyPrint() == "0.5"
) # IMPORTANT: test display hint "d-1".


Expand Down Expand Up @@ -267,5 +267,5 @@ async def test_v1_get_scaled_unsigned_object():
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == "LEXTUDIO-TEST-MIB::testScaledUnsigned.0"
assert (
varBinds[0][1].prettyPrint() == "50" # GitHub issue #139
varBinds[0][1].prettyPrint() == "0.5" # GitHub issue #139
) # IMPORTANT: test display hint "d-1".
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,37 @@ def test_configure_mib_viewer_and_resolve_pdu_varbinds_full():
for i, varBind in enumerate(varBinds):
assert isinstance(varBind, rfc1902.ObjectType)
assert varBind.prettyPrint() == expected_varbinds[i]


def test_syntax_integer():
# Create MIB builder
mib_builder = builder.MibBuilder()

# Load MIB modules
compiler.addMibCompiler(mib_builder)
mib_builder.loadModules("LEXTUDIO-TEST-MIB")

# Get MIB object
(mib_object,) = mib_builder.import_symbols("LEXTUDIO-TEST-MIB", "testScaledInteger")

# Print MIB object syntax
assert (1, 3, 6, 1, 4, 1, 60069, 9, 7) == mib_object.getName()
assert "ScaledInteger" == mib_object.syntax.__class__.__name__


def test_syntax_unsigned():
# Create MIB builder
mib_builder = builder.MibBuilder()

# Load MIB modules
compiler.addMibCompiler(mib_builder)
mib_builder.loadModules("LEXTUDIO-TEST-MIB")

# Get MIB object
(mib_object,) = mib_builder.import_symbols(
"LEXTUDIO-TEST-MIB", "testScaledUnsigned"
)

# Print MIB object syntax
assert (1, 3, 6, 1, 4, 1, 60069, 9, 8) == mib_object.getName()
assert "ScaledUnsigned" == mib_object.syntax.__class__.__name__

0 comments on commit b5a542e

Please sign in to comment.