Author: jezdez
Date: 2012-02-09 10:56:58 -0800 (Thu, 09 Feb 2012)
New Revision: 17469

Modified:
   django/trunk/django/core/serializers/json.py
   django/trunk/django/core/serializers/pyyaml.py
   django/trunk/tests/regressiontests/serializers_regress/tests.py
Log:
Fixed #11970 -- Wrapped the exception happening during deserialization in 
DeserializationError exceptions. Thanks, Claude Paroz.

Modified: django/trunk/django/core/serializers/json.py
===================================================================
--- django/trunk/django/core/serializers/json.py        2012-02-09 18:56:49 UTC 
(rev 17468)
+++ django/trunk/django/core/serializers/json.py        2012-02-09 18:56:58 UTC 
(rev 17469)
@@ -6,6 +6,7 @@
 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
@@ -27,6 +28,7 @@
         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.
@@ -35,9 +37,14 @@
         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):
     """
     JSONEncoder subclass that knows how to encode date/time and decimal types.

Modified: django/trunk/django/core/serializers/pyyaml.py
===================================================================
--- django/trunk/django/core/serializers/pyyaml.py      2012-02-09 18:56:49 UTC 
(rev 17468)
+++ django/trunk/django/core/serializers/pyyaml.py      2012-02-09 18:56:58 UTC 
(rev 17469)
@@ -9,6 +9,7 @@
 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
 
@@ -51,6 +52,9 @@
         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)

Modified: django/trunk/tests/regressiontests/serializers_regress/tests.py
===================================================================
--- django/trunk/tests/regressiontests/serializers_regress/tests.py     
2012-02-09 18:56:49 UTC (rev 17468)
+++ django/trunk/tests/regressiontests/serializers_regress/tests.py     
2012-02-09 18:56:58 UTC (rev 17469)
@@ -19,6 +19,7 @@
 
 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
@@ -390,6 +391,17 @@
         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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to