fix various bugs found by the 'prospector' static-analysis tool

This commit is contained in:
Andrew Scheller
2016-04-07 16:12:17 +01:00
parent 4f7e1f003e
commit 72ca075668
13 changed files with 33 additions and 34 deletions

View File

@@ -10,11 +10,11 @@ except ImportError:
pass pass
from time import sleep from time import sleep
from collections import namedtuple
from itertools import repeat, cycle, chain from itertools import repeat, cycle, chain
from threading import Lock from threading import Lock
from .exc import ( from .exc import (
DeviceClosed,
GPIOPinMissing, GPIOPinMissing,
EnergenieSocketMissing, EnergenieSocketMissing,
EnergenieBadSocket, EnergenieBadSocket,

View File

@@ -14,7 +14,7 @@ from itertools import chain
from types import FunctionType from types import FunctionType
from threading import RLock from threading import RLock
from .threads import GPIOThread, _threads_shutdown from .threads import _threads_shutdown
from .mixins import ( from .mixins import (
ValuesMixin, ValuesMixin,
SharedMixin, SharedMixin,
@@ -26,7 +26,6 @@ from .exc import (
GPIOPinMissing, GPIOPinMissing,
GPIOPinInUse, GPIOPinInUse,
GPIODeviceClosed, GPIODeviceClosed,
GPIOBadSourceDelay,
) )
# Get a pin implementation to use as the default; we prefer RPi.GPIO's here # Get a pin implementation to use as the default; we prefer RPi.GPIO's here

View File

@@ -7,12 +7,11 @@ from __future__ import (
division, division,
) )
import warnings
from time import sleep, time from time import sleep, time
from threading import Event from threading import Event
from .exc import InputDeviceError, GPIODeviceError, DeviceClosed from .exc import InputDeviceError, DeviceClosed
from .devices import GPIODevice, CompositeDevice from .devices import GPIODevice
from .mixins import GPIOQueue, EventsMixin from .mixins import GPIOQueue, EventsMixin
@@ -560,7 +559,7 @@ class DistanceSensor(SmoothedInputDevice):
def __init__( def __init__(
self, echo=None, trigger=None, queue_len=30, max_distance=1, self, echo=None, trigger=None, queue_len=30, max_distance=1,
threshold_distance=0.3, partial=False): threshold_distance=0.3, partial=False):
if not (max_distance > 0): if max_distance <= 0:
raise ValueError('invalid maximum distance (must be positive)') raise ValueError('invalid maximum distance (must be positive)')
self._trigger = None self._trigger = None
super(DistanceSensor, self).__init__( super(DistanceSensor, self).__init__(

View File

@@ -13,13 +13,18 @@ from functools import wraps
from threading import Event from threading import Event
from collections import deque from collections import deque
try: try:
from statistics import median, mean from statistics import median
except ImportError: except ImportError:
from .compat import median, mean from .compat import median
from .threads import GPIOThread from .threads import GPIOThread
from .exc import BadEventHandler, DeviceClosed from .exc import (
BadEventHandler,
DeviceClosed,
GPIOBadSourceDelay,
GPIOBadQueueLen,
GPIOBadSampleWait,
)
class ValuesMixin(object): class ValuesMixin(object):
""" """

View File

@@ -5,8 +5,6 @@ from __future__ import (
division, division,
) )
import warnings
from time import sleep
from threading import Lock from threading import Lock
from itertools import repeat, cycle, chain from itertools import repeat, cycle, chain
@@ -194,7 +192,7 @@ class DigitalOutputDevice(OutputDevice):
def _blink_device(self, on_time, off_time, n): def _blink_device(self, on_time, off_time, n):
iterable = repeat(0) if n is None else repeat(0, n) iterable = repeat(0) if n is None else repeat(0, n)
for i in iterable: for _ in iterable:
self._write(True) self._write(True)
if self._blink_thread.stopping.wait(on_time): if self._blink_thread.stopping.wait(on_time):
break break

View File

@@ -9,7 +9,7 @@ str = type('')
import io import io
from collections import namedtuple from collections import namedtuple
from ..exc import PinUnknownPi, PinMultiplePins from ..exc import PinUnknownPi, PinMultiplePins, PinNoPins
# Some useful constants for describing pins # Some useful constants for describing pins

View File

@@ -15,7 +15,7 @@ try:
except ImportError: except ImportError:
from ..compat import isclose from ..compat import isclose
from . import Pin, PINS_CLEANUP from . import Pin
from ..exc import PinSetInput, PinPWMUnsupported, PinFixedPull from ..exc import PinSetInput, PinPWMUnsupported, PinFixedPull
@@ -38,7 +38,7 @@ class MockPin(Pin):
try: try:
old_pin = cls._PINS[number] old_pin = cls._PINS[number]
except KeyError: except KeyError:
self = super(Pin, cls).__new__(cls) self = super(MockPin, cls).__new__(cls)
cls._PINS[number] = self cls._PINS[number] = self
self._number = number self._number = number
self._function = 'input' self._function = 'input'

View File

@@ -16,6 +16,7 @@ from ..exc import (
PinFixedPull, PinFixedPull,
PinInvalidPull, PinInvalidPull,
PinInvalidBounce, PinInvalidBounce,
PinInvalidState,
) )
@@ -135,7 +136,7 @@ class PiGPIOPin(Pin):
if self._host == 'localhost': if self._host == 'localhost':
return "GPIO%d" % self._number return "GPIO%d" % self._number
else: else:
return "GPIO%d on %s:%d" % (self._host, self._port) return "GPIO%d on %s:%d" % (self._number, self._host, self._port)
@property @property
def host(self): def host(self):
@@ -182,7 +183,7 @@ class PiGPIOPin(Pin):
try: try:
self._connection.set_PWM_dutycycle(self._number, int(value * 255)) self._connection.set_PWM_dutycycle(self._number, int(value * 255))
except pigpio.error: except pigpio.error:
raise PinInvalidValue('invalid state "%s" for pin %r' % (value, self)) raise PinInvalidState('invalid state "%s" for pin %r' % (value, self))
elif self.function == 'input': elif self.function == 'input':
raise PinSetInput('cannot set state of pin %r' % self) raise PinSetInput('cannot set state of pin %r' % self)
else: else:

View File

@@ -7,8 +7,6 @@ from __future__ import (
str = type('') str = type('')
from threading import Lock
import RPIO import RPIO
import RPIO.PWM import RPIO.PWM
from RPIO.Exceptions import InvalidChannelException from RPIO.Exceptions import InvalidChannelException
@@ -21,6 +19,8 @@ from ..exc import (
PinFixedPull, PinFixedPull,
PinInvalidPull, PinInvalidPull,
PinInvalidBounce, PinInvalidBounce,
PinInvalidState,
PinPWMError,
) )
@@ -126,7 +126,7 @@ class RPIOPin(Pin):
def _set_state(self, value): def _set_state(self, value):
if not 0 <= value <= 1: if not 0 <= value <= 1:
raise PinInvalidValue('invalid state "%s" for pin %r' % (value, self)) raise PinInvalidState('invalid state "%s" for pin %r' % (value, self))
if self._pwm: if self._pwm:
RPIO.PWM.clear_channel_gpio(0, self._number) RPIO.PWM.clear_channel_gpio(0, self._number)
if value == 0: if value == 0:

View File

@@ -15,8 +15,8 @@ try:
from itertools import izip as zip from itertools import izip as zip
except ImportError: except ImportError:
pass pass
from itertools import count, cycle from itertools import cycle
from math import sin, cos, floor, radians from math import sin, cos, radians
try: try:
from statistics import mean from statistics import mean
except ImportError: except ImportError:

View File

@@ -184,7 +184,7 @@ class SPISoftwareBus(SharedMixin, Device):
for write_word in data: for write_word in data:
mask = 1 if self.lsb_first else 1 << (self.bits_per_word - 1) mask = 1 if self.lsb_first else 1 << (self.bits_per_word - 1)
read_word = 0 read_word = 0
for bit in range(self.bits_per_word): for _ in range(self.bits_per_word):
if self.mosi is not None: if self.mosi is not None:
self.mosi.value = bool(write_word & mask) self.mosi.value = bool(write_word & mask)
self.clock.on() self.clock.on()

View File

@@ -7,9 +7,9 @@ from __future__ import (
str = type('') str = type('')
from .exc import DeviceClosed from .exc import DeviceClosed, InputDeviceError
from .devices import Device from .devices import Device
from .spi import extract_spi_args, SPI from .spi import SPI
class SPIDevice(Device): class SPIDevice(Device):
@@ -206,7 +206,7 @@ class MCP33xx(MCP3xxx):
data = data[-2:] data = data[-2:]
result = ((data[0] & 63) << 7) | (data[1] >> 1) result = ((data[0] & 63) << 7) | (data[1] >> 1)
# Account for the sign bit # Account for the sign bit
if self.differential and value > 4095: if self.differential and result > 4095:
result = -(8192 - result) result = -(8192 - result)
assert -4096 <= result < 4096 assert -4096 <= result < 4096
return result return result

View File

@@ -8,11 +8,6 @@ str = type('')
from threading import Thread, Event from threading import Thread, Event
from .exc import (
GPIOBadQueueLen,
GPIOBadSampleWait,
)
_THREADS = set() _THREADS = set()
def _threads_shutdown(): def _threads_shutdown():
@@ -22,7 +17,9 @@ def _threads_shutdown():
class GPIOThread(Thread): class GPIOThread(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
if kwargs is None:
kwargs = {}
self.stopping = Event() self.stopping = Event()
super(GPIOThread, self).__init__(group, target, name, args, kwargs) super(GPIOThread, self).__init__(group, target, name, args, kwargs)
self.daemon = True self.daemon = True