Skip to content

Commit e908a41

Browse files
committed
MNT: py312 deprecates pickling objects in itertools
1 parent ebc7ade commit e908a41

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy