Code

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

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

patch + tests + docs

Line 
1diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
2index 7afc94c..30bdfe0 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 SerializerNotFound
10 
11 # Built-in serializers
12 BUILTIN_SERIALIZERS = {
13@@ -63,6 +64,8 @@ def unregister_serializer(format):
14     del _serializers[format]
15 
16 def get_serializer(format):
17+    if format not in get_serializer_formats():
18+        raise SerializerNotFound()
19     if not _serializers:
20         _load_serializers()
21     return _serializers[format].Serializer
22diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
23index cdcc7fa..64fc3d7 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 SerializerNotFound(Exception):
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..80d2885 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.base.SerializerNotFound` 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..00d2a23 100644
56--- a/tests/regressiontests/serializers_regress/tests.py
57+++ b/tests/regressiontests/serializers_regress/tests.py
58@@ -17,6 +17,7 @@ except ImportError:
59 
60 from django.conf import settings
61 from django.core import serializers, management
62+from django.core.serializers.base import SerializerNotFound
63 from django.db import transaction, DEFAULT_DB_ALIAS, connection
64 from django.test import TestCase
65 from django.utils.functional import curry
66@@ -350,7 +351,9 @@ if connection.features.allows_primary_key_0:
67 # Dynamically create serializer tests to ensure that all
68 # registered serializers are automatically tested.
69 class SerializerTests(TestCase):
70-    pass
71+    def test_get_unknown_serializer(self):
72+        self.assertRaises(SerializerNotFound,
73+                          serializers.get_serializer, 'unknown')
74 
75 def serializerTest(format, self):
76 
77@@ -416,3 +419,8 @@ for format in serializers.get_serializer_formats():
78     setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
79     if format != 'python':
80         setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
81+
82+# Regression test for #15889 -- serializers.get_serializer('unknown') used to
83+# raise a KeyError, should now raise a SerializerNotFound.
84+class GetSerializerTests(TestCase):
85+    pass