summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2013-09-06 16:05:02 -0700
committerRussell Keith-Magee <russell@keith-magee.com>2013-09-06 16:05:02 -0700
commit4f5faa1916e7c8cb72cc9ebf1a1fd964ba6e707b (patch)
treead8e10a4c0ae8b54bd10790358a78a004c6f8b71 /django
parent9b2dc12b8332389d1bfb9e83123a088a084a6a47 (diff)
parent01a5359477fbc82488dddf9476581fe72c049b51 (diff)
Merge pull request #1582 from rca/12756-missing-yaml-module-serializer-error-message
Fixed #12756: Improved error message when yaml module is missing.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/dumpdata.py8
-rw-r--r--django/core/serializers/__init__.py39
2 files changed, 35 insertions, 12 deletions
diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
index c74eede846..ed58ec79a1 100644
--- a/django/core/management/commands/dumpdata.py
+++ b/django/core/management/commands/dumpdata.py
@@ -106,11 +106,11 @@ class Command(BaseCommand):
# Check that the serialization format exists; this is a shortcut to
# avoid collating all the objects and _then_ failing.
if format not in serializers.get_public_serializer_formats():
- raise CommandError("Unknown serialization format: %s" % format)
+ try:
+ serializers.get_serializer(format)
+ except serializers.SerializerDoesNotExist:
+ pass
- try:
- serializers.get_serializer(format)
- except KeyError:
raise CommandError("Unknown serialization format: %s" % format)
def get_objects():
diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
index dc3d139d3b..89d9877ebf 100644
--- a/django/core/serializers/__init__.py
+++ b/django/core/serializers/__init__.py
@@ -27,17 +27,29 @@ BUILTIN_SERIALIZERS = {
"xml" : "django.core.serializers.xml_serializer",
"python" : "django.core.serializers.python",
"json" : "django.core.serializers.json",
+ "yaml" : "django.core.serializers.pyyaml",
}
-# Check for PyYaml and register the serializer if it's available.
-try:
- import yaml
- BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
-except ImportError:
- pass
-
_serializers = {}
+
+class BadSerializer(object):
+ """
+ Stub serializer to hold exception raised during registration
+
+ This allows the serializer registration to cache serializers and if there
+ is an error raised in the process of creating a serializer it will be
+ raised and passed along to the caller when the serializer is used.
+ """
+ internal_use_only = False
+
+ def __init__(self, exception):
+ self.exception = exception
+
+ def __call__(self, *args, **kwargs):
+ raise self.exception
+
+
def register_serializer(format, serializer_module, serializers=None):
"""Register a new serializer.
@@ -53,12 +65,23 @@ def register_serializer(format, serializer_module, serializers=None):
"""
if serializers is None and not _serializers:
_load_serializers()
- module = importlib.import_module(serializer_module)
+
+ try:
+ module = importlib.import_module(serializer_module)
+ except ImportError, exc:
+ bad_serializer = BadSerializer(exc)
+
+ module = type('BadSerializerModule', (object,), {
+ 'Deserializer': bad_serializer,
+ 'Serializer': bad_serializer,
+ })
+
if serializers is None:
_serializers[format] = module
else:
serializers[format] = module
+
def unregister_serializer(format):
"Unregister a given serializer. This is not a thread-safe operation."
if not _serializers: