diff options
author | Ilya Etingof <etingof@gmail.com> | 2019-04-01 18:44:40 +0200 |
---|---|---|
committer | Ilya Etingof <etingof@gmail.com> | 2019-04-02 09:51:22 +0200 |
commit | a22840d214f199fcbe5a6867bc69f390386fe0c8 (patch) | |
tree | 3d2123f7cc927c40a18091eac3d2f4c56b2cc54a | |
parent | a13afa0f44decc9a71ce6ff15e1b6244d0e31319 (diff) | |
download | pysnmp-git-a22840d214f199fcbe5a6867bc69f390386fe0c8.tar.gz |
Replace `imp` with `importlib` is available
-rw-r--r-- | CHANGES.txt | 9 | ||||
-rw-r--r-- | pysnmp/smi/builder.py | 48 |
2 files changed, 34 insertions, 23 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 24d05291..3c9a1ddd 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -99,6 +99,15 @@ Revision 5.0.0, released 2019-03-XX * Global constants turned UPPER_CASED * Long lines wrapped by more or less 80 chars +Revision 4.4.10, released 2019-04-XX +------------------------------------ + +- 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 ----------------------------------- - Made MIB loader ignoring file and directory access errors diff --git a/pysnmp/smi/builder.py b/pysnmp/smi/builder.py index e2d8d42f..eb05d529 100644 --- a/pysnmp/smi/builder.py +++ b/pysnmp/smi/builder.py @@ -4,7 +4,6 @@ # Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> # License: http://snmplabs.com/pysnmp/license.html # -import imp import marshal import os import struct @@ -13,6 +12,22 @@ 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: @@ -33,18 +48,8 @@ from pysnmp import debug 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.FLAG_BLD and debug.logger( - 'trying %s' % self) + debug.logger & debug.FLAG_BLD and debug.logger('trying %s' % self) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self._srcName) @@ -56,10 +61,8 @@ class __AbstractMibSource(object): 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 @@ -89,12 +92,10 @@ class __AbstractMibSource(object): def read(self, f): pycTime = pyTime = -1 - for pycSfx, pycSfxLen, pycMode in self._sfx[imp.PY_COMPILED]: - - pycFile = f + pycSfx + for pycSfx in BYTECODE_SUFFIXES: try: - pycData, pycPath = self._getData(pycFile, pycMode) + pycData, pycPath = self._getData(f + pycSfx, 'rb') except IOError as exc: if ENOENT == -1 or exc.errno == ENOENT: @@ -106,7 +107,7 @@ class __AbstractMibSource(object): 'MIB file %s access error: %s' % (pycFile, exc)) 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:] @@ -120,7 +121,8 @@ class __AbstractMibSource(object): debug.logger & debug.FLAG_BLD and debug.logger( 'bad magic in %s' % pycPath) - for pySfx, pySfxLen, pyMode in self._sfx[imp.PY_SOURCE]: + for pySfx in SOURCE_SUFFIXES: + pyFile = f + pySfx try: @@ -144,7 +146,7 @@ class __AbstractMibSource(object): return marshal.loads(pycData), pycSfx if pyTime != -1: - modData, pyPath = self._getData(pyFile, pyMode) + modData, pyPath = self._getData(f + pySfx, 'r') return compile(modData, pyPath, 'exec'), pyPath raise IOError(ENOENT, 'No suitable module found', f) |