Commits

Anonymous committed f238b96

Fixed #11970 -- Wrapped the exception happening during deserialization in DeserializationError exceptions. Thanks, Claude Paroz.

Comments (0)

Files changed (3)

django/core/serializers/json.py

 import decimal
 from StringIO import StringIO
 
+from django.core.serializers.base import DeserializationError
 from django.core.serializers.python import Serializer as PythonSerializer
 from django.core.serializers.python import Deserializer as PythonDeserializer
 from django.utils import simplejson
         if callable(getattr(self.stream, 'getvalue', None)):
             return self.stream.getvalue()
 
+
 def Deserializer(stream_or_string, **options):
     """
     Deserialize a stream or string of JSON data.
         stream = StringIO(stream_or_string)
     else:
         stream = stream_or_string
-    for obj in PythonDeserializer(simplejson.load(stream), **options):
-        yield obj
+    try:
+        for obj in PythonDeserializer(simplejson.load(stream), **options):
+            yield obj
+    except Exception, e:
+        # Map to deserializer error
+        raise DeserializationError(e)
+
 
 class DjangoJSONEncoder(simplejson.JSONEncoder):
     """

django/core/serializers/pyyaml.py

 import yaml
 
 from django.db import models
+from django.core.serializers.base import DeserializationError
 from django.core.serializers.python import Serializer as PythonSerializer
 from django.core.serializers.python import Deserializer as PythonDeserializer
 
         stream = StringIO(stream_or_string)
     else:
         stream = stream_or_string
-    for obj in PythonDeserializer(yaml.safe_load(stream), **options):
-        yield obj
-
+    try:
+        for obj in PythonDeserializer(yaml.safe_load(stream), **options):
+            yield obj
+    except Exception, e:
+        # Map to deserializer error
+        raise DeserializationError(e)

tests/regressiontests/serializers_regress/tests.py

 
 from django.core import serializers
 from django.core.serializers import SerializerDoesNotExist
+from django.core.serializers.base import DeserializationError
 from django.db import connection, models
 from django.test import TestCase
 from django.utils.functional import curry
         with self.assertRaises(SerializerDoesNotExist):
             serializers.get_deserializer("nonsense")
 
+    def test_json_deserializer_exception(self):
+        with self.assertRaises(DeserializationError):
+            for obj in serializers.deserialize("json", """[{"pk":1}"""):
+                pass
+
+    def test_yaml_deserializer_exception(self):
+        with self.assertRaises(DeserializationError):
+            for obj in serializers.deserialize("yaml", "{"):
+                pass
+
+
 def serializerTest(format, self):
 
     # Create all the objects defined in the test data