From 0c115f12d742429ca32b4bcce787873eda4ffccc Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 15 Aug 2008 11:53:49 +0000 Subject: [PATCH] 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 --- django/core/serializers/__init__.py | 31 ++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py index 47dfb0c87a6..5365efeaccc 100644 --- a/django/core/serializers/__init__.py +++ b/django/core/serializers/__init__.py @@ -34,13 +34,27 @@ except ImportError: _serializers = {} -def register_serializer(format, serializer_module): - """Register a new serializer by passing in a module name.""" +def register_serializer(format, serializer_module, serializers=None): + """"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, {}, {}, ['']) - _serializers[format] = module - + if serializers is None: + _serializers[format] = module + else: + serializers[format] = module + def unregister_serializer(format): - """Unregister a given serializer""" + "Unregister a given serializer. This is not a thread-safe operation." del _serializers[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 needing to be careful of import order. """ + global _serializers + serializers = {} for format in BUILTIN_SERIALIZERS: - register_serializer(format, BUILTIN_SERIALIZERS[format]) + register_serializer(format, BUILTIN_SERIALIZERS[format], serializers) if hasattr(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