Code

Ticket #15889: 15889-get_serializer-specific-exception_3.diff

File 15889-get_serializer-specific-exception_3.diff, 3.5 KB (added by magopian, 3 years ago)

integrated Alex's new feedbacks

Line 
1diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
2index 7afc94c..b52165f 100644
3--- a/django/core/serializers/__init__.py
4+++ b/django/core/serializers/__init__.py
5@@ -18,6 +18,7 @@ To add your own serializers, use the SERIALIZATION_MODULES setting::
6 
7 from django.conf import settings
8 from django.utils import importlib
9+from django.core.serializers.base import SerializerDoesNotExist
10 
11 # Built-in serializers
12 BUILTIN_SERIALIZERS = {
13@@ -65,6 +66,8 @@ def unregister_serializer(format):
14 def get_serializer(format):
15     if not _serializers:
16         _load_serializers()
17+    if format not in _serializers:
18+        raise SerializerDoesNotExist(format)
19     return _serializers[format].Serializer
20 
21 def get_serializer_formats():
22diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
23index cdcc7fa..6b532af 100644
24--- a/django/core/serializers/base.py
25+++ b/django/core/serializers/base.py
26@@ -8,6 +8,10 @@ from django.db import models
27 from django.utils.encoding import smart_str, smart_unicode
28 from django.utils import datetime_safe
29 
30+class SerializerDoesNotExist(KeyError):
31+    """A requested serializer was not found."""
32+    pass
33+
34 class SerializationError(Exception):
35     """Something bad happened during serialization."""
36     pass
37diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
38index c8acc85..f0f17b2 100644
39--- a/docs/topics/serialization.txt
40+++ b/docs/topics/serialization.txt
41@@ -38,6 +38,12 @@ This is useful if you want to serialize data directly to a file-like object
42     out = open("file.xml", "w")
43     xml_serializer.serialize(SomeModel.objects.all(), stream=out)
44 
45+.. note::
46+
47+    Calling :func:`~django.core.serializers.get_serializer` with an unknown
48+    :ref:`format <serialization-formats>` will raise a
49+    :class:`~django.core.serializers.SerializerDoesNotExist` exception.
50+
51 Subset of fields
52 ~~~~~~~~~~~~~~~~
53 
54diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
55index de57a6f..6b302f0 100644
56--- a/tests/regressiontests/serializers_regress/tests.py
57+++ b/tests/regressiontests/serializers_regress/tests.py
58@@ -8,6 +8,7 @@ forward, backwards and self references.
59 """
60 
61 
62+from __future__ import with_statement
63 import datetime
64 import decimal
65 try:
66@@ -17,6 +18,7 @@ except ImportError:
67 
68 from django.conf import settings
69 from django.core import serializers, management
70+from django.core.serializers import SerializerDoesNotExist
71 from django.db import transaction, DEFAULT_DB_ALIAS, connection
72 from django.test import TestCase
73 from django.utils.functional import curry
74@@ -350,7 +352,20 @@ if connection.features.allows_primary_key_0:
75 # Dynamically create serializer tests to ensure that all
76 # registered serializers are automatically tested.
77 class SerializerTests(TestCase):
78-    pass
79+
80+    def test_get_unknown_serializer(self):
81+        """
82+        #15889: get_serializer('nonsense') raises a SerializerDoesNotExist
83+        """
84+        self.assertRaises(SerializerDoesNotExist,
85+                          serializers.get_serializer, 'nonsense')
86+        # subclasses KeyError for backwards compatibility
87+        self.assertRaises(KeyError, serializers.get_serializer, 'nonsense')
88+
89+        # SerializerDoesNotExist is instantiated with the inexistent format
90+        with self.assertRaises(SerializerDoesNotExist) as cm:
91+            serializers.get_serializer('nonsense')
92+        self.assertEqual(cm.exception.args, ('nonsense',))
93 
94 def serializerTest(format, self):
95