Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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 alex authored
View
1  AUTHORS
@@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better:
Gisle Aas <gisle@aas.no>
Chris Adams
+ Mathieu Agopian <mathieu.agopian@gmail.com>
ajs <adi@sieker.info>
alang@bright-green.com
A S Alam <aalam@users.sf.net>
View
7 django/core/serializers/__init__.py
@@ -18,6 +18,7 @@
from django.conf import settings
from django.utils import importlib
+from django.core.serializers.base import SerializerDoesNotExist
# Built-in serializers
BUILTIN_SERIALIZERS = {
@@ -60,11 +61,15 @@ def unregister_serializer(format):
"Unregister a given serializer. This is not a thread-safe operation."
if not _serializers:
_load_serializers()
+ if format not in _serializers:
+ raise SerializerDoesNotExist(format)
del _serializers[format]
def get_serializer(format):
if not _serializers:
_load_serializers()
+ if format not in _serializers:
+ raise SerializerDoesNotExist(format)
return _serializers[format].Serializer
def get_serializer_formats():
@@ -80,6 +85,8 @@ def get_public_serializer_formats():
def get_deserializer(format):
if not _serializers:
_load_serializers()
+ if format not in _serializers:
+ raise SerializerDoesNotExist(format)
return _serializers[format].Deserializer
def serialize(format, queryset, **options):
View
4 django/core/serializers/base.py
@@ -8,6 +8,10 @@
from django.utils.encoding import smart_str, smart_unicode
from django.utils import datetime_safe
+class SerializerDoesNotExist(KeyError):
+ """The requested serializer was not found."""
+ pass
+
class SerializationError(Exception):
"""Something bad happened during serialization."""
pass
View
6 docs/topics/serialization.txt
@@ -38,6 +38,12 @@ This is useful if you want to serialize data directly to a file-like object
out = open("file.xml", "w")
xml_serializer.serialize(SomeModel.objects.all(), stream=out)
+.. note::
+
+ Calling :func:`~django.core.serializers.get_serializer` with an unknown
+ :ref:`format <serialization-formats>` will raise a
+ :class:`~django.core.serializers.SerializerDoesNotExist` exception.
+
Subset of fields
~~~~~~~~~~~~~~~~
View
26 tests/regressiontests/serializers_regress/tests.py
@@ -6,7 +6,7 @@
the serializers. This includes all valid data values, plus
forward, backwards and self references.
"""
-
+from __future__ import with_statement
import datetime
import decimal
@@ -17,6 +17,7 @@
from django.conf import settings
from django.core import serializers, management
+from django.core.serializers import SerializerDoesNotExist
from django.db import transaction, DEFAULT_DB_ALIAS, connection
from django.test import TestCase
from django.utils.functional import curry
@@ -350,7 +351,28 @@ def inherited_compare(testcase, pk, klass, data):
# Dynamically create serializer tests to ensure that all
# registered serializers are automatically tested.
class SerializerTests(TestCase):
- pass
+ def test_get_unknown_serializer(self):
+ """
+ #15889: get_serializer('nonsense') raises a SerializerDoesNotExist
+ """
+ with self.assertRaises(SerializerDoesNotExist):
+ serializers.get_serializer("nonsense")
+
+ with self.assertRaises(KeyError):
+ serializers.get_serializer("nonsense")
+
+ # SerializerDoesNotExist is instantiated with the nonexistent format
+ with self.assertRaises(SerializerDoesNotExist) as cm:
+ serializers.get_serializer("nonsense")
+ self.assertEqual(cm.exception.args, ("nonsense",))
+
+ def test_unregister_unkown_serializer(self):
+ with self.assertRaises(SerializerDoesNotExist):
+ serializers.unregister_serializer("nonsense")
+
+ def test_get_unkown_deserializer(self):
+ with self.assertRaises(SerializerDoesNotExist):
+ serializers.get_deserializer("nonsense")
def serializerTest(format, self):
Please sign in to comment.
Something went wrong with that request. Please try again.