Skip to content

Commit 2a5a365

Browse files
committed
MNT: use weakkeydict to manage "next number"
1 parent 2661edb commit 2a5a365

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

mpl_gui/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
1414
"""
1515
from collections import Counter
16-
from itertools import count
1716
import functools
1817
import logging
1918
import warnings
19+
from weakref import WeakKeyDictionary
2020

2121
from matplotlib.backend_bases import FigureCanvasBase as _FigureCanvasBase
2222

@@ -118,7 +118,7 @@ def __init__(self, *, block=None, timeout=0, prefix="Figure "):
118118
self._timeout = timeout
119119
self._block = block
120120
# Settings / state to control the default figure label
121-
self._count = count()
121+
self._fig_to_number = WeakKeyDictionary()
122122
self._prefix = prefix
123123
# the canonical location for storing the Figures this registry owns.
124124
# any additional views must never include a figure not in the list but
@@ -140,11 +140,10 @@ def _register_fig(self, fig):
140140
# empty string) on a Figure so if they provide duplicate labels, change
141141
# the labels under us, or provide a label that will be shadowed in the
142142
# future it will be what it is.
143-
fignum = next(self._count)
143+
fignum = max(self._fig_to_number.values(), default=0) + 1
144144
if fig.get_label() == "":
145145
fig.set_label(f"{self._prefix}{fignum:d}")
146-
# TODO: is there a better way to track this than monkey patching?
147-
fig._mpl_gui_fignum = fignum
146+
self._fig_to_number[fig] = fignum
148147
return fig
149148

150149
@property
@@ -194,7 +193,7 @@ def subplot_mosaic(self, *args, **kwargs):
194193
def _ensure_all_figures_promoted(self):
195194
for f in self.figures:
196195
if f.canvas.manager is None:
197-
promote_figure(f, num=f._mpl_gui_fignum)
196+
promote_figure(f, num=self._fig_to_number[f])
198197

199198
def show_all(self, *, block=None, timeout=None):
200199
"""

0 commit comments

Comments
 (0)