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
from time import sleep
from collections import namedtuple
from itertools import repeat, cycle, chain
from threading import Lock
from .exc import (
DeviceClosed,
GPIOPinMissing,
EnergenieSocketMissing,
EnergenieBadSocket,

View File

@@ -14,7 +14,7 @@ from itertools import chain
from types import FunctionType
from threading import RLock
from .threads import GPIOThread, _threads_shutdown
from .threads import _threads_shutdown
from .mixins import (
ValuesMixin,
SharedMixin,
@@ -26,7 +26,6 @@ from .exc import (
GPIOPinMissing,
GPIOPinInUse,
GPIODeviceClosed,
GPIOBadSourceDelay,
)
# 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,
)
import warnings
from time import sleep, time
from threading import Event
from .exc import InputDeviceError, GPIODeviceError, DeviceClosed
from .devices import GPIODevice, CompositeDevice
from .exc import InputDeviceError, DeviceClosed
from .devices import GPIODevice
from .mixins import GPIOQueue, EventsMixin
@@ -560,7 +559,7 @@ class DistanceSensor(SmoothedInputDevice):
def __init__(
self, echo=None, trigger=None, queue_len=30, max_distance=1,
threshold_distance=0.3, partial=False):
if not (max_distance > 0):
if max_distance <= 0:
raise ValueError('invalid maximum distance (must be positive)')
self._trigger = None
super(DistanceSensor, self).__init__(

View File

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

View File

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

View File

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

View File

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

View File

@@ -16,6 +16,7 @@ from ..exc import (
PinFixedPull,
PinInvalidPull,
PinInvalidBounce,
PinInvalidState,
)
@@ -135,7 +136,7 @@ class PiGPIOPin(Pin):
if self._host == 'localhost':
return "GPIO%d" % self._number
else:
return "GPIO%d on %s:%d" % (self._host, self._port)
return "GPIO%d on %s:%d" % (self._number, self._host, self._port)
@property
def host(self):
@@ -182,7 +183,7 @@ class PiGPIOPin(Pin):
try:
self._connection.set_PWM_dutycycle(self._number, int(value * 255))
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':
raise PinSetInput('cannot set state of pin %r' % self)
else:

View File

@@ -7,8 +7,6 @@ from __future__ import (
str = type('')
from threading import Lock
import RPIO
import RPIO.PWM
from RPIO.Exceptions import InvalidChannelException
@@ -21,6 +19,8 @@ from ..exc import (
PinFixedPull,
PinInvalidPull,
PinInvalidBounce,
PinInvalidState,
PinPWMError,
)
@@ -126,7 +126,7 @@ class RPIOPin(Pin):
def _set_state(self, value):
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:
RPIO.PWM.clear_channel_gpio(0, self._number)
if value == 0:

View File

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

View File

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

View File

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

View File

@@ -8,11 +8,6 @@ str = type('')
from threading import Thread, Event
from .exc import (
GPIOBadQueueLen,
GPIOBadSampleWait,
)
_THREADS = set()
def _threads_shutdown():
@@ -22,7 +17,9 @@ def _threads_shutdown():
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()
super(GPIOThread, self).__init__(group, target, name, args, kwargs)
self.daemon = True