From 116ba72f3782e43803d097e82906d40049012048 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Mon, 3 Dec 2012 12:38:05 +0200 Subject: [PATCH] fixed possible dangling weakrefs on start-up --- app/listener.py | 4 ++-- app/solaar_cli.py | 2 +- app/ui/status_icon.py | 2 +- lib/logitech/scanner.py | 2 +- lib/logitech/unifying_receiver/receiver.py | 7 +++++-- lib/logitech/unifying_receiver/status.py | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/listener.py b/app/listener.py index 66f20649..9ed252e7 100644 --- a/app/listener.py +++ b/app/listener.py @@ -74,7 +74,7 @@ class ReceiverListener(_listener.EventsListener): self.receiver.enable_notifications(False) self.receiver.close() self.receiver = None - self._status_changed(None, alert=_status.ALERT.LOW) + self._status_changed(None, _status.ALERT.LOW) def tick(self, timestamp): if _log.isEnabledFor(_DEBUG): @@ -137,7 +137,7 @@ class ReceiverListener(_listener.EventsListener): _log.warn("received event %s for invalid device %d", event, event.devnumber) def __str__(self): - return '' % (self.receiver.path, self.receiver.status) + return '' % (self.receiver.path, self.receiver.handle) @classmethod def open(self, status_changed_callback=None): diff --git a/app/solaar_cli.py b/app/solaar_cli.py index 70758d5c..f6ea5705 100644 --- a/app/solaar_cli.py +++ b/app/solaar_cli.py @@ -87,7 +87,7 @@ def _print_receiver(receiver, short=True): else: print (" All notifications disabled.") - print (" Reported %d paired device(s)." % len(receiver)) + print (" Reported %d paired device(s)." % receiver.count()) activity = receiver.request(0x83B3) if activity: activity = [(d, ord(activity[d - 1:d])) for d in range(1, receiver.max_devices)] diff --git a/app/ui/status_icon.py b/app/ui/status_icon.py index ef2eadb1..a1c7ae38 100644 --- a/app/ui/status_icon.py +++ b/app/ui/status_icon.py @@ -61,7 +61,7 @@ def _icon_with_battery(s): return _PIXMAPS[name] def update(icon, receiver, device=None): - # print ("icon update", receiver, receiver.status, len(receiver._devices), device) + # print ("icon update", receiver, receiver.status, len(receiver), device) battery_status = None if device: diff --git a/lib/logitech/scanner.py b/lib/logitech/scanner.py index 08c9dbaf..9c30710f 100644 --- a/lib/logitech/scanner.py +++ b/lib/logitech/scanner.py @@ -16,7 +16,7 @@ def print_receiver(receiver): else: print (" All notifications disabled.") - print (" Reported %d paired device(s)." % len(receiver)) + print (" Reported %d paired device(s)." % receiver.count()) activity = receiver.request(0x83B3) if activity: activity = [(d, ord(activity[d - 1:d])) for d in range(1, receiver.max_devices)] diff --git a/lib/logitech/unifying_receiver/receiver.py b/lib/logitech/unifying_receiver/receiver.py index 736cb4f0..ae42dfdb 100644 --- a/lib/logitech/unifying_receiver/receiver.py +++ b/lib/logitech/unifying_receiver/receiver.py @@ -236,6 +236,10 @@ class Receiver(object): return True _log.warn("failed to %s the receiver lock", 'close' if lock_closed else 'open') + def count(self): + count = self.request(0x8102) + return 0 if count is None else ord(count[1:2]) + def request(self, request_id, *params): if self.handle: return _base.request(self.handle, 0xFF, request_id, *params) @@ -278,8 +282,7 @@ class Receiver(object): raise IndexError(key) def __len__(self): - count = self.request(0x8102) - return 0 if count is None else ord(count[1:2]) + return reduce(lambda partial, item: partial if item is None else partial + 1, self._devices.values(), 0) def __contains__(self, dev): if type(dev) == int: diff --git a/lib/logitech/unifying_receiver/status.py b/lib/logitech/unifying_receiver/status.py index 6461d8d2..6944fb9f 100644 --- a/lib/logitech/unifying_receiver/status.py +++ b/lib/logitech/unifying_receiver/status.py @@ -49,7 +49,7 @@ class ReceiverStatus(dict): self[ERROR] = None def __str__(self): - count = len([1 for d in self._receiver if d is not None]) + count = len(self._receiver) return ('No devices found.' if count == 0 else '1 device found.' if count == 1 else '%d devices found.' % count)