8000 MNT: py312 deprecates pickling objects in itertools · matplotlib/matplotlib@c01373c · GitHub
[go: up one dir, main page]

Skip to content

Commit c01373c

Browse files
committed
MNT: py312 deprecates pickling objects in itertools
1 parent 088e50b commit c01373c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/matplotlib/cbook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ def __getstate__(self):
192192
for s, d in self.callbacks.items()},
193193
# It is simpler to reconstruct this from callbacks in __setstate__.
194194
"_func_cid_map": None,
195+
"_cid_gen": next(self._cid_gen)
195196
}
196197

197198
def __setstate__(self, state):
199+
cid_count = state.pop('_cid_gen')
198200
vars(self).update(state)
199201
self.callbacks = {
200202
s: {cid: _weak_or_strong_ref(func, self._remove_proxy)
@@ -203,6 +205,7 @@ def __setstate__(self, state):
203205
self._func_cid_map = {
204206
s: {proxy: cid for cid, proxy in d.items()}
205207
for s, d in self.callbacks.items()}
208+
self._cid_gen = itertools.count(cid_count)
206209

207210
def connect(self, signal, func):
208211
"""Register *func* to be called when signal *signal* is generated."""

lib/matplotlib/tests/test_cbook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def is_not_empty(self):
209209
assert self.callbacks._func_cid_map != {}
210210
assert self.callbacks.callbacks != {}
211211

212+
def test_cid_restore(self):
213+
cb = cbook.CallbackRegistry()
214+
cb.connect('a', lambda: None)
215+
cb2 = pickle.loads(pickle.dumps(cb))
216+
cid = cb2.connect('c', lambda: None)
217+
assert cid == 1
218+
212219
@pytest.mark.parametrize('pickle', [True, False])
213220
def test_callback_complete(self, pickle):
214221
# ensure we start with an empty registry

0 commit comments

Comments
 (0)
0