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

Skip to content

Commit d404e35

Browse files
committed
Even better GC and tests!
1 parent 8864e5c commit d404e35

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

lib/matplotlib/cbook.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,17 @@ def connect(self, s, func):
520520
return cid
521521

522522
def _remove_proxy(self, proxy):
523-
for category, proxies in list(six.iteritems(self._func_cid_map)):
523+
for signal, proxies in list(six.iteritems(self._func_cid_map)):
524524
try:
525-
del self.callbacks[category][proxies[proxy]]
525+
del self.callbacks[signal][proxies[proxy]]
526526
except KeyError:
527527
pass
528528

529+
if len(self.callbacks[signal]) == 0:
530+
del self.callbacks[signal]
531+
del self._func_cid_map[signal]
532+
533+
529534
def disconnect(self, cid):
530535
"""
531536
disconnect the callback registered with callback id *cid*
@@ -536,7 +541,7 @@ def disconnect(self, cid):
536541
except KeyError:
537542
continue
538543
else:
539-
for category, functions in list(
544+
for signal, functions in list(
540545
six.iteritems(self._func_cid_map)):
541546
for function, value in list(six.iteritems(functions)):
542547
if value == cid:

lib/matplotlib/tests/test_cbook.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99
from numpy.testing.utils import (assert_array_equal, assert_approx_equal,
1010
assert_array_almost_equal)
11-
from nose.tools import assert_equal, raises, assert_true
11+
from nose.tools import assert_equal, assert_not_equal, raises, assert_true
1212

1313
import matplotlib.cbook as cbook
1414
import matplotlib.colors as mcolors
@@ -243,3 +243,47 @@ def test_label_error(self):
243243
def test_bad_dims(self):
244244
data = np.random.normal(size=(34, 34, 34))
245245
results = cbook.boxplot_stats(data)
246+
247+
248+
class Test_callback_registry(object):
249+
def setup(self):
250+
self.signal = 'test'
251+
self.callbacks = cbook.CallbackRegistry()
252+
253+
def connect(self, s, func):
254+
return self.callbacks.connect(s, func)
255+
256+
def is_empty(self):
257+
assert_equal(self.callbacks._func_cid_map, {})
258+
assert_equal(self.callbacks.callbacks, {})
259+
260+
def is_not_empty(self):
261+
assert_not_equal(self.callbacks._func_cid_map, {})
262+
assert_not_equal(self.callbacks.callbacks, {})
263+
264+
def test_callback_complete(self):
265+
# ensure we start with an empty registry
266+
self.is_empty()
267+
268+
# create a class for testing
269+
mini_me = Test_callback_registry()
270+
271+
# test that we can add a callback
272+
cid1 = self.connect(self.signal, mini_me.dummy)
273+
assert_equal(type(cid1), int)
274+
self.is_not_empty()
275+
276+
# test that we don't add a second callback
277+
cid2 = self.connect(self.signal, mini_me.dummy)
278+
assert_equal(cid1, cid2)
279+
self.is_not_empty()
280+
assert_equal(len(self.callbacks._func_cid_map), 1)
281+
assert_equal(len(self.callbacks.callbacks), 1)
282+
283+
del mini_me
284+
285+
# check we now have no callbacks registered
286+
self.is_empty()
287+
288+
def dummy(self):
289+
pass

0 commit comments

Comments
 (0)