Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.2.X] Fixed #14823 -- Corrected bootstrapping problems with registe…

…r_serializers. Thanks to miker985@uw.edu for the report and draft patch.

Backport of r15336 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15339 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 407208b0c2b237f8ba7b1bd9d209b1053e6c77c4 1 parent 81f844a
@freakboy3742 freakboy3742 authored
View
4 django/core/serializers/__init__.py
@@ -48,6 +48,8 @@ def register_serializer(format, serializer_module, serializers=None):
directly into the global register of serializers. Adding serializers
directly is not a thread-safe operation.
"""
+ if serializers is None and not _serializers:
+ _load_serializers()
module = importlib.import_module(serializer_module)
if serializers is None:
_serializers[format] = module
@@ -56,6 +58,8 @@ def register_serializer(format, serializer_module, serializers=None):
def unregister_serializer(format):
"Unregister a given serializer. This is not a thread-safe operation."
+ if not _serializers:
+ _load_serializers()
del _serializers[format]
def get_serializer(format):
View
52 tests/modeltests/serializers/tests.py
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from StringIO import StringIO
+import unittest
from xml.dom import minidom
+from django.conf import settings
from django.core import serializers
from django.db import transaction
from django.test import TestCase, TransactionTestCase, Approximate
@@ -11,6 +13,56 @@
from models import Category, Author, Article, AuthorProfile, Actor, \
Movie, Score, Player, Team
+class SerializerRegistrationTests(unittest.TestCase):
+ def setUp(self):
+ self.old_SERIALIZATION_MODULES = getattr(settings, 'SERIALIZATION_MODULES', None)
+ self.old_serializers = serializers._serializers
+
+ serializers._serializers = {}
+ settings.SERIALIZATION_MODULES = {
+ "json2" : "django.core.serializers.json",
+ }
+
+ def tearDown(self):
+ serializers._serializers = self.old_serializers
+ if self.old_SERIALIZATION_MODULES:
+ settings.SERIALIZATION_MODULES = self.old_SERIALIZATION_MODULES
+ else:
+ delattr(settings, 'SERIALIZATION_MODULES')
+
+ def test_register(self):
+ "Registering a new serializer populates the full registry. Refs #14823"
+ serializers.register_serializer('json3', 'django.core.serializers.json')
+
+ public_formats = serializers.get_public_serializer_formats()
+ self.assertTrue('json3' in public_formats)
+ self.assertTrue('json2' in public_formats)
+ self.assertTrue('xml' in public_formats)
+
+ def test_unregister(self):
+ "Unregistering a serializer doesn't cause the registry to be repopulated. Refs #14823"
+ serializers.unregister_serializer('xml')
+ serializers.register_serializer('json3', 'django.core.serializers.json')
+
+ public_formats = serializers.get_public_serializer_formats()
+
+ self.assertFalse('xml' in public_formats)
+ self.assertTrue('json3' in public_formats)
+
+ def test_builtin_serializers(self):
+ "Requesting a list of serializer formats popuates the registry"
+ all_formats = set(serializers.get_serializer_formats())
+ public_formats = set(serializers.get_public_serializer_formats())
+
+ self.assertTrue('xml' in all_formats),
+ self.assertTrue('xml' in public_formats)
+
+ self.assertTrue('json2' in all_formats)
+ self.assertTrue('json2' in public_formats)
+
+ self.assertTrue('python' in all_formats)
+ self.assertFalse('python' in public_formats)
+
class SerializersTestBase(object):
@staticmethod
def _comparison_value(value):
Please sign in to comment.
Something went wrong with that request. Please try again.