Merge pull request #2351 from gunjambi/thread-safe-mode-cache-init

Make mode descriptor cache initialization thread-safe.
This commit is contained in:
wiredfool 2017-01-16 11:20:45 +00:00 committed by GitHub
commit 579c716864

View File

@ -14,7 +14,7 @@
# #
# mode descriptor cache # mode descriptor cache
_modes = {} _modes = None
class ModeDescriptor(object): class ModeDescriptor(object):
@ -32,19 +32,23 @@ class ModeDescriptor(object):
def getmode(mode): def getmode(mode):
"""Gets a mode descriptor for the given mode.""" """Gets a mode descriptor for the given mode."""
global _modes
if not _modes: if not _modes:
# initialize mode cache # initialize mode cache
from PIL import Image from PIL import Image
modes = {}
# core modes # core modes
for m, (basemode, basetype, bands) in Image._MODEINFO.items(): for m, (basemode, basetype, bands) in Image._MODEINFO.items():
_modes[m] = ModeDescriptor(m, bands, basemode, basetype) modes[m] = ModeDescriptor(m, bands, basemode, basetype)
# extra experimental modes # extra experimental modes
_modes["RGBa"] = ModeDescriptor("RGBa", ("R", "G", "B", "a"), "RGB", "L") modes["RGBa"] = ModeDescriptor("RGBa", ("R", "G", "B", "a"), "RGB", "L")
_modes["LA"] = ModeDescriptor("LA", ("L", "A"), "L", "L") modes["LA"] = ModeDescriptor("LA", ("L", "A"), "L", "L")
_modes["La"] = ModeDescriptor("La", ("L", "a"), "L", "L") modes["La"] = ModeDescriptor("La", ("L", "a"), "L", "L")
_modes["PA"] = ModeDescriptor("PA", ("P", "A"), "RGB", "L") modes["PA"] = ModeDescriptor("PA", ("P", "A"), "RGB", "L")
# mapping modes # mapping modes
_modes["I;16"] = ModeDescriptor("I;16", "I", "L", "L") modes["I;16"] = ModeDescriptor("I;16", "I", "L", "L")
_modes["I;16L"] = ModeDescriptor("I;16L", "I", "L", "L") modes["I;16L"] = ModeDescriptor("I;16L", "I", "L", "L")
_modes["I;16B"] = ModeDescriptor("I;16B", "I", "L", "L") modes["I;16B"] = ModeDescriptor("I;16B", "I", "L", "L")
# set global mode cache atomically
_modes = modes
return _modes[mode] return _modes[mode]