From 4665ed66af47e9f9ea31a0e4f69c1202b56c2b2f Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 17 Jul 2014 22:31:36 -0400 Subject: [PATCH] BUG : fixes FontProperties memory leak According to the data model (https://docs.python.org/2/reference/datamodel.html#object.__hash__) objects that define __hash__ need to define __eq__ or __cmp__. By default, all user defined classes have a __cmp__ which evaluates to False for all other objects. Previously we do not define either __cmp__ or __eq__ on FontProperties, This results in never finding the property cached in the dict, hence the growth in the number of FontProperties (and I assume the stuff in them). By adding __eq__ and __ne__ we complete the data model and adding FontProperties to dictionaries should work as expected. This was not a problem before, but in #3077 the caching key was changed from hash(prop) -> prop. Closes #3264 --- lib/matplotlib/font_manager.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index b6882826a17e..3d33a28ce923 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -707,6 +707,12 @@ def __hash__(self): self.get_file()) return hash(l) + def __eq__(self, other): + return hash(self) == hash(other) + + def __ne__(self, other): + return hash(self) != hash(other) + def __str__(self): return self.get_fontconfig_pattern()