From 2b5603ecf98c1c47e94a405bd98054d1703af9e7 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sat, 9 Feb 2019 17:47:15 +0100 Subject: Drop Python < 2.6 except statement compatibility trick --- pysnmp/smi/builder.py | 43 ++++++++++++++++++++----------------------- pysnmp/smi/compiler.py | 4 ++-- pysnmp/smi/mibs/SNMPv2-SMI.py | 41 +++++++++++++++-------------------------- pysnmp/smi/mibs/SNMPv2-TC.py | 24 ++++++++++++------------ pysnmp/smi/rfc1902.py | 8 ++++---- 5 files changed, 53 insertions(+), 67 deletions(-) (limited to 'pysnmp/smi') diff --git a/pysnmp/smi/builder.py b/pysnmp/smi/builder.py index 779f39d4..01273491 100644 --- a/pysnmp/smi/builder.py +++ b/pysnmp/smi/builder.py @@ -80,15 +80,14 @@ class __AbstractMibSource(object): try: pycData, pycPath = self._getData(f + pycSfx, pycMode) - except IOError: - why = sys.exc_info()[1] - if ENOENT == -1 or why.errno == ENOENT: + except IOError as exc: + if ENOENT == -1 or exc.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( - 'file %s access error: %s' % (f + pycSfx, why) + 'file %s access error: %s' % (f + pycSfx, exc) ) else: - raise error.MibLoadError('MIB file %s access error: %s' % (f + pycSfx, why)) + raise error.MibLoadError('MIB file %s access error: %s' % (f + pycSfx, exc)) else: if self.__magic == pycData[:4]: @@ -107,15 +106,14 @@ class __AbstractMibSource(object): try: pyTime = self._getTimestamp(f + pySfx) - except IOError: - why = sys.exc_info()[1] - if ENOENT == -1 or why.errno == ENOENT: + except IOError as exc: + if ENOENT == -1 or exc.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( - 'file %s access error: %s' % (f + pySfx, why) + 'file %s access error: %s' % (f + pySfx, exc) ) else: - raise error.MibLoadError('MIB file %s access error: %s' % (f + pySfx, why)) + raise error.MibLoadError('MIB file %s access error: %s' % (f + pySfx, exc)) else: debug.logger & debug.flagBld and debug.logger('file %s mtime %d' % (f + pySfx, pyTime)) @@ -200,9 +198,9 @@ class ZipMibSource(__AbstractMibSource): try: return self.__loader.get_data(p), p - except Exception: # ZIP code seems to return all kinds of errors - why = sys.exc_info() - raise IOError(ENOENT, 'File or ZIP archive %s access error: %s' % (p, why[1])) + # ZIP code seems to return all kinds of errors + except Exception as exc: + raise IOError(ENOENT, 'File or ZIP archive %s access error: %s' % (p, exc)) class DirMibSource(__AbstractMibSource): @@ -213,18 +211,17 @@ class DirMibSource(__AbstractMibSource): def _listdir(self): try: return self._uniqNames(os.listdir(self._srcName)) - except OSError: - why = sys.exc_info() + except OSError as exc: debug.logger & debug.flagBld and debug.logger( - 'listdir() failed for %s: %s' % (self._srcName, why[1])) + 'listdir() failed for %s: %s' % (self._srcName, exc)) return () def _getTimestamp(self, f): p = os.path.join(self._srcName, f) try: return os.stat(p)[8] - except OSError: - raise IOError(ENOENT, 'No such file: %s' % sys.exc_info()[1], p) + except OSError as exc: + raise IOError(ENOENT, 'No such file: %s' % exc, p) def _getData(self, f, mode): p = os.path.join(self._srcName, '*') @@ -236,15 +233,15 @@ class DirMibSource(__AbstractMibSource): fp.close() return data, p - except (IOError, OSError): - why = sys.exc_info() - msg = 'File or directory %s access error: %s' % (p, why[1]) + except (IOError, OSError) as exc: + msg = 'File or directory %s access error: %s' % (p, exc) else: msg = 'No such file or directory: %s' % p raise IOError(ENOENT, msg) + class MibBuilder(object): defaultCoreMibs = os.pathsep.join( ('pysnmp.smi.mibs.instances', 'pysnmp.smi.mibs') @@ -307,9 +304,9 @@ class MibBuilder(object): try: codeObj, sfx = mibSource.read(modName) - except IOError: + except IOError as exc: debug.logger & debug.flagBld and debug.logger( - 'loadModule: read %s from %s failed: %s' % (modName, mibSource, sys.exc_info()[1])) + 'loadModule: read %s from %s failed: %s' % (modName, mibSource, exc)) continue modPath = mibSource.fullPath(modName, sfx) diff --git a/pysnmp/smi/compiler.py b/pysnmp/smi/compiler.py index b53d10e5..08e38811 100644 --- a/pysnmp/smi/compiler.py +++ b/pysnmp/smi/compiler.py @@ -28,7 +28,7 @@ try: from pysmi.codegen.pysnmp import PySnmpCodeGen, baseMibs from pysmi.compiler import MibCompiler -except ImportError: +except ImportError as exc: from pysnmp.smi import error @@ -40,7 +40,7 @@ except ImportError: return addMibCompiler - addMibCompiler = addMibCompilerDecorator(sys.exc_info()[1]) + addMibCompiler = addMibCompilerDecorator(exc) else: diff --git a/pysnmp/smi/mibs/SNMPv2-SMI.py b/pysnmp/smi/mibs/SNMPv2-SMI.py index 52e3ea11..03a085a1 100644 --- a/pysnmp/smi/mibs/SNMPv2-SMI.py +++ b/pysnmp/smi/mibs/SNMPv2-SMI.py @@ -532,9 +532,7 @@ class ManagedMibObject(ObjectType): except error.NoSuchInstanceError: val = exval.noSuchInstance - except error.SmiError: - exc = sys.exc_info()[1] - + except error.SmiError as exc: (debug.logger & debug.flagIns and debug.logger('%s: exception %r' % (self, exc))) @@ -605,9 +603,7 @@ class ManagedMibObject(ObjectType): except error.NoSuchInstanceError: val = exval.noSuchInstance - except error.SmiError: - exc = sys.exc_info()[1] - + except error.SmiError as exc: (debug.logger & debug.flagIns and debug.logger('%s: exception %r' % (self, exc))) @@ -650,9 +646,7 @@ class ManagedMibObject(ObjectType): except error.NoSuchInstanceError: val = exval.noSuchInstance - except error.SmiError: - exc = sys.exc_info()[1] - + except error.SmiError as exc: (debug.logger & debug.flagIns and debug.logger('%s: exception %r' % (self, exc))) @@ -891,8 +885,7 @@ class ManagedMibObject(ObjectType): try: node = self.getBranch(name, **context) - except (error.NoSuchInstanceError, error.NoSuchObjectError): - exc = sys.exc_info()[1] + except (error.NoSuchInstanceError, error.NoSuchObjectError) as exc: cbFun(varBind, **dict(context, error=exc)) else: @@ -958,8 +951,7 @@ class ManagedMibObject(ObjectType): try: node = self.getBranch(name, **context) - except (error.NoSuchInstanceError, error.NoSuchObjectError): - exc = sys.exc_info()[1] + except (error.NoSuchInstanceError, error.NoSuchObjectError) as exc: cbFun(varBind, **dict(context, error=exc)) else: @@ -1023,8 +1015,7 @@ class ManagedMibObject(ObjectType): try: node = self.getBranch(name, **context) - except (error.NoSuchInstanceError, error.NoSuchObjectError): - exc = sys.exc_info()[1] + except (error.NoSuchInstanceError, error.NoSuchObjectError) as exc: cbFun(varBind, **dict(context, error=exc)) else: @@ -1609,14 +1600,13 @@ class MibScalarInstance(ManagedMibObject): else: return self.syntax.clone(value) - except PyAsn1Error: - exc_t, exc_v, exc_tb = sys.exc_info() + except PyAsn1Error as exc: debug.logger & debug.flagIns and debug.logger('setValue: %s=%r failed with traceback %s' % ( - self.name, value, traceback.format_exception(exc_t, exc_v, exc_tb))) - if isinstance(exc_v, error.TableRowManagement): - raise exc_v + self.name, value, traceback.format_exception(*sys.exc_info()))) + if isinstance(exc, error.TableRowManagement): + raise exc else: - raise error.WrongValueError(name=name, idx=context.get('idx'), msg=exc_v) + raise error.WrongValueError(name=name, idx=context.get('idx'), msg=exc) # # Subtree traversal @@ -1937,16 +1927,14 @@ class MibScalarInstance(ManagedMibObject): try: instances[self.ST_CREATE][idx] = self.setValue(val, name, **context) - except error.MibOperationError: + except error.MibOperationError as exc: # SMI exceptions may carry additional content - exc = sys.exc_info()[1] if 'syntax' in exc: instances[self.ST_CREATE][idx] = exc['syntax'] cbFun(varBind, **dict(context, error=exc)) return else: - exc = sys.exc_info()[1] exc = error.WrongValueError(name=name, idx=context.get('idx'), msg=exc) cbFun(varBind, **dict(context, error=exc)) return @@ -3264,8 +3252,9 @@ class MibTableRow(ManagedMibObject): mibObj, = mibBuilder.importSymbols(modName, symName) try: syntax, instId = self.oidToValue(mibObj.syntax, instId, impliedFlag, indices) - except PyAsn1Error: - debug.logger & debug.flagIns and debug.logger('error resolving table indices at %s, %s: %s' % (self.__class__.__name__, instId, sys.exc_info()[1])) + except PyAsn1Error as exc: + debug.logger & debug.flagIns and debug.logger( + 'error resolving table indices at %s, %s: %s' % (self.__class__.__name__, instId, exc)) indices = [instId] instId = () break diff --git a/pysnmp/smi/mibs/SNMPv2-TC.py b/pysnmp/smi/mibs/SNMPv2-TC.py index 2242b91e..1367e936 100644 --- a/pysnmp/smi/mibs/SNMPv2-TC.py +++ b/pysnmp/smi/mibs/SNMPv2-TC.py @@ -62,9 +62,9 @@ class TextualConvention(object): elif displayHintType == 'd': try: return '%.*f' % (int(decimalPrecision), float(value) / pow(10, int(decimalPrecision))) - except Exception: + except Exception as exc: raise SmiError( - 'float evaluation error: %s' % sys.exc_info()[1] + 'float evaluation error: %s' % exc ) elif displayHintType == 'o': return '0%o' % value @@ -148,10 +148,10 @@ class TextualConvention(object): try: number |= octets.oct2int(numberString[0]) numberString = numberString[1:] - except Exception: + except Exception as exc: raise SmiError( 'Display format eval failure: %s: %s' - % (numberString, sys.exc_info()[1]) + % (numberString, exc) ) if displayFormat == 'x': outputValue += '%02x' % number @@ -210,23 +210,23 @@ class TextualConvention(object): return base.prettyIn(self, -int(value[3:], 16)) else: return base.prettyIn(self, int(value[2:], 16)) - except Exception: + except Exception as exc: raise SmiError( - 'integer evaluation error: %s' % sys.exc_info()[1] + 'integer evaluation error: %s' % exc ) elif displayHintType == 'd': try: return base.prettyIn(self, int(float(value) * 10**int(decimalPrecision))) - except Exception: + except Exception as exc: raise SmiError( - 'float evaluation error: %s' % sys.exc_info()[1] + 'float evaluation error: %s' % exc ) elif displayHintType == 'o' and (value.startswith('0') or value.startswith('-0')): try: return base.prettyIn(self, int(value, 8)) - except Exception: + except Exception as exc: raise SmiError( - 'octal evaluation error: %s' % sys.exc_info()[1] + 'octal evaluation error: %s' % exc ) elif displayHintType == 'b' and (value.startswith('B') or value.startswith('-B')): negative = value.startswith('-') @@ -328,10 +328,10 @@ class TextualConvention(object): try: num = int(octets.octs2str(runningValue[:guessedOctetLength]), numBase[displayFormat]) - except Exception: + except Exception as exc: raise SmiError( 'Display format eval failure: %s: %s' - % (runningValue[:guessedOctetLength], sys.exc_info()[1]) + % (runningValue[:guessedOctetLength], exc) ) num_as_bytes = [] diff --git a/pysnmp/smi/rfc1902.py b/pysnmp/smi/rfc1902.py index 83a1ec28..506434e9 100644 --- a/pysnmp/smi/rfc1902.py +++ b/pysnmp/smi/rfc1902.py @@ -505,9 +505,9 @@ class ObjectIdentity(object): instIds = rowNode.getInstIdFromIndices(*self.__args[2:]) self.__oid += instIds self.__indices = rowNode.getIndicesFromInstId(instIds) - except PyAsn1Error: + except PyAsn1Error as exc: raise SmiError('Instance index %r to OID conversion failure at object %r: %s' % ( - self.__args[2:], mibNode.getLabel(), sys.exc_info()[1])) + self.__args[2:], mibNode.getLabel(), exc)) elif self.__args[2:]: # any other kind of MIB node with indices if self.__args[2:]: instId = rfc1902.ObjectName( @@ -865,10 +865,10 @@ class ObjectType(object): try: self.__args[1] = self.__args[0].getMibNode().getSyntax().clone(self.__args[1]) - except PyAsn1Error: + except PyAsn1Error as exc: raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % ( self.__args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class__.__name__, self.__args[1], - sys.exc_info()[1])) + exc)) if rfc1902.ObjectIdentifier().isSuperTypeOf(self.__args[1], matchConstraints=False): self.__args[1] = ObjectIdentity(self.__args[1]).resolveWithMib(mibViewController) -- cgit v1.2.1