Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added yaml directly into BUILTIN_SERIALIZERS.

The serializer definitely exists, but the dependent yaml module may not
be installed.  The register_serializer() function will catch exceptions
and will stub in a fake serializer object that will raise the exception
when the serializer is used.
  • Loading branch information...
commit c72392dab4b4cf6f5d9b35a3dcb582fbfe38ebb8 1 parent d8d61d8
Roberto Aguilar authored

Showing 1 changed file with 32 additions and 8 deletions. Show diff stats Hide diff stats

  1. 40  django/core/serializers/__init__.py
40  django/core/serializers/__init__.py
@@ -17,6 +17,7 @@
17 17
 """
18 18
 
19 19
 import importlib
  20
+import sys
20 21
 
21 22
 from django.conf import settings
22 23
 from django.utils import six
@@ -27,17 +28,29 @@
27 28
     "xml"    : "django.core.serializers.xml_serializer",
28 29
     "python" : "django.core.serializers.python",
29 30
     "json"   : "django.core.serializers.json",
  31
+    "yaml"   : "django.core.serializers.pyyaml",
30 32
 }
31 33
 
32  
-# Check for PyYaml and register the serializer if it's available.
33  
-try:
34  
-    import yaml
35  
-    BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
36  
-except ImportError:
37  
-    pass
38  
-
39 34
 _serializers = {}
40 35
 
  36
+
  37
+class BadSerializer(object):
  38
+    """
  39
+    Stub serializer to hold exception raised during registration
  40
+
  41
+    This allows the serializer registration to cache serializers and if there
  42
+    is an error raised in the process of creating a serializer it will be
  43
+    raised and passed along to the caller when the serializer is used.
  44
+    """
  45
+    internal_use_only = False
  46
+
  47
+    def __init__(self, exception):
  48
+        self.exception = exception
  49
+
  50
+    def __call__(self, *args, **kwargs):
  51
+        raise self.exception
  52
+
  53
+
41 54
 def register_serializer(format, serializer_module, serializers=None):
42 55
     """Register a new serializer.
43 56
 
@@ -53,12 +66,23 @@ def register_serializer(format, serializer_module, serializers=None):
53 66
     """
54 67
     if serializers is None and not _serializers:
55 68
         _load_serializers()
56  
-    module = importlib.import_module(serializer_module)
  69
+
  70
+    try:
  71
+        module = importlib.import_module(serializer_module)
  72
+    except ImportError, exc:
  73
+        bad_serializer = BadSerializer(exc)
  74
+
  75
+        module = type('BadSerializerModule', (object,), {
  76
+            'Deserializer': bad_serializer,
  77
+            'Serializer': bad_serializer,
  78
+        })
  79
+
57 80
     if serializers is None:
58 81
         _serializers[format] = module
59 82
     else:
60 83
         serializers[format] = module
61 84
 
  85
+
62 86
 def unregister_serializer(format):
63 87
     "Unregister a given serializer. This is not a thread-safe operation."
64 88
     if not _serializers:

0 notes on commit c72392d

Please sign in to comment.
Something went wrong with that request. Please try again.