Skip to content

Commit a8abe09

Browse files
Carreaumethane
authored andcommitted
bpo-33461: emit DeprecationWarning when json.loads(encoding=...) is used (GH-6762)
1 parent 5909ad1 commit a8abe09

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

Doc/library/json.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,21 @@ Basic Usage
265265
*fp* can now be a :term:`binary file`. The input encoding should be
266266
UTF-8, UTF-16 or UTF-32.
267267

268-
.. function:: loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
268+
.. function:: loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
269269

270270
Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
271271
instance containing a JSON document) to a Python object using this
272272
:ref:`conversion table <json-to-py-table>`.
273273

274274
The other arguments have the same meaning as in :func:`load`, except
275-
*encoding* which is ignored and deprecated.
275+
*encoding* which is ignored and deprecated since Python 3.1.
276276

277277
If the data being deserialized is not a valid JSON document, a
278278
:exc:`JSONDecodeError` will be raised.
279279

280+
.. deprecated-removed:: 3.1 3.9
281+
*encoding* keyword argument.
282+
280283
.. versionchanged:: 3.6
281284
*s* can now be of type :class:`bytes` or :class:`bytearray`. The
282285
input encoding should be UTF-8, UTF-16 or UTF-32.

Lib/json/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None,
296296
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297297

298298

299-
def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
299+
def loads(s, *, cls=None, object_hook=None, parse_float=None,
300300
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
301301
"""Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
302302
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,
330330
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
331331
kwarg; otherwise ``JSONDecoder`` is used.
332332
333-
The ``encoding`` argument is ignored and deprecated.
333+
The ``encoding`` argument is ignored and deprecated since Python 3.1.
334334
"""
335335
if isinstance(s, str):
336336
if s.startswith('\ufeff'):
@@ -342,6 +342,15 @@ def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
342342
f'not {s.__class__.__name__}')
343343
s = s.decode(detect_encoding(s), 'surrogatepass')
344344

345+
if "encoding" in kw:
346+
import warnings
347+
warnings.warn(
348+
"'encoding' is ignored and deprecated. It will be removed in Python 3.9",
349+
DeprecationWarning,
350+
stacklevel=2
351+
)
352+
del kw['encoding']
353+
345354
if (cls is None and object_hook is None and
346355
parse_int is None and parse_float is None and
347356
parse_constant is None and object_pairs_hook is None and not kw):

Lib/test/test_json/test_decode.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,9 @@ def test_negative_index(self):
9595
d = self.json.JSONDecoder()
9696
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
9797

98+
def test_deprecated_encode(self):
99+
with self.assertWarns(DeprecationWarning):
100+
self.loads('{}', encoding='fake')
101+
98102
class TestPyDecode(TestDecode, PyTest): pass
99103
class TestCDecode(TestDecode, CTest): pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``json.loads`` now emits ``DeprecationWarning`` when ``encoding`` option is
2+
specified. Patch by Matthias Bussonnier.

0 commit comments

Comments
 (0)