Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14823 -- Corrected bootstrapping problems with register_serial…

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15336 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4a3ea263ef964a7fb7840544870834889f56f7e4 1 parent 456b8a9
Russell Keith-Magee authored January 27, 2011
4  django/core/serializers/__init__.py
@@ -48,6 +48,8 @@ def register_serializer(format, serializer_module, serializers=None):
48 48
     directly into the global register of serializers. Adding serializers
49 49
     directly is not a thread-safe operation.
50 50
     """
  51
+    if serializers is None and not _serializers:
  52
+        _load_serializers()
51 53
     module = importlib.import_module(serializer_module)
52 54
     if serializers is None:
53 55
         _serializers[format] = module
@@ -56,6 +58,8 @@ def register_serializer(format, serializer_module, serializers=None):
56 58
 
57 59
 def unregister_serializer(format):
58 60
     "Unregister a given serializer. This is not a thread-safe operation."
  61
+    if not _serializers:
  62
+        _load_serializers()
59 63
     del _serializers[format]
60 64
 
61 65
 def get_serializer(format):
53  tests/modeltests/serializers/tests.py
@@ -3,14 +3,65 @@
3 3
 from StringIO import StringIO
4 4
 from xml.dom import minidom
5 5
 
  6
+from django.conf import settings
6 7
 from django.core import serializers
7 8
 from django.db import transaction
8 9
 from django.test import TestCase, TransactionTestCase, Approximate
9  
-from django.utils import simplejson
  10
+from django.utils import simplejson, unittest
10 11
 
11 12
 from models import Category, Author, Article, AuthorProfile, Actor, \
12 13
                                     Movie, Score, Player, Team
13 14
 
  15
+class SerializerRegistrationTests(unittest.TestCase):
  16
+    def setUp(self):
  17
+        self.old_SERIALIZATION_MODULES = getattr(settings, 'SERIALIZATION_MODULES', None)
  18
+        self.old_serializers = serializers._serializers
  19
+
  20
+        serializers._serializers = {}
  21
+        settings.SERIALIZATION_MODULES = {
  22
+            "json2" : "django.core.serializers.json",
  23
+        }
  24
+
  25
+    def tearDown(self):
  26
+        serializers._serializers = self.old_serializers
  27
+        if self.old_SERIALIZATION_MODULES:
  28
+            settings.SERIALIZATION_MODULES = self.old_SERIALIZATION_MODULES
  29
+        else:
  30
+            delattr(settings, 'SERIALIZATION_MODULES')
  31
+
  32
+    def test_register(self):
  33
+        "Registering a new serializer populates the full registry. Refs #14823"
  34
+        serializers.register_serializer('json3', 'django.core.serializers.json')
  35
+
  36
+        public_formats = serializers.get_public_serializer_formats()
  37
+        self.assertIn('json3', public_formats)
  38
+        self.assertIn('json2', public_formats)
  39
+        self.assertIn('xml', public_formats)
  40
+
  41
+    def test_unregister(self):
  42
+        "Unregistering a serializer doesn't cause the registry to be repopulated. Refs #14823"
  43
+        serializers.unregister_serializer('xml')
  44
+        serializers.register_serializer('json3', 'django.core.serializers.json')
  45
+
  46
+        public_formats = serializers.get_public_serializer_formats()
  47
+
  48
+        self.assertNotIn('xml', public_formats)
  49
+        self.assertIn('json3', public_formats)
  50
+
  51
+    def test_builtin_serializers(self):
  52
+        "Requesting a list of serializer formats popuates the registry"
  53
+        all_formats = set(serializers.get_serializer_formats())
  54
+        public_formats = set(serializers.get_public_serializer_formats())
  55
+
  56
+        self.assertIn('xml', all_formats),
  57
+        self.assertIn('xml', public_formats)
  58
+
  59
+        self.assertIn('json2', all_formats)
  60
+        self.assertIn('json2', public_formats)
  61
+
  62
+        self.assertIn('python', all_formats)
  63
+        self.assertNotIn('python', public_formats)
  64
+
14 65
 class SerializersTestBase(object):
15 66
     @staticmethod
16 67
     def _comparison_value(value):

0 notes on commit 4a3ea26

Please sign in to comment.
Something went wrong with that request. Please try again.