Fixed #7676 -- Modified serializer registration to be thread safe. Thanks to mrts for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8370 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2008-08-15 11:53:49 +00:00
parent 188f131813
commit 0c115f12d7
1 changed files with 24 additions and 7 deletions

View File

@ -34,13 +34,27 @@ except ImportError:
_serializers = {} _serializers = {}
def register_serializer(format, serializer_module): def register_serializer(format, serializer_module, serializers=None):
"""Register a new serializer by passing in a module name.""" """"Register a new serializer.
``serializer_module`` should be the fully qualified module name
for the serializer.
If ``serializers`` is provided, the registration will be added
to the provided dictionary.
If ``serializers`` is not provided, the registration will be made
directly into the global register of serializers. Adding serializers
directly is not a thread-safe operation.
"""
module = __import__(serializer_module, {}, {}, ['']) module = __import__(serializer_module, {}, {}, [''])
if serializers is None:
_serializers[format] = module _serializers[format] = module
else:
serializers[format] = module
def unregister_serializer(format): def unregister_serializer(format):
"""Unregister a given serializer""" "Unregister a given serializer. This is not a thread-safe operation."
del _serializers[format] del _serializers[format]
def get_serializer(format): def get_serializer(format):
@ -88,8 +102,11 @@ def _load_serializers():
that user code has a chance to (e.g.) set up custom settings without that user code has a chance to (e.g.) set up custom settings without
needing to be careful of import order. needing to be careful of import order.
""" """
global _serializers
serializers = {}
for format in BUILTIN_SERIALIZERS: for format in BUILTIN_SERIALIZERS:
register_serializer(format, BUILTIN_SERIALIZERS[format]) register_serializer(format, BUILTIN_SERIALIZERS[format], serializers)
if hasattr(settings, "SERIALIZATION_MODULES"): if hasattr(settings, "SERIALIZATION_MODULES"):
for format in settings.SERIALIZATION_MODULES: for format in settings.SERIALIZATION_MODULES:
register_serializer(format, settings.SERIALIZATION_MODULES[format]) register_serializer(format, settings.SERIALIZATION_MODULES[format], serializers)
_serializers = serializers