diff options
author | Ilya Etingof <etingof@gmail.com> | 2019-02-10 16:38:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-10 16:38:35 +0100 |
commit | 588b9b902d191d8010cb6b247fcb07887d59542c (patch) | |
tree | 419b01d2598e91331db784ac3a6675324aba8c24 /pysnmp/carrier/asyncore | |
parent | 9664858b145140a4fbb2a22b633c1ab41c2555bd (diff) | |
download | pysnmp-git-588b9b902d191d8010cb6b247fcb07887d59542c.tar.gz |
Uppercase global constants (#238)
This is a massive patch essentially upper-casing global/class attributes
that mean to be constants.
Some previously exposed constants have been preserved for compatibility
reasons (notably, in `hlapi`), though the rest might break user code relying
on pysnmp 4.
Diffstat (limited to 'pysnmp/carrier/asyncore')
-rw-r--r-- | pysnmp/carrier/asyncore/base.py | 28 | ||||
-rw-r--r-- | pysnmp/carrier/asyncore/dgram/base.py | 56 | ||||
-rw-r--r-- | pysnmp/carrier/asyncore/dgram/udp.py | 6 | ||||
-rw-r--r-- | pysnmp/carrier/asyncore/dgram/udp6.py | 6 | ||||
-rw-r--r-- | pysnmp/carrier/asyncore/dgram/unix.py | 6 | ||||
-rw-r--r-- | pysnmp/carrier/asyncore/dispatch.py | 6 |
6 files changed, 55 insertions, 53 deletions
diff --git a/pysnmp/carrier/asyncore/base.py b/pysnmp/carrier/asyncore/base.py index e4824f32..035dcbab 100644 --- a/pysnmp/carrier/asyncore/base.py +++ b/pysnmp/carrier/asyncore/base.py @@ -14,37 +14,37 @@ from pysnmp import debug class AbstractSocketTransport(asyncore.dispatcher, AbstractTransport): - protoTransportDispatcher = AsyncoreDispatcher - sockFamily = sockType = None - retryCount = 0 - retryInterval = 0 - bufferSize = 131070 + PROTO_TRANSPORT_DISPATCHER = AsyncoreDispatcher + SOCK_FAMILY = SOCK_TYPE = None + RETRY_COUNT = 0 + RETRY_INTERVAL = 0 + BUFFER_SIZE = 131070 # noinspection PyUnusedLocal def __init__(self, sock=None, sockMap=None): asyncore.dispatcher.__init__(self) if sock is None: - if self.sockFamily is None: + if self.SOCK_FAMILY is None: raise error.CarrierError( 'Address family %s not supported' % self.__class__.__name__ ) - if self.sockType is None: + if self.SOCK_TYPE is None: raise error.CarrierError( 'Socket type %s not supported' % self.__class__.__name__ ) try: - sock = socket.socket(self.sockFamily, self.sockType) + sock = socket.socket(self.SOCK_FAMILY, self.SOCK_TYPE) except socket.error as exc: raise error.CarrierError('socket() failed: %s' % exc) try: for b in socket.SO_RCVBUF, socket.SO_SNDBUF: bsize = sock.getsockopt(socket.SOL_SOCKET, b) - if bsize < self.bufferSize: - sock.setsockopt(socket.SOL_SOCKET, b, self.bufferSize) - debug.logger & debug.flagIO and debug.logger('%s: socket %d buffer size increased from %d to %d for buffer %d' % (self.__class__.__name__, sock.fileno(), bsize, self.bufferSize, b)) + if bsize < self.BUFFER_SIZE: + sock.setsockopt(socket.SOL_SOCKET, b, self.BUFFER_SIZE) + debug.logger & debug.FLAG_IO and debug.logger('%s: socket %d buffer size increased from %d to %d for buffer %d' % (self.__class__.__name__, sock.fileno(), bsize, self.BUFFER_SIZE, b)) except Exception as exc: - debug.logger & debug.flagIO and debug.logger('%s: socket buffer size option mangling failure for buffer: %s' % (self.__class__.__name__, exc)) + debug.logger & debug.FLAG_IO and debug.logger('%s: socket buffer size option mangling failure for buffer: %s' % (self.__class__.__name__, exc)) # The socket map is managed by the AsyncoreDispatcher on # which this transport is registered. Here we just prepare @@ -82,8 +82,10 @@ class AbstractSocketTransport(asyncore.dispatcher, AbstractTransport): self.close() # asyncore API + def handle_close(self): raise error.CarrierError('Transport unexpectedly closed') def handle_error(self): - raise + exc = sys.exc_info()[1] + raise exc diff --git a/pysnmp/carrier/asyncore/dgram/base.py b/pysnmp/carrier/asyncore/dgram/base.py index e9ffc9b0..f103e781 100644 --- a/pysnmp/carrier/asyncore/dgram/base.py +++ b/pysnmp/carrier/asyncore/dgram/base.py @@ -12,23 +12,23 @@ from pysnmp.carrier import sockfix, sockmsg, error from pysnmp import debug # Ignore these socket errors -sockErrors = {errno.ESHUTDOWN: True, - errno.ENOTCONN: True, - errno.ECONNRESET: False, - errno.ECONNREFUSED: False, - errno.EAGAIN: False, - errno.EWOULDBLOCK: False} +SOCK_ERRORS = {errno.ESHUTDOWN: True, + errno.ENOTCONN: True, + errno.ECONNRESET: False, + errno.ECONNREFUSED: False, + errno.EAGAIN: False, + errno.EWOULDBLOCK: False} if hasattr(errno, 'EBADFD'): # bad FD may happen upon FD closure on n-1 select() event - sockErrors[errno.EBADFD] = True + SOCK_ERRORS[errno.EBADFD] = True class DgramSocketTransport(AbstractSocketTransport): - sockType = socket.SOCK_DGRAM - retryCount = 3 - retryInterval = 1 - addressType = lambda x: x + SOCK_TYPE = socket.SOCK_DGRAM + RETRY_COUNT = 3 + RETRY_INTERVAL = 1 + ADDRESS_TYPE = lambda x: x def __init__(self, sock=None, sockMap=None): self.__outQueue = [] @@ -36,7 +36,7 @@ class DgramSocketTransport(AbstractSocketTransport): def __recvfrom(s, sz): d, a = s.recvfrom(sz) - return d, self.addressType(a) + return d, self.ADDRESS_TYPE(a) self._recvfrom = __recvfrom AbstractSocketTransport.__init__(self, sock, sockMap) @@ -64,7 +64,7 @@ class DgramSocketTransport(AbstractSocketTransport): ) except socket.error as exc: raise error.CarrierError('setsockopt() for SO_BROADCAST failed: %s' % exc) - debug.logger & debug.flagIO and debug.logger('enableBroadcast: %s option SO_BROADCAST on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno())) + debug.logger & debug.FLAG_IO and debug.logger('enableBroadcast: %s option SO_BROADCAST on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno())) return self def enablePktInfo(self, flag=1): @@ -82,10 +82,10 @@ class DgramSocketTransport(AbstractSocketTransport): except socket.error as exc: raise error.CarrierError('setsockopt() for %s failed: %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", exc)) - self._sendto = sockmsg.getSendTo(self.addressType) - self._recvfrom = sockmsg.getRecvFrom(self.addressType) + self._sendto = sockmsg.getSendTo(self.ADDRESS_TYPE) + self._recvfrom = sockmsg.getRecvFrom(self.ADDRESS_TYPE) - debug.logger & debug.flagIO and debug.logger('enablePktInfo: %s option %s on socket %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", flag and "enabled" or "disabled", self.socket.fileno())) + debug.logger & debug.FLAG_IO and debug.logger('enablePktInfo: %s option %s on socket %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", flag and "enabled" or "disabled", self.socket.fileno())) return self def enableTransparent(self, flag=1): @@ -105,18 +105,18 @@ class DgramSocketTransport(AbstractSocketTransport): except OSError: raise error.CarrierError('IP_TRANSPARENT socket option requires superuser priveleges') - debug.logger & debug.flagIO and debug.logger('enableTransparent: %s option IP_TRANSPARENT on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno())) + debug.logger & debug.FLAG_IO and debug.logger('enableTransparent: %s option IP_TRANSPARENT on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno())) return self def sendMessage(self, outgoingMessage, transportAddress): self.__outQueue.append( (outgoingMessage, self.normalizeAddress(transportAddress)) ) - debug.logger & debug.flagIO and debug.logger('sendMessage: outgoingMessage queued (%d octets) %s' % (len(outgoingMessage), debug.hexdump(outgoingMessage))) + debug.logger & debug.FLAG_IO and debug.logger('sendMessage: outgoingMessage queued (%d octets) %s' % (len(outgoingMessage), debug.hexdump(outgoingMessage))) def normalizeAddress(self, transportAddress): - if not isinstance(transportAddress, self.addressType): - transportAddress = self.addressType(transportAddress) + if not isinstance(transportAddress, self.ADDRESS_TYPE): + transportAddress = self.ADDRESS_TYPE(transportAddress) if not transportAddress.getLocalAddress(): transportAddress.setLocalAddress(self.getLocalAddress()) @@ -140,17 +140,17 @@ class DgramSocketTransport(AbstractSocketTransport): def handle_write(self): outgoingMessage, transportAddress = self.__outQueue.pop(0) - debug.logger & debug.flagIO and debug.logger('handle_write: transportAddress %r -> %r outgoingMessage (%d octets) %s' % (transportAddress.getLocalAddress(), transportAddress, len(outgoingMessage), debug.hexdump(outgoingMessage))) + debug.logger & debug.FLAG_IO and debug.logger('handle_write: transportAddress %r -> %r outgoingMessage (%d octets) %s' % (transportAddress.getLocalAddress(), transportAddress, len(outgoingMessage), debug.hexdump(outgoingMessage))) if not transportAddress: - debug.logger & debug.flagIO and debug.logger('handle_write: missing dst address, loosing outgoing msg') + debug.logger & debug.FLAG_IO and debug.logger('handle_write: missing dst address, loosing outgoing msg') return try: self._sendto( self.socket, outgoingMessage, transportAddress ) except socket.error as exc: - if exc.args[0] in sockErrors: - debug.logger & debug.flagIO and debug.logger('handle_write: ignoring socket error %s' % exc) + if exc.args[0] in SOCK_ERRORS: + debug.logger & debug.FLAG_IO and debug.logger('handle_write: ignoring socket error %s' % exc) else: raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, exc)) @@ -161,7 +161,7 @@ class DgramSocketTransport(AbstractSocketTransport): try: incomingMessage, transportAddress = self._recvfrom(self.socket, 65535) transportAddress = self.normalizeAddress(transportAddress) - debug.logger & debug.flagIO and debug.logger( + debug.logger & debug.FLAG_IO and debug.logger( 'handle_read: transportAddress %r -> %r incomingMessage (%d octets) %s' % (transportAddress, transportAddress.getLocalAddress(), len(incomingMessage), debug.hexdump(incomingMessage))) if not incomingMessage: self.handle_close() @@ -170,9 +170,9 @@ class DgramSocketTransport(AbstractSocketTransport): self._cbFun(self, transportAddress, incomingMessage) return except socket.error as exc: - if exc.args[0] in sockErrors: - debug.logger & debug.flagIO and debug.logger('handle_read: known socket error %s' % exc) - sockErrors[exc.args[0]] and self.handle_close() + if exc.args[0] in SOCK_ERRORS: + debug.logger & debug.FLAG_IO and debug.logger('handle_read: known socket error %s' % exc) + SOCK_ERRORS[exc.args[0]] and self.handle_close() return else: raise error.CarrierError('recvfrom() failed: %s' % exc) diff --git a/pysnmp/carrier/asyncore/dgram/udp.py b/pysnmp/carrier/asyncore/dgram/udp.py index 08ae5ceb..60bc32a9 100644 --- a/pysnmp/carrier/asyncore/dgram/udp.py +++ b/pysnmp/carrier/asyncore/dgram/udp.py @@ -8,7 +8,7 @@ from socket import AF_INET from pysnmp.carrier.base import AbstractTransportAddress from pysnmp.carrier.asyncore.dgram.base import DgramSocketTransport -domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1) +DOMAIN_NAME = SNMP_UDP_DOMAIN = (1, 3, 6, 1, 6, 1, 1) class UdpTransportAddress(tuple, AbstractTransportAddress): @@ -16,8 +16,8 @@ class UdpTransportAddress(tuple, AbstractTransportAddress): class UdpSocketTransport(DgramSocketTransport): - sockFamily = AF_INET - addressType = UdpTransportAddress + SOCK_FAMILY = AF_INET + ADDRESS_TYPE = UdpTransportAddress UdpTransport = UdpSocketTransport diff --git a/pysnmp/carrier/asyncore/dgram/udp6.py b/pysnmp/carrier/asyncore/dgram/udp6.py index 900ef3c7..ee99674d 100644 --- a/pysnmp/carrier/asyncore/dgram/udp6.py +++ b/pysnmp/carrier/asyncore/dgram/udp6.py @@ -9,7 +9,7 @@ from pysnmp.carrier.base import AbstractTransportAddress from pysnmp.carrier.asyncore.dgram.base import DgramSocketTransport import socket -domainName = snmpUDP6Domain = (1, 3, 6, 1, 2, 1, 100, 1, 2) +DOMAIN_NAME = SNMP_UDP6_DOMAIN = (1, 3, 6, 1, 2, 1, 100, 1, 2) class Udp6TransportAddress(tuple, AbstractTransportAddress): @@ -17,8 +17,8 @@ class Udp6TransportAddress(tuple, AbstractTransportAddress): class Udp6SocketTransport(DgramSocketTransport): - sockFamily = socket.has_ipv6 and socket.AF_INET6 or None - addressType = Udp6TransportAddress + SOCK_FAMILY = socket.has_ipv6 and socket.AF_INET6 or None + ADDRESS_TYPE = Udp6TransportAddress def normalizeAddress(self, transportAddress): if '%' in transportAddress[0]: # strip zone ID diff --git a/pysnmp/carrier/asyncore/dgram/unix.py b/pysnmp/carrier/asyncore/dgram/unix.py index 15f68b2b..486c84f8 100644 --- a/pysnmp/carrier/asyncore/dgram/unix.py +++ b/pysnmp/carrier/asyncore/dgram/unix.py @@ -14,7 +14,7 @@ except ImportError: from pysnmp.carrier.base import AbstractTransportAddress from pysnmp.carrier.asyncore.dgram.base import DgramSocketTransport -domainName = snmpLocalDomain = (1, 3, 6, 1, 2, 1, 100, 1, 13) +DOMAIN_NAME = SNMP_LOCAL_DOMAIN = (1, 3, 6, 1, 2, 1, 100, 1, 13) random.seed() @@ -24,8 +24,8 @@ class UnixTransportAddress(str, AbstractTransportAddress): class UnixSocketTransport(DgramSocketTransport): - sockFamily = AF_UNIX - addressType = UnixTransportAddress + SOCK_FAMILY = AF_UNIX + ADDRESS_TYPE = UnixTransportAddress _iface = '' def openClientMode(self, iface=None): diff --git a/pysnmp/carrier/asyncore/dispatch.py b/pysnmp/carrier/asyncore/dispatch.py index 221c596c..3517bdff 100644 --- a/pysnmp/carrier/asyncore/dispatch.py +++ b/pysnmp/carrier/asyncore/dispatch.py @@ -35,8 +35,7 @@ class AsyncoreDispatcher(AbstractTransportDispatcher): def transportsAreWorking(self): for transport in self.__sockMap.values(): if transport.writable(): - return 1 - return 0 + return True def runDispatcher(self, timeout=0.0): while self.jobsArePending() or self.transportsAreWorking(): @@ -45,6 +44,7 @@ class AsyncoreDispatcher(AbstractTransportDispatcher): use_poll=True, map=self.__sockMap, count=1) except KeyboardInterrupt: raise - except: + + except Exception: raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info()))) self.handleTimerTick(time()) |