Ensure SourceMixin descendents shut down the source prior to closing.
Furthermore, make sure devices are closed before pin factory shuts down,
and that pins have a strong reference to their owning factory (to
prevent losing the factory before the pins).
This commit is contained in:
Dave Jones
2017-06-19 22:22:39 +01:00
parent ab73e857fd
commit bcc94354ea
4 changed files with 25 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ from .exc import (
GPIOPinInUse,
GPIODeviceClosed,
PinFactoryFallback,
PinReservationsExist,
)
from .compat import frozendict
@@ -202,6 +203,20 @@ class Device(ValuesMixin, GPIOBase):
@classmethod
def _set_pin_factory(cls, new_factory):
reserved_devices = {
dev
for ref_list in cls._reservations.values()
for ref in ref_list
for dev in (ref(),)
if dev is not None
}
if new_factory is None:
for dev in reserved_devices:
dev.close()
elif reserved_devices:
raise PinReservationsExist(
"can't change factory while devices still hold pin "
"reservations (%r)" % dev)
if cls._pin_factory is not None:
cls._pin_factory.close()
cls._pin_factory = new_factory