Skip to content

Commit

Permalink
Removed pyasn1.compat dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 18, 2024
1 parent 09bec27 commit 85bc0f1
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 79 deletions.
3 changes: 1 addition & 2 deletions pysnmp/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import logging
import warnings

from pyasn1.compat.octets import octs2ints

from pysnmp import __version__, error

Expand Down Expand Up @@ -149,6 +148,6 @@ def hexdump(octets):
return " ".join(
[
"{}{:02X}".format(n % 16 == 0 and ("\n%.5d: " % n) or "", x)
for n, x in zip(range(len(octets)), octs2ints(octets))
for n, x in zip(range(len(octets)), octets)
]
)
19 changes: 9 additions & 10 deletions pysnmp/entity/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#
import warnings

from pyasn1.compat.octets import null

from pysnmp import debug, error
from pysnmp.carrier.asyncio.dgram import udp, udp6
Expand Down Expand Up @@ -156,7 +155,7 @@ def addV1System(
contextEngineId = snmpEngineID.syntax.clone(contextEngineId)

if contextName is None:
contextName = null
contextName = b""

securityName = securityName is not None and securityName or communityIndex

Expand Down Expand Up @@ -295,7 +294,7 @@ def addV3User(
masterAuthKey = localAuthKey = authKey

if authKeyType < USM_KEY_TYPE_MASTER: # pass phrase is given
masterAuthKey = AUTH_SERVICES[authProtocol].hashPassphrase(authKey or null)
masterAuthKey = AUTH_SERVICES[authProtocol].hashPassphrase(authKey or b"")

if authKeyType < USM_KEY_TYPE_LOCALIZED: # pass phrase or master key is given
localAuthKey = AUTH_SERVICES[authProtocol].localizeKey(
Expand All @@ -312,7 +311,7 @@ def addV3User(

if privKeyType < USM_KEY_TYPE_MASTER: # pass phrase is given
masterPrivKey = PRIV_SERVICES[privProtocol].hashPassphrase(
authProtocol, privKey or null
authProtocol, privKey or b""
)

if privKeyType < USM_KEY_TYPE_LOCALIZED: # pass phrase or master key is given
Expand Down Expand Up @@ -505,7 +504,7 @@ def addTargetAddr(
params: str,
timeout: "float | None" = None,
retryCount: "int | None" = None,
tagList=null,
tagList=b"",
sourceAddress=None,
):
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
Expand Down Expand Up @@ -815,7 +814,7 @@ def addVacmUser(
readSubTree=(),
writeSubTree=(),
notifySubTree=(),
contextName=null,
contextName=b"",
):
(groupName, securityLevel, readView, writeView, notifyView) = __cookVacmUserInfo(
snmpEngine, securityModel, securityName, securityLevel
Expand All @@ -834,11 +833,11 @@ def addVacmUser(
notifyView,
)
if readSubTree:
addVacmView(snmpEngine, readView, "included", readSubTree, null)
addVacmView(snmpEngine, readView, "included", readSubTree, b"")
if writeSubTree:
addVacmView(snmpEngine, writeView, "included", writeSubTree, null)
addVacmView(snmpEngine, writeView, "included", writeSubTree, b"")
if notifySubTree:
addVacmView(snmpEngine, notifyView, "included", notifySubTree, null)
addVacmView(snmpEngine, notifyView, "included", notifySubTree, b"")


def delVacmUser(
Expand All @@ -849,7 +848,7 @@ def delVacmUser(
readSubTree=(),
writeSubTree=(),
notifySubTree=(),
contextName=null,
contextName=b"",
):
(groupName, securityLevel, readView, writeView, notifyView) = __cookVacmUserInfo(
snmpEngine, securityModel, securityName, securityLevel
Expand Down
4 changes: 1 addition & 3 deletions pysnmp/entity/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import tempfile
from typing import Any, Dict

from pyasn1.compat.octets import str2octs


from pysnmp import debug, error
from pysnmp.carrier.base import AbstractTransportAddress, AbstractTransportDispatcher
Expand Down Expand Up @@ -160,7 +158,7 @@ def __init__(

try:
fd, fn = tempfile.mkstemp(dir=persistentPath)
os.write(fd, str2octs(snmpEngineBoots.syntax.prettyPrint()))
os.write(fd, snmpEngineBoots.syntax.prettyPrint().encode("iso-8859-1"))
os.close(fd)
shutil.move(fn, f)
except Exception:
Expand Down
7 changes: 3 additions & 4 deletions pysnmp/entity/rfc3413/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Copyright (c) 2005-2020, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.compat.octets import null
from pyasn1.type import univ


Expand All @@ -27,7 +26,7 @@ def __init__(self, snmpEngine, contextEngineId=None):
f'SnmpContext: contextEngineId "{self.contextEngineId!r}"'
)
self.contextNames = {
null: snmpEngine.msgAndPduDsp.mibInstrumController # Default name
b"": snmpEngine.msgAndPduDsp.mibInstrumController # Default name
}

def registerContextName(self, contextName, mibInstrum=None):
Expand All @@ -38,7 +37,7 @@ def registerContextName(self, contextName, mibInstrum=None):
f"registerContextName: registered contextName {contextName!r}, mibInstrum {mibInstrum!r}"
)
if mibInstrum is None:
self.contextNames[contextName] = self.contextNames[null]
self.contextNames[contextName] = self.contextNames[b""]
else:
self.contextNames[contextName] = mibInstrum

Expand All @@ -50,7 +49,7 @@ def unregisterContextName(self, contextName):
)
del self.contextNames[contextName]

def getMibInstrum(self, contextName=null):
def getMibInstrum(self, contextName=b""):
contextName = univ.OctetString(contextName).asOctets()
if contextName not in self.contextNames:
debug.logger & debug.FLAG_INS and debug.logger(
Expand Down
3 changes: 1 addition & 2 deletions pysnmp/entity/rfc3413/ntforg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#
import sys

from pyasn1.compat.octets import null

from pysnmp import debug, nextid
from pysnmp.entity.rfc3413 import config
from pysnmp.proto import errind, error, rfc3411
from pysnmp.proto.api import v2c
from pysnmp.proto.proxy import rfc2576
from pysnmp.smi import rfc1902, view


getNextHandle = nextid.Integer(0x7FFFFFFF) # noqa: N816

Expand Down
7 changes: 2 additions & 5 deletions pysnmp/entity/rfc3413/ntfrcv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#
import sys

from pyasn1.compat.octets import null

from pysnmp import debug
from pysnmp.proto import error, rfc3411
Expand All @@ -24,7 +23,7 @@ class NotificationReceiver:

def __init__(self, snmpEngine, cbFun, cbCtx=None):
snmpEngine.msgAndPduDsp.registerContextEngineId(
null, self.SUPPORTED_PDU_TYPES, self.processPdu # '' is a wildcard
b"", self.SUPPORTED_PDU_TYPES, self.processPdu # '' is a wildcard
)

self.__snmpTrapCommunity = ""
Expand All @@ -39,9 +38,7 @@ def storeSnmpTrapCommunity(snmpEngine, execpoint, variables, cbCtx):
)

def close(self, snmpEngine):
snmpEngine.msgAndPduDsp.unregisterContextEngineId(
null, self.SUPPORTED_PDU_TYPES
)
snmpEngine.msgAndPduDsp.unregisterContextEngineId(b"", self.SUPPORTED_PDU_TYPES)
self.__cbFun = self.__cbCtx = None

def processPdu(
Expand Down
3 changes: 1 addition & 2 deletions pysnmp/hlapi/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#
from typing import Tuple

from pyasn1.compat.octets import null

from pysnmp import error
from pysnmp.carrier.base import AbstractTransport, AbstractTransportAddress
Expand All @@ -29,7 +28,7 @@ def __init__(
transportAddr: Tuple,
timeout: float = 1,
retries: int = 5,
tagList=null,
tagList=b"",
):
self.transportAddr = self._resolveAddr(transportAddr)
self.timeout = timeout
Expand Down
8 changes: 4 additions & 4 deletions pysnmp/hlapi/v3arch/asyncio/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
import warnings

from pyasn1.compat.octets import null

from pysnmp import error
from pysnmp.entity import config

Expand Down Expand Up @@ -118,8 +118,8 @@ class instance.
mpModel = 1 # Default is SMIv2
securityModel = mpModel + 1
securityLevel = "noAuthNoPriv"
contextName = null
tag = null
contextName = b""
tag = b""

def __init__(
self,
Expand Down Expand Up @@ -386,7 +386,7 @@ class instance.
securityLevel = "noAuthNoPriv"
securityModel = 3
mpModel = 3
contextName = null
contextName = b""

def __init__(
self,
Expand Down
4 changes: 1 addition & 3 deletions pysnmp/hlapi/v3arch/asyncio/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Copyright (c) 2005-2020, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.compat.octets import null

__all__ = ["ContextData"]


Expand Down Expand Up @@ -46,7 +44,7 @@ class ContextData:
"""

def __init__(self, contextEngineId=None, contextName=null):
def __init__(self, contextEngineId=None, contextName=b""):
self.contextEngineId = contextEngineId
self.contextName = contextName

Expand Down
9 changes: 3 additions & 6 deletions pysnmp/hlapi/v3arch/asyncio/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
# Copyright (c) 2005-2020, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.compat.octets import null


from pysnmp import error, nextid
from pysnmp.entity import config
from pysnmp.entity.engine import SnmpEngine
Expand Down Expand Up @@ -39,7 +36,7 @@ class CommandGeneratorLcdConfigurator(AbstractLcdConfigurator):
cacheKeys = ["auth", "parm", "tran", "addr"]

def configure(
self, snmpEngine, authData, transportTarget, contextName=null, **options
self, snmpEngine, authData, transportTarget, contextName=b"", **options
):
cache = self._getCache(snmpEngine)
if isinstance(authData, CommunityData):
Expand Down Expand Up @@ -140,7 +137,7 @@ def configure(

return addrName, paramsName

def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
def unconfigure(self, snmpEngine, authData=None, contextName=b"", **options):
cache = self._getCache(snmpEngine)
if authData:
if isinstance(authData, CommunityData):
Expand Down Expand Up @@ -289,7 +286,7 @@ def configure(

return notifyName

def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
def unconfigure(self, snmpEngine, authData=None, contextName=b"", **options):
cache = self._getCache(snmpEngine)
if authData:
authDataKey = (
Expand Down
6 changes: 2 additions & 4 deletions pysnmp/proto/mpmod/rfc2576.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import sys

from pyasn1.codec.ber import decoder, eoo
from pyasn1.compat.octets import null
from pyasn1.error import PyAsn1Error
from pyasn1.type import univ

from pysnmp import debug
Expand Down Expand Up @@ -67,7 +65,7 @@ def prepareOutgoingMessage(

# rfc3412: 7.1.5
if not contextName:
contextName = null
contextName = b""

debug.logger & debug.FLAG_MP and debug.logger(
f"prepareOutgoingMessage: using contextEngineId {contextEngineId!r} contextName {contextName!r}"
Expand Down Expand Up @@ -229,7 +227,7 @@ def prepareResponseMessage(

# rfc3412: 7.1.5
if not contextName:
contextName = null
contextName = b""

# rfc3412: 7.1.6
scopedPDU = (contextEngineId, contextName, pdu)
Expand Down
8 changes: 4 additions & 4 deletions pysnmp/proto/rfc3412.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
import sys

from pyasn1.compat.octets import null

from pyasn1.error import PyAsn1Error

from pysnmp import debug, nextid
Expand Down Expand Up @@ -107,7 +107,7 @@ def getRegisteredApp(self, contextEngineId, pduType):
k = (contextEngineId, pduType)
if k in self.__appsRegistration:
return self.__appsRegistration[k]
k = (null, pduType)
k = (b"", pduType)
if k in self.__appsRegistration:
return self.__appsRegistration[k] # wildcard

Expand Down Expand Up @@ -366,15 +366,15 @@ def receiveMessage(self, snmpEngine, transportDomain, transportAddress, wholeMsg

# 4.2.1.2
try:
restOfWholeMsg = null # XXX fix decoder non-recursive return
restOfWholeMsg = b"" # XXX fix decoder non-recursive return
msgVersion = verdec.decodeMessageVersion(wholeMsg)

except error.ProtocolError:
(snmpInASNParseErrs,) = self.mibInstrumController.mibBuilder.importSymbols(
"__SNMPv2-MIB", "snmpInASNParseErrs" # type: ignore
)
snmpInASNParseErrs.syntax += 1
return null # n.b the whole buffer gets dropped
return b"" # n.b the whole buffer gets dropped

debug.logger & debug.FLAG_DSP and debug.logger(
"receiveMessage: msgVersion %s, msg decoded" % msgVersion
Expand Down
1 change: 0 additions & 1 deletion pysnmp/proto/secmod/eso/priv/des3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import random
from hashlib import md5, sha1

from pyasn1.compat.octets import null
from pyasn1.type import univ

from pysnmp.proto import errind, error
Expand Down
Loading

0 comments on commit 85bc0f1

Please sign in to comment.