Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3061c05

Browse files
authored
Merge pull request #13113 from anntzer/singleton-texmanager-texttopath
Globally cache single TexManager instances.
2 parents 0dac001 + 5d82059 commit 3061c05

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/matplotlib/texmanager.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"""
3030

3131
import copy
32+
import functools
3233
import glob
3334
import hashlib
3435
import logging
@@ -48,6 +49,8 @@
4849
class TexManager(object):
4950
"""
5051
Convert strings to dvi files using TeX, caching the results to a directory.
52+
53+
Repeated calls to this constructor always return the same instance.
5154
"""
5255

5356
cachedir = mpl.get_cachedir()
@@ -93,8 +96,13 @@ class TexManager(object):
9396
('text.latex.preamble', 'text.latex.unicode', 'text.latex.preview',
9497
'font.family') + tuple('font.' + n for n in font_families))
9598

96-
def __init__(self):
99+
@functools.lru_cache() # Always return the same instance.
100+
def __new__(cls):
101+
self = object.__new__(cls)
102+
self._reinit()
103+
return self
97104

105+
def _reinit(self):
98106
if self.texcache is None:
99107
raise RuntimeError('Cannot create TexManager, as there is no '
100108
'cache directory available')
@@ -167,7 +175,7 @@ def get_font_config(self):
167175
# deepcopy may not be necessary, but feels more future-proof
168176
self._rc_cache[k] = copy.deepcopy(rcParams[k])
169177
_log.debug('RE-INIT\nold fontconfig: %s', self._fontconfig)
170-
self.__init__()
178+
self._reinit()
171179
_log.debug('fontconfig: %s', self._fontconfig)
172180
return self._fontconfig
173181

lib/matplotlib/textpath.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def _get_adobe_standard_encoding():
2424

2525

2626
class TextToPath(object):
27-
"""
28-
A class that convert a given text to a path using ttf fonts.
29-
"""
27+
"""A class that converts strings to paths."""
3028

3129
FONT_SCALE = 100.
3230
DPI = 72

0 commit comments

Comments
 (0)