From 98706bb35e7de0e445cc336f669919047bf46b75 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 20 Jun 2017 15:16:37 -0400 Subject: [PATCH] Refs #27857 -- Replaced json.loads() ValueError exception catching with JSONDecodeError. --- django/contrib/admin/models.py | 2 +- django/contrib/messages/storage/cookie.py | 2 +- django/contrib/postgres/forms/hstore.py | 2 +- django/contrib/postgres/forms/jsonb.py | 4 ++-- django/contrib/staticfiles/storage.py | 2 +- django/test/testcases.py | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index 82b3cc0585..2f8ecc88df 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -95,7 +95,7 @@ class LogEntry(models.Model): if self.change_message and self.change_message[0] == '[': try: change_message = json.loads(self.change_message) - except ValueError: + except json.JSONDecodeError: return self.change_message messages = [] for sub_message in change_message: diff --git a/django/contrib/messages/storage/cookie.py b/django/contrib/messages/storage/cookie.py index 49270cac17..6a6a301db7 100644 --- a/django/contrib/messages/storage/cookie.py +++ b/django/contrib/messages/storage/cookie.py @@ -157,7 +157,7 @@ class CookieStorage(BaseStorage): # If we get here (and the JSON decode works), everything is # good. In any other case, drop back and return None. return json.loads(value, cls=MessageDecoder) - except ValueError: + except json.JSONDecodeError: pass # Mark the data as used (so it gets removed) since something was wrong # with the data. diff --git a/django/contrib/postgres/forms/hstore.py b/django/contrib/postgres/forms/hstore.py index 984227ff71..f5af8f10e3 100644 --- a/django/contrib/postgres/forms/hstore.py +++ b/django/contrib/postgres/forms/hstore.py @@ -28,7 +28,7 @@ class HStoreField(forms.CharField): if not isinstance(value, dict): try: value = json.loads(value) - except ValueError: + except json.JSONDecodeError: raise ValidationError( self.error_messages['invalid_json'], code='invalid_json', diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py index 2cb6092cb7..158ab8fdb9 100644 --- a/django/contrib/postgres/forms/jsonb.py +++ b/django/contrib/postgres/forms/jsonb.py @@ -29,7 +29,7 @@ class JSONField(forms.CharField): return value try: converted = json.loads(value) - except ValueError: + except json.JSONDecodeError: raise forms.ValidationError( self.error_messages['invalid'], code='invalid', @@ -45,7 +45,7 @@ class JSONField(forms.CharField): return initial try: return json.loads(data) - except ValueError: + except json.JSONDecodeError: return InvalidJSONInput(data) def prepare_value(self, value): diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index ad9b4b0124..2e228bd90e 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -391,7 +391,7 @@ class ManifestFilesMixin(HashedFilesMixin): return OrderedDict() try: stored = json.loads(content, object_pairs_hook=OrderedDict) - except ValueError: + except json.JSONDecodeError: pass else: version = stored.get('version') diff --git a/django/test/testcases.py b/django/test/testcases.py index ff17639d51..afab58f2dc 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -708,7 +708,7 @@ class SimpleTestCase(unittest.TestCase): """ try: data = json.loads(raw) - except ValueError: + except json.JSONDecodeError: self.fail("First argument is not valid JSON: %r" % raw) if isinstance(expected_data, str): try: @@ -725,12 +725,12 @@ class SimpleTestCase(unittest.TestCase): """ try: data = json.loads(raw) - except ValueError: + except json.JSONDecodeError: self.fail("First argument is not valid JSON: %r" % raw) if isinstance(expected_data, str): try: expected_data = json.loads(expected_data) - except ValueError: + except json.JSONDecodeError: self.fail("Second argument is not valid JSON: %r" % expected_data) self.assertNotEqual(data, expected_data, msg=msg)