From 6bf3470b013d62a97518546a965fecd9a2855e75 Mon Sep 17 00:00:00 2001 From: David Edwards Date: Sun, 27 Dec 2020 08:49:45 -0500 Subject: [PATCH 1/2] Added six.JSONDecodeError exception --- documentation/index.rst | 15 +++++++++++++++ six.py | 2 ++ test_six.py | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/documentation/index.rst b/documentation/index.rst index 45390b81b..78a65f749 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -843,6 +843,21 @@ Contains classes from Python 3's :mod:`py3:urllib.response` and Python 2's: * :class:`py2:urllib.addinfourl` +JSON JSONDecodeError Exception +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +.. currentmodule:: six + +The Python3 *json* module raises the JSONDecodeError exception when it can't +decode the given json input. The Python2 *json* module raises the ValueError +exception under the same cicumstances. + +.. data:: JSONDecodeError + + A JSONDecodeError exception that can be used to catch exceptions raised by + both the Python2 and Python3 *json* modules. + + Advanced - Customizing renames <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< diff --git a/six.py b/six.py index 83f69783d..6cf51b789 100644 --- a/six.py +++ b/six.py @@ -45,6 +45,7 @@ binary_type = bytes MAXSIZE = sys.maxsize + from json import JSONDecodeError else: string_types = basestring, integer_types = (int, long) @@ -70,6 +71,7 @@ def __len__(self): # 64-bit MAXSIZE = int((1 << 63) - 1) del X + JSONDecodeError = ValueError def _add_doc(func, doc): diff --git a/test_six.py b/test_six.py index 7b8b03b5e..cd93abf27 100644 --- a/test_six.py +++ b/test_six.py @@ -1039,3 +1039,11 @@ def test_ensure_text(self): assert converted_unicode == self.UNICODE_EMOJI and isinstance(converted_unicode, str) # PY3: bytes -> str assert converted_binary == self.UNICODE_EMOJI and isinstance(converted_unicode, str) + +def test_JSONDecodeError(): + import json + from six import JSONDecodeError + try: + json.loads('') + except JSONDecodeError: + pass From 49c18eed5935ec7f75d5b3c8469d465396410e37 Mon Sep 17 00:00:00 2001 From: David Edwards Date: Sun, 27 Dec 2020 08:54:58 -0500 Subject: [PATCH 2/2] Fixed typo --- documentation/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/index.rst b/documentation/index.rst index 78a65f749..4e24fb59a 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -850,7 +850,7 @@ JSON JSONDecodeError Exception The Python3 *json* module raises the JSONDecodeError exception when it can't decode the given json input. The Python2 *json* module raises the ValueError -exception under the same cicumstances. +exception under the same circumstances. .. data:: JSONDecodeError