Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15889 -- when trying to access to access a serializer that doe…

…sn't exist, raise a new SerializerDoesNotExist exception. Thanks to Mathieu Agopian for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16104 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 930371e91b03f91691fa5e2e4f428cb72469e191 1 parent 1b6670d
Alex Gaynor authored
1  AUTHORS
@@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better:
35 35
 
36 36
     Gisle Aas <gisle@aas.no>
37 37
     Chris Adams
  38
+    Mathieu Agopian <mathieu.agopian@gmail.com>
38 39
     ajs <adi@sieker.info>
39 40
     alang@bright-green.com
40 41
     A S Alam <aalam@users.sf.net>
7  django/core/serializers/__init__.py
@@ -18,6 +18,7 @@
18 18
 
19 19
 from django.conf import settings
20 20
 from django.utils import importlib
  21
+from django.core.serializers.base import SerializerDoesNotExist
21 22
 
22 23
 # Built-in serializers
23 24
 BUILTIN_SERIALIZERS = {
@@ -60,11 +61,15 @@ def unregister_serializer(format):
60 61
     "Unregister a given serializer. This is not a thread-safe operation."
61 62
     if not _serializers:
62 63
         _load_serializers()
  64
+    if format not in _serializers:
  65
+        raise SerializerDoesNotExist(format)
63 66
     del _serializers[format]
64 67
 
65 68
 def get_serializer(format):
66 69
     if not _serializers:
67 70
         _load_serializers()
  71
+    if format not in _serializers:
  72
+        raise SerializerDoesNotExist(format)
68 73
     return _serializers[format].Serializer
69 74
 
70 75
 def get_serializer_formats():
@@ -80,6 +85,8 @@ def get_public_serializer_formats():
80 85
 def get_deserializer(format):
81 86
     if not _serializers:
82 87
         _load_serializers()
  88
+    if format not in _serializers:
  89
+        raise SerializerDoesNotExist(format)
83 90
     return _serializers[format].Deserializer
84 91
 
85 92
 def serialize(format, queryset, **options):
4  django/core/serializers/base.py
@@ -8,6 +8,10 @@
8 8
 from django.utils.encoding import smart_str, smart_unicode
9 9
 from django.utils import datetime_safe
10 10
 
  11
+class SerializerDoesNotExist(KeyError):
  12
+    """The requested serializer was not found."""
  13
+    pass
  14
+
11 15
 class SerializationError(Exception):
12 16
     """Something bad happened during serialization."""
13 17
     pass
6  docs/topics/serialization.txt
@@ -38,6 +38,12 @@ This is useful if you want to serialize data directly to a file-like object
38 38
     out = open("file.xml", "w")
39 39
     xml_serializer.serialize(SomeModel.objects.all(), stream=out)
40 40
 
  41
+.. note::
  42
+
  43
+    Calling :func:`~django.core.serializers.get_serializer` with an unknown
  44
+    :ref:`format <serialization-formats>` will raise a
  45
+    :class:`~django.core.serializers.SerializerDoesNotExist` exception.
  46
+
41 47
 Subset of fields
42 48
 ~~~~~~~~~~~~~~~~
43 49
 
26  tests/regressiontests/serializers_regress/tests.py
@@ -6,7 +6,7 @@
6 6
 the serializers. This includes all valid data values, plus
7 7
 forward, backwards and self references.
8 8
 """
9  
-
  9
+from __future__ import with_statement
10 10
 
11 11
 import datetime
12 12
 import decimal
@@ -17,6 +17,7 @@
17 17
 
18 18
 from django.conf import settings
19 19
 from django.core import serializers, management
  20
+from django.core.serializers import SerializerDoesNotExist
20 21
 from django.db import transaction, DEFAULT_DB_ALIAS, connection
21 22
 from django.test import TestCase
22 23
 from django.utils.functional import curry
@@ -350,7 +351,28 @@ def inherited_compare(testcase, pk, klass, data):
350 351
 # Dynamically create serializer tests to ensure that all
351 352
 # registered serializers are automatically tested.
352 353
 class SerializerTests(TestCase):
353  
-    pass
  354
+    def test_get_unknown_serializer(self):
  355
+        """
  356
+        #15889: get_serializer('nonsense') raises a SerializerDoesNotExist
  357
+        """
  358
+        with self.assertRaises(SerializerDoesNotExist):
  359
+            serializers.get_serializer("nonsense")
  360
+
  361
+        with self.assertRaises(KeyError):
  362
+            serializers.get_serializer("nonsense")
  363
+
  364
+        # SerializerDoesNotExist is instantiated with the nonexistent format
  365
+        with self.assertRaises(SerializerDoesNotExist) as cm:
  366
+            serializers.get_serializer("nonsense")
  367
+        self.assertEqual(cm.exception.args, ("nonsense",))
  368
+
  369
+    def test_unregister_unkown_serializer(self):
  370
+        with self.assertRaises(SerializerDoesNotExist):
  371
+            serializers.unregister_serializer("nonsense")
  372
+
  373
+    def test_get_unkown_deserializer(self):
  374
+        with self.assertRaises(SerializerDoesNotExist):
  375
+            serializers.get_deserializer("nonsense")
354 376
 
355 377
 def serializerTest(format, self):
356 378
 

0 notes on commit 930371e

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