diff options
-rw-r--r-- | CHANGES.txt | 8 | ||||
-rw-r--r-- | pysnmp/smi/builder.py | 44 |
2 files changed, 33 insertions, 19 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 8942df65..90454a5d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -2,9 +2,11 @@ Revision 4.4.10, released 2019-04-XX ------------------------------------ -- Fix to respect non-default timer resolution in asyncore main loop -- Fix to update call interval of the existing periodic dispatcher jobs - on interval change (via .setTimerResolution()) +- Rebased MIB importing code onto `importlib` because `imp` is long + deprecated +- 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 Revision 4.4.9, released 2019-02-09 ----------------------------------- diff --git a/pysnmp/smi/builder.py b/pysnmp/smi/builder.py index 87c6b683..2f55b8eb 100644 --- a/pysnmp/smi/builder.py +++ b/pysnmp/smi/builder.py @@ -6,16 +6,32 @@ # import os import sys -import imp import struct import marshal import time import traceback try: + import importlib + + PY_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER + SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES + BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES + +except ImportError: + import imp + + PY_MAGIC_NUMBER = imp.get_magic() + SOURCE_SUFFIXES = [imp.PY_SOURCE] + BYTECODE_SUFFIXES = [imp.PY_COMPILED] + +PY_SUFFIXES = SOURCE_SUFFIXES + BYTECODE_SUFFIXES + +try: from errno import ENOENT except ImportError: ENOENT = -1 + from pysnmp import version as pysnmp_version from pysnmp.smi import error from pysnmp import debug @@ -31,13 +47,7 @@ else: class __AbstractMibSource(object): def __init__(self, srcName): self._srcName = srcName - self.__magic = imp.get_magic() - self.__sfx = {} self.__inited = None - for sfx, mode, typ in imp.get_suffixes(): - if typ not in self.__sfx: - self.__sfx[typ] = [] - self.__sfx[typ].append((sfx, len(sfx), mode)) debug.logger & debug.flagBld and debug.logger('trying %s' % self) def __repr__(self): @@ -45,13 +55,13 @@ class __AbstractMibSource(object): def _uniqNames(self, files): u = set() + for f in files: if f.startswith('__init__.'): continue - for typ in (imp.PY_SOURCE, imp.PY_COMPILED): - for sfx, sfxLen, mode in self.__sfx[typ]: - if f[-sfxLen:] == sfx: - u.add(f[:-sfxLen]) + + u.update(f[:-len(sfx)] for sfx in PY_SUFFIXES if f.endswith(sfx)) + return tuple(u) # MibSource API follows @@ -76,9 +86,10 @@ class __AbstractMibSource(object): def read(self, f): pycTime = pyTime = -1 - for pycSfx, pycSfxLen, pycMode in self.__sfx[imp.PY_COMPILED]: + for pycSfx in BYTECODE_SUFFIXES: + try: - pycData, pycPath = self._getData(f + pycSfx, pycMode) + pycData, pycPath = self._getData(f + pycSfx, 'rb') except IOError: why = sys.exc_info()[1] @@ -91,7 +102,7 @@ class __AbstractMibSource(object): raise error.MibLoadError('MIB file %s access error: %s' % (f + pycSfx, why)) else: - if self.__magic == pycData[:4]: + if PY_MAGIC_NUMBER == pycData[:4]: pycData = pycData[4:] pycTime = struct.unpack('<L', pycData[:4])[0] pycData = pycData[4:] @@ -103,7 +114,8 @@ class __AbstractMibSource(object): else: debug.logger & debug.flagBld and debug.logger('bad magic in %s' % pycPath) - for pySfx, pySfxLen, pyMode in self.__sfx[imp.PY_SOURCE]: + for pySfx in SOURCE_SUFFIXES: + try: pyTime = self._getTimestamp(f + pySfx) @@ -125,7 +137,7 @@ class __AbstractMibSource(object): return marshal.loads(pycData), pycSfx if pyTime != -1: - modData, pyPath = self._getData(f + pySfx, pyMode) + modData, pyPath = self._getData(f + pySfx, 'r') return compile(modData, pyPath, 'exec'), pyPath raise IOError(ENOENT, 'No suitable module found', f) |