Matplotlib-pyodide: font caching with maxdict and lru_cache

I am trying to update Pyodide to use Matplotlib 3.8.2. The main issue is that in v3.6 maxdict was dropped and we are told to use lru_cache instead. matplotlib-pyodide uses maxdict and so breaks with the upgrade. matplotlib-pyodide is unmaintained but I’d prefer not to stay on old matplotlib versions forever so I’m trying to fix this incompatibility.

We are using the cache to cache fonts. The maxdict cache was owned by a Renderer instance. I’d like to write new code like:

def _get_font(prop):
     fname = findfont(prop)
     font = FT2Font(str(fname))
     font_file_name = fname.rpartition("/")[-1]
     return (font, font_file_name)

The renderer mutates the font so logically we’d do deepcopy(_get_font(prop)) but this doesn’t work because FT2Font is a C++ type and doesn’t implement pickle or any way of copying it.

What’s the right way to handle this? Maybe the correct thing would be to have a freelist instead of a cache? How much more expensive is it to call the FT2Font constructor than to clear and reuse an old one? Maybe we should just get rid of the caching entirely?