summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2019-04-01 18:44:40 +0200
committerIlya Etingof <etingof@gmail.com>2019-04-01 18:44:40 +0200
commitc3d6619176a108758e1c7f0622cb64766471b27d (patch)
tree44504d5f40966915b89a3a3aa1d07b6aff6ec54b
parent8a6dc6331cc667840166a25406057794f11fe8ec (diff)
downloadpysnmp-git-c3d6619176a108758e1c7f0622cb64766471b27d.tar.gz
Replace `imp` with `importlib` is available
-rw-r--r--CHANGES.txt8
-rw-r--r--pysnmp/smi/builder.py44
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)