Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 683e7bd

Browse files
committed
Record the connected signal in CallbackRegistry weakref cleanup function.
... to remove the need to loop over all signals in _remove_proxy.
1 parent 2f778fd commit 683e7bd

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

lib/matplotlib/cbook.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __getstate__(self):
197197
def __setstate__(self, state):
198198
vars(self).update(state)
199199
self.callbacks = {
200-
s: {cid: _weak_or_strong_ref(func, self._remove_proxy)
200+
s: {cid: _weak_or_strong_ref(func, functools.partial(self._remove_proxy, s))
201201
for cid, func in d.items()}
202202
for s, d in self.callbacks.items()}
203203
self._func_cid_map = {
@@ -209,7 +209,7 @@ def connect(self, signal, func):
209209
if self._signals is not None:
210210
_api.check_in_list(self._signals, signal=signal)
211211
self._func_cid_map.setdefault(signal, {})
212-
proxy = _weak_or_strong_ref(func, self._remove_proxy)
212+
proxy = _weak_or_strong_ref(func, functools.partial(self._remove_proxy, signal))
213213
if proxy in self._func_cid_map[signal]:
214214
return self._func_cid_map[signal][proxy]
215215
cid = next(self._cid_gen)
@@ -230,18 +230,15 @@ def _connect_picklable(self, signal, func):
230230

231231
# Keep a reference to sys.is_finalizing, as sys may have been cleared out
232232
# at that point.
233-
def _remove_proxy(self, proxy, *, _is_finalizing=sys.is_finalizing):
233+
def _remove_proxy(self, signal, proxy, *, _is_finalizing=sys.is_finalizing):
234234
if _is_finalizing():
235235
# Weakrefs can't be properly torn down at that point anymore.
236236
return
237-
for signal, proxy_to_cid in list(self._func_cid_map.items()):
238-
cid = proxy_to_cid.pop(proxy, None)
239-
if cid is not None:
240-
del self.callbacks[signal][cid]
241-
self._pickled_cids.discard(cid)
242-
break
243-
else:
244-
# Not found
237+
cid = self._func_cid_map[signal].pop(proxy, None)
238+
if cid is not None:
239+
del self.callbacks[signal][cid]
240+
self._pickled_cids.discard(cid)
241+
else: # Not found
245242
return
246243
# Clean up empty dicts
247244
if len(self.callbacks[signal]) == 0:
@@ -260,10 +257,8 @@ def disconnect(self, cid):
260257
proxy = cid_to_proxy.pop(cid, None)
261258
if proxy is not None:
262259
break
263-
else:
264-
# Not found
260+
else: # Not found
265261
return
266-
267262
proxy_to_cid = self._func_cid_map[signal]
268263
for current_proxy, current_cid in list(proxy_to_cid.items()):
269264
if current_cid == cid:

0 commit comments

Comments
 (0)