Skip to content

Commit

Permalink
Fixed #15889 -- when trying to access to access a serializer that doe…
Browse files Browse the repository at this point in the history
…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
Alex authored and Alex committed Apr 26, 2011
1 parent 2aa8a21 commit 5ee9f24
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -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>
Expand Down
7 changes: 7 additions & 0 deletions django/core/serializers/__init__.py
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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():
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions django/core/serializers/base.py
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/topics/serialization.txt
Expand Up @@ -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
~~~~~~~~~~~~~~~~

Expand Down
26 changes: 24 additions & 2 deletions tests/regressiontests/serializers_regress/tests.py
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 5ee9f24

Please sign in to comment.