gh-140793: Improve documentatation and tests for the ensure_ascii option in the json module (GH-140906)

* Document that ensure_ascii=True forces escaping not only non-ASCII, but also
  non-printable characters (the only affected ASCII character is U+007F).
* Ensure that the help output for the json module does not exceed 80
  columns (except one long line in an example and generated lines).
* Add more tests.
This commit is contained in:
Serhiy Storchaka 2025-11-08 12:07:27 +02:00 committed by GitHub
parent 8cec3d3a9d
commit 7e90bac3cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 89 additions and 43 deletions

View file

@ -111,9 +111,10 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
encoding of keys that are not str, int, float, bool or None.
If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str
objects with all incoming non-ASCII characters escaped. If
ensure_ascii is false, the output can contain non-ASCII characters.
If ensure_ascii is true, the output is guaranteed to be str objects
with all incoming non-ASCII and non-printable characters escaped.
If ensure_ascii is false, the output can contain non-ASCII and
non-printable characters.
If check_circular is true, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to
@ -134,14 +135,15 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
indent level. An indent level of 0 will only insert newlines.
None is the most compact representation.
If specified, separators should be an (item_separator, key_separator)
tuple. The default is (', ', ': ') if *indent* is ``None`` and
(',', ': ') otherwise. To get the most compact JSON representation,
you should specify (',', ':') to eliminate whitespace.
If specified, separators should be an (item_separator,
key_separator) tuple. The default is (', ', ': ') if *indent* is
``None`` and (',', ': ') otherwise. To get the most compact JSON
representation, you should specify (',', ':') to eliminate
whitespace.
If specified, default is a function that gets called for objects
that can't otherwise be serialized. It should return a JSON encodable
version of the object or raise a ``TypeError``.
that can't otherwise be serialized. It should return a JSON
encodable version of the object or raise a ``TypeError``.
"""