Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11970 -- Wrapped the exception happening during deserializatio…

…n in DeserializationError exceptions. Thanks, Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17469 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 49288f838809d797fa7b8b2e6a2cb2f65d6cf8bd 1 parent 75a9c8a
Jannis Leidel authored February 09, 2012
11  django/core/serializers/json.py
@@ -6,6 +6,7 @@
6 6
 import decimal
7 7
 from StringIO import StringIO
8 8
 
  9
+from django.core.serializers.base import DeserializationError
9 10
 from django.core.serializers.python import Serializer as PythonSerializer
10 11
 from django.core.serializers.python import Deserializer as PythonDeserializer
11 12
 from django.utils import simplejson
@@ -27,6 +28,7 @@ def getvalue(self):
27 28
         if callable(getattr(self.stream, 'getvalue', None)):
28 29
             return self.stream.getvalue()
29 30
 
  31
+
30 32
 def Deserializer(stream_or_string, **options):
31 33
     """
32 34
     Deserialize a stream or string of JSON data.
@@ -35,8 +37,13 @@ def Deserializer(stream_or_string, **options):
35 37
         stream = StringIO(stream_or_string)
36 38
     else:
37 39
         stream = stream_or_string
38  
-    for obj in PythonDeserializer(simplejson.load(stream), **options):
39  
-        yield obj
  40
+    try:
  41
+        for obj in PythonDeserializer(simplejson.load(stream), **options):
  42
+            yield obj
  43
+    except Exception, e:
  44
+        # Map to deserializer error
  45
+        raise DeserializationError(e)
  46
+
40 47
 
41 48
 class DjangoJSONEncoder(simplejson.JSONEncoder):
42 49
     """
10  django/core/serializers/pyyaml.py
@@ -9,6 +9,7 @@
9 9
 import yaml
10 10
 
11 11
 from django.db import models
  12
+from django.core.serializers.base import DeserializationError
12 13
 from django.core.serializers.python import Serializer as PythonSerializer
13 14
 from django.core.serializers.python import Deserializer as PythonDeserializer
14 15
 
@@ -51,6 +52,9 @@ def Deserializer(stream_or_string, **options):
51 52
         stream = StringIO(stream_or_string)
52 53
     else:
53 54
         stream = stream_or_string
54  
-    for obj in PythonDeserializer(yaml.safe_load(stream), **options):
55  
-        yield obj
56  
-
  55
+    try:
  56
+        for obj in PythonDeserializer(yaml.safe_load(stream), **options):
  57
+            yield obj
  58
+    except Exception, e:
  59
+        # Map to deserializer error
  60
+        raise DeserializationError(e)
12  tests/regressiontests/serializers_regress/tests.py
@@ -19,6 +19,7 @@
19 19
 
20 20
 from django.core import serializers
21 21
 from django.core.serializers import SerializerDoesNotExist
  22
+from django.core.serializers.base import DeserializationError
22 23
 from django.db import connection, models
23 24
 from django.test import TestCase
24 25
 from django.utils.functional import curry
@@ -390,6 +391,17 @@ def test_get_unkown_deserializer(self):
390 391
         with self.assertRaises(SerializerDoesNotExist):
391 392
             serializers.get_deserializer("nonsense")
392 393
 
  394
+    def test_json_deserializer_exception(self):
  395
+        with self.assertRaises(DeserializationError):
  396
+            for obj in serializers.deserialize("json", """[{"pk":1}"""):
  397
+                pass
  398
+
  399
+    def test_yaml_deserializer_exception(self):
  400
+        with self.assertRaises(DeserializationError):
  401
+            for obj in serializers.deserialize("yaml", "{"):
  402
+                pass
  403
+
  404
+
393 405
 def serializerTest(format, self):
394 406
 
395 407
     # Create all the objects defined in the test data

0 notes on commit 49288f8

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