From 06623b56fd6c563b18ac3cbebd39ff353a96fb99 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Thu, 10 May 2018 19:31:39 -0700 Subject: [PATCH 1/5] bpo-33461: Emit DeprecationWarning when json.loads(encoding=...) is used. I also remove the `encoding` keyword from the docs docs. --- Doc/library/json.rst | 7 +++++-- Lib/json/__init__.py | 13 +++++++++++-- Lib/test/test_json/test_decode.py | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Doc/library/json.rst b/Doc/library/json.rst index 589e86ca81071b..ab1a1b45299575 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -265,18 +265,21 @@ Basic Usage *fp* can now be a :term:`binary file`. The input encoding should be UTF-8, UTF-16 or UTF-32. -.. function:: loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) +.. function:: loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray` instance containing a JSON document) to a Python object using this :ref:`conversion table `. The other arguments have the same meaning as in :func:`load`, except - *encoding* which is ignored and deprecated. + *encoding* which is ignored and deprecated since Python 3.1. If the data being deserialized is not a valid JSON document, a :exc:`JSONDecodeError` will be raised. + .. deprecated:: 3.1 + The *encoding* keyword argument has been deprecated and will be ignored. + .. versionchanged:: 3.6 *s* can now be of type :class:`bytes` or :class:`bytearray`. The input encoding should be UTF-8, UTF-16 or UTF-32. diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index 3bb4490e818ba0..456b6f79388ff7 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -296,7 +296,7 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) -def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, +def loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance containing a JSON document) to a Python object. @@ -330,7 +330,7 @@ def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg; otherwise ``JSONDecoder`` is used. - The ``encoding`` argument is ignored and deprecated. + The ``encoding`` argument is ignored and deprecated since Python 3.1. """ if isinstance(s, str): if s.startswith('\ufeff'): @@ -342,6 +342,15 @@ def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, f'not {s.__class__.__name__}') s = s.decode(detect_encoding(s), 'surrogatepass') + if "encoding" is not kw: + import warnings + warnings.warn( + "passing 'encoding' as a keyword argument is deprecated since" + " Python 3.1, is ignored and will be removed in Python 3.9.", + DeprecationWarning, + stacklevel=2 + ) + if (cls is None and object_hook is None and parse_int is None and parse_float is None and parse_constant is None and object_pairs_hook is None and not kw): diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py index fdb9e62124ece1..895c95b54c3b65 100644 --- a/Lib/test/test_json/test_decode.py +++ b/Lib/test/test_json/test_decode.py @@ -95,5 +95,9 @@ def test_negative_index(self): d = self.json.JSONDecoder() self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000) + def test_deprecated_encode(self): + with self.assertWarns(DeprecationWarning): + self.loads('{}', encoding='fake') + class TestPyDecode(TestDecode, PyTest): pass class TestCDecode(TestDecode, CTest): pass From a0a5a0a36690003c702aa6bf65a13d3bd176d5e4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 8 Apr 2019 17:47:06 -0700 Subject: [PATCH 2/5] Update Lib/json/__init__.py Co-Authored-By: Carreau --- Lib/json/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index 456b6f79388ff7..6e5a1aa3096dc8 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -342,7 +342,7 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None, f'not {s.__class__.__name__}') s = s.decode(detect_encoding(s), 'surrogatepass') - if "encoding" is not kw: + if "encoding" not in kw: import warnings warnings.warn( "passing 'encoding' as a keyword argument is deprecated since" From 0ccae85ce1abeb2f69ed108772cee4fd74d0b228 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 8 Apr 2019 17:52:14 -0700 Subject: [PATCH 3/5] Update to take into accounts comments. --- Doc/library/json.rst | 4 ++-- Lib/json/__init__.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/json.rst b/Doc/library/json.rst index ab1a1b45299575..b476c372370d06 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -277,8 +277,8 @@ Basic Usage If the data being deserialized is not a valid JSON document, a :exc:`JSONDecodeError` will be raised. - .. deprecated:: 3.1 - The *encoding* keyword argument has been deprecated and will be ignored. + .. deprecated-removed:: 3.1 3.9 + *encoding* keyword argument. .. versionchanged:: 3.6 *s* can now be of type :class:`bytes` or :class:`bytearray`. The diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index 6e5a1aa3096dc8..c56da4b12df895 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -345,11 +345,11 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None, if "encoding" not in kw: import warnings warnings.warn( - "passing 'encoding' as a keyword argument is deprecated since" - " Python 3.1, is ignored and will be removed in Python 3.9.", + "'encoding' is ignored and deprecated. It will be removed in Python 3.9", DeprecationWarning, stacklevel=2 ) + del kw['encoding'] if (cls is None and object_hook is None and parse_int is None and parse_float is None and From a7e4acbe8331494f5f6b79ff631987017e5feab4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 9 Apr 2019 14:01:01 +0900 Subject: [PATCH 4/5] Update __init__.py --- Lib/json/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index c56da4b12df895..1ba8b48bd78cde 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -342,7 +342,7 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None, f'not {s.__class__.__name__}') s = s.decode(detect_encoding(s), 'surrogatepass') - if "encoding" not in kw: + if "encoding" in kw: import warnings warnings.warn( "'encoding' is ignored and deprecated. It will be removed in Python 3.9", From 8e3188099e955bf5b34d2053b1c87620e7bc0a1a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 9 Apr 2019 14:46:32 +0900 Subject: [PATCH 5/5] add news entry --- .../next/Library/2019-04-09-14-46-28.bpo-33461.SYJM-E.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-04-09-14-46-28.bpo-33461.SYJM-E.rst diff --git a/Misc/NEWS.d/next/Library/2019-04-09-14-46-28.bpo-33461.SYJM-E.rst b/Misc/NEWS.d/next/Library/2019-04-09-14-46-28.bpo-33461.SYJM-E.rst new file mode 100644 index 00000000000000..12b3bceaf8e3c0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-04-09-14-46-28.bpo-33461.SYJM-E.rst @@ -0,0 +1,2 @@ +``json.loads`` now emits ``DeprecationWarning`` when ``encoding`` option is +specified. Patch by Matthias Bussonnier.