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

@@ -7,6 +7,14 @@ from __future__ import (
str = type('')
import warnings
from types import MethodType
from threading import RLock
from weakref import ref
try:
from weakref import WeakMethod
except ImportError:
from .compat import WeakMethod
from RPi import GPIO
from .local import LocalPiFactory, LocalPiPin
@@ -89,6 +97,7 @@ class RPiGPIOPin(LocalPiPin):
self._frequency = None
self._duty_cycle = None
self._bounce = -666
self._when_changed_lock = RLock()
self._when_changed = None
self._edges = GPIO.BOTH
GPIO.setup(self.number, GPIO.IN, self.GPIO_PULL_UPS[self._pull])
@@ -202,19 +211,15 @@ class RPiGPIOPin(LocalPiPin):
finally:
self.when_changed = f
def _get_when_changed(self):
return self._when_changed
def _call_when_changed(self, channel):
super(RPiGPIOPin, self)._call_when_changed()
def _set_when_changed(self, value):
if self._when_changed is None and value is not None:
self._when_changed = value
GPIO.add_event_detect(
self.number, self._edges,
callback=lambda channel: self._when_changed(),
bouncetime=self._bounce)
elif self._when_changed is not None and value is None:
GPIO.remove_event_detect(self.number)
self._when_changed = None
else:
self._when_changed = value
def _enable_event_detect(self):
GPIO.add_event_detect(
self.number, self._edges,
callback=self._call_when_changed,
bouncetime=self._bounce)
def _disable_event_detect(self):
GPIO.remove_event_detect(self.number)