Skip to content

Commit e1ccb6f

Browse files
committed
Move locking of fontlist.json *into* json_dump.
This makes it easier for end users to call json_dump (which is public API) safely, given that _lock_path is not public API.
1 parent eb18cb2 commit e1ccb6f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ property. This now raises a TypeError.
6262
`.FileMovieWriter` now defaults to writing temporary frames in a temporary
6363
directory, which is always cleared at exit. In order to keep the individual
6464
frames saved on the filesystem, pass an explicit *frame_prefix*.
65+
66+
`.font_manager.json_dump` now locks the font manager dump file
67+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68+
... to prevent multiple processes from writing to it at the same time.

lib/matplotlib/font_manager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,14 @@ def json_dump(data, filename):
914914
shipped with Matplotlib) are stored relative to that data path (to remain
915915
valid across virtualenvs).
916916
917+
This function temporarily locks the output file to prevent multiple
918+
processes from overwriting one another's output.
919+
917920
See Also
918921
--------
919922
json_load
920923
"""
921-
with open(filename, 'w') as fh:
924+
with cbook._lock_path(filename), open(filename, 'w') as fh:
922925
try:
923926
json.dump(data, fh, cls=_JSONEncoder, indent=2)
924927
except OSError as e:
@@ -1335,8 +1338,7 @@ def get_font(filename, hinting_factor=None):
13351338
def _rebuild():
13361339
global fontManager
13371340
fontManager = FontManager()
1338-
with cbook._lock_path(_fmcache):
1339-
json_dump(fontManager, _fmcache)
1341+
json_dump(fontManager, _fmcache)
13401342
_log.info("generated new fontManager")
13411343

13421344

0 commit comments

Comments
 (0)