Skip to content

Commit 52f9e5a

Browse files
committed
Bug fix, speed-up, and safer-than-sorry.
1 parent 59f77aa commit 52f9e5a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/matplotlib/cbook.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def add_callback(self, callback):
384384

385385
def _destroy(self, wk):
386386
for callback in self._callbacks:
387-
callback(self)
387+
try:
388+
callback(self)
389+
except ReferenceError: pass
388390

389391
def __getstate__(self):
390392
d = self.__dict__.copy()
@@ -508,18 +510,19 @@ def connect(self, s, func):
508510
if proxy in self._func_cid_map[s]:
509511
return self._func_cid_map[s][proxy]
510512

511-
proxy.add_callback(self.remove_proxy) # Remove the proxy when it dies.
513+
proxy.add_callback(self._remove_proxy) # Remove the proxy when it dies.
512514
self._cid += 1
513515
cid = self._cid
514516
self._func_cid_map[s][proxy] = cid
515517
self.callbacks.setdefault(s, dict())
516518
self.callbacks[s][cid] = proxy
517519
return cid
518520

519-
def remove_proxy(self, proxy):
521+
def _remove_proxy(self, proxy):
520522
for category, proxies in list(six.iteritems(self._func_cid_map)):
521-
if proxy in proxies:
523+
try:
522524
del self.callbacks[category][proxies[proxy]]
525+
except KeyError: pass
523526

524527
def disconnect(self, cid):
525528
"""
@@ -545,7 +548,10 @@ def process(self, s, *args, **kwargs):
545548
"""
546549
if s in self.callbacks:
547550
for cid, proxy in list(six.iteritems(self.callbacks[s])):
548-
proxy(*args, **kwargs)
551+
try:
552+
proxy(*args, **kwargs)
553+
except ReferenceError:
554+
self._remove_proxy(proxy)
549555

550556

551557
class Scheduler(threading.Thread):

0 commit comments

Comments
 (0)