From 8d224089a85581324420248a5afa7f24c7ae98f6 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 22 Aug 2017 00:13:18 -0700 Subject: [PATCH] Replace use of renderer._uid by weakref. This avoids the need for a note regarding issues with renderer classes that do not inherit from RendererBase. Remember that the goal of _uid was to prevent two renderers from sharing the same id() even though they were actually different (if one has been GC'd and another is created at the same address). Maintaining a weakref to the renderer works as well, as weakref equality is thusly defined: If the referents are still alive, two references have the same equality relationship as their referents (regardless of the callback). If either referent has been deleted, the references are equal only if the reference objects are the same object. --- .../api_changes/2017-06-03-ES_unique_renderer.rst | 11 ----------- lib/matplotlib/backend_bases.py | 12 ------------ lib/matplotlib/backends/backend_mixed.py | 4 ---- lib/matplotlib/text.py | 4 ++-- 4 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 doc/api/api_changes/2017-06-03-ES_unique_renderer.rst diff --git a/doc/api/api_changes/2017-06-03-ES_unique_renderer.rst b/doc/api/api_changes/2017-06-03-ES_unique_renderer.rst deleted file mode 100644 index 3dd58e0a16f3..000000000000 --- a/doc/api/api_changes/2017-06-03-ES_unique_renderer.rst +++ /dev/null @@ -1,11 +0,0 @@ -Unique identifier added to `RendererBase` classes -````````````````````````````````````````````````` - -Since ``id()`` is not guaranteed to be unique between objects that exist at -different times, a new private property ``_uid`` has been added to -`RendererBase` which is used along with the renderer's ``id()`` to cache -certain expensive operations. - -If a custom renderer does not subclass `RendererBase` or `MixedModeRenderer`, -it is not required to implement this ``_uid`` property, but this may produce -incorrect behavior when the renderers' ``id()`` clashes. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 98aca9251d4c..de8325e382fc 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -42,7 +42,6 @@ from functools import partial import importlib import io -import itertools import os import sys import time @@ -101,12 +100,6 @@ } -# Used to ensure that caching based on renderer id() is unique without being as -# expensive as a real UUID. 0 is used for renderers that don't derive from -# here, so start at 1. -_unique_renderer_id = itertools.count(1) - - def register_backend(format, backend, description=None): """ Register a backend for saving to a given file format. @@ -277,12 +270,7 @@ class RendererBase(object): """ def __init__(self): - # A lightweight id for unique-ification purposes. Along with id(self), - # the combination should be unique enough to use as part of a cache key. - self._uid = next(_unique_renderer_id) - self._texmanager = None - self._text2path = textpath.TextToPath() def open_group(self, s, gid=None): diff --git a/lib/matplotlib/backends/backend_mixed.py b/lib/matplotlib/backends/backend_mixed.py index a93ef062f279..40d7fd64398c 100644 --- a/lib/matplotlib/backends/backend_mixed.py +++ b/lib/matplotlib/backends/backend_mixed.py @@ -5,7 +5,6 @@ import six -import matplotlib.backend_bases from matplotlib.backends.backend_agg import RendererAgg from matplotlib.tight_bbox import process_figure_for_rasterizing @@ -50,9 +49,6 @@ def __init__(self, figure, width, height, dpi, vector_renderer, if raster_renderer_class is None: raster_renderer_class = RendererAgg - # See matplotlib.backend_bases.RendererBase._uid. - self._uid = next(matplotlib.backend_bases._unique_renderer_id) - self._raster_renderer_class = raster_renderer_class self._width = width self._height = height diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index caa5dcdaf73f..b65e9f5bd228 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -9,6 +9,7 @@ import math import warnings +import weakref import contextlib @@ -180,7 +181,6 @@ class Text(Artist): Handle storing and drawing of text in window or data coordinates. """ zorder = 3 - _cached = maxdict(50) def __repr__(self): @@ -912,7 +912,7 @@ def get_prop_tup(self, renderer=None): self._verticalalignment, self._horizontalalignment, hash(self._fontproperties), self._rotation, self._rotation_mode, - self.figure.dpi, id(renderer), getattr(renderer, '_uid', 0), + self.figure.dpi, weakref.ref(renderer), self._linespacing )