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

File 15889-get_serializer-specific-exception.diff, 3.3 KB (added by Mathieu Agopian, 13 years ago)

patch + tests + docs

  • django/core/serializers/__init__.py

    diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
    index 7afc94c..30bdfe0 100644
    a b To add your own serializers, use the SERIALIZATION_MODULES setting::  
    1818
    1919from django.conf import settings
    2020from django.utils import importlib
     21from django.core.serializers.base import SerializerNotFound
    2122
    2223# Built-in serializers
    2324BUILTIN_SERIALIZERS = {
    def unregister_serializer(format):  
    6364    del _serializers[format]
    6465
    6566def get_serializer(format):
     67    if format not in get_serializer_formats():
     68        raise SerializerNotFound()
    6669    if not _serializers:
    6770        _load_serializers()
    6871    return _serializers[format].Serializer
  • django/core/serializers/base.py

    diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
    index cdcc7fa..64fc3d7 100644
    a b from django.db import models  
    88from django.utils.encoding import smart_str, smart_unicode
    99from django.utils import datetime_safe
    1010
     11class SerializerNotFound(Exception):
     12    """A requested serializer was not found."""
     13    pass
     14
    1115class SerializationError(Exception):
    1216    """Something bad happened during serialization."""
    1317    pass
  • docs/topics/serialization.txt

    diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
    index c8acc85..80d2885 100644
    a b This is useful if you want to serialize data directly to a file-like object  
    3838    out = open("file.xml", "w")
    3939    xml_serializer.serialize(SomeModel.objects.all(), stream=out)
    4040
     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.base.SerializerNotFound` exception.
     46
    4147Subset of fields
    4248~~~~~~~~~~~~~~~~
    4349
  • tests/regressiontests/serializers_regress/tests.py

    diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
    index de57a6f..00d2a23 100644
    a b except ImportError:  
    1717
    1818from django.conf import settings
    1919from django.core import serializers, management
     20from django.core.serializers.base import SerializerNotFound
    2021from django.db import transaction, DEFAULT_DB_ALIAS, connection
    2122from django.test import TestCase
    2223from django.utils.functional import curry
    if connection.features.allows_primary_key_0:  
    350351# Dynamically create serializer tests to ensure that all
    351352# registered serializers are automatically tested.
    352353class SerializerTests(TestCase):
    353     pass
     354    def test_get_unknown_serializer(self):
     355        self.assertRaises(SerializerNotFound,
     356                          serializers.get_serializer, 'unknown')
    354357
    355358def serializerTest(format, self):
    356359
    for format in serializers.get_serializer_formats():  
    416419    setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
    417420    if format != 'python':
    418421        setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
     422
     423# Regression test for #15889 -- serializers.get_serializer('unknown') used to
     424# raise a KeyError, should now raise a SerializerNotFound.
     425class GetSerializerTests(TestCase):
     426    pass
Back to Top