Rework when_changed attribute to use weakrefs

Some fairly major changes to ensure that the Pin.when_changed property
doesn't keep references to the objects owning the callbacks that are
assigned. This is vaguely tricky given that ordinary weakref's can't be
used with bound methods (which are ephemeral), so I've back-ported
weakref.WeakMethod from Py3.4.

This solves a whole pile of things like Button instances not
disappearing when they're deleted, and makes composite devices
containing Buttons much easier to construct as we don't need to worry
about partially constructed things not getting deleted.
This commit is contained in:
Dave Jones
2016-10-22 13:55:31 +01:00
parent 08076e8d0e
commit cab6cc8086
7 changed files with 193 additions and 80 deletions

View File

@@ -6,9 +6,15 @@ from __future__ import (
)
str = type('')
import weakref
import pigpio
import os
from weakref import proxy
from threading import RLock
try:
from weakref import WeakMethod
except ImportError:
from .compat import WeakMethod
import pigpio
from . import SPI
from .pi import PiPin, PiFactory
@@ -164,6 +170,7 @@ class PiGPIOPin(PiPin):
self._pull = 'up' if factory.pi_info.pulled_up(self.address[-1]) else 'floating'
self._pwm = False
self._bounce = None
self._when_changed_lock = RLock()
self._when_changed = None
self._callback = None
self._edges = pigpio.EITHER_EDGE
@@ -269,26 +276,24 @@ class PiGPIOPin(PiPin):
finally:
self.when_changed = f
def _get_when_changed(self):
if self._callback is None:
return None
return self._callback.callb.func
def _call_when_changed(self, gpio, level, tick):
super(PiGPIOPin, self)._call_when_changed()
def _set_when_changed(self, value):
def _enable_event_detect(self):
self._callback = self.factory.connection.callback(
self.number, self._edges, self._call_when_changed)
def _disable_event_detect(self):
if self._callback is not None:
self._callback.cancel()
self._callback = None
if value is not None:
self._callback = self.factory.connection.callback(
self.number, self._edges,
lambda gpio, level, tick: value())
class PiGPIOHardwareSPI(SPI, Device):
def __init__(self, factory, port, device):
self._port = port
self._device = device
self._factory = weakref.proxy(factory)
self._factory = proxy(factory)
super(PiGPIOHardwareSPI, self).__init__()
self._reserve_pins(*(
factory.address + ('GPIO%d' % pin,)
@@ -382,7 +387,7 @@ class PiGPIOSoftwareSPI(SPI, Device):
self._clock_pin = clock_pin
self._mosi_pin = mosi_pin
self._miso_pin = miso_pin
self._factory = weakref.proxy(factory)
self._factory = proxy(factory)
super(PiGPIOSoftwareSPI, self).__init__()
self._reserve_pins(
factory.pin_address(clock_pin),