mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
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:
parent
8cec3d3a9d
commit
7e90bac3cc
6 changed files with 89 additions and 43 deletions
|
|
@ -32,6 +32,29 @@ def test_encoding7(self):
|
|||
j = self.dumps(u + "\n", ensure_ascii=False)
|
||||
self.assertEqual(j, f'"{u}\\n"')
|
||||
|
||||
def test_ascii_non_printable_encode(self):
|
||||
u = '\b\t\n\f\r\x00\x1f\x7f'
|
||||
self.assertEqual(self.dumps(u),
|
||||
'"\\b\\t\\n\\f\\r\\u0000\\u001f\\u007f"')
|
||||
self.assertEqual(self.dumps(u, ensure_ascii=False),
|
||||
'"\\b\\t\\n\\f\\r\\u0000\\u001f\x7f"')
|
||||
|
||||
def test_ascii_non_printable_decode(self):
|
||||
self.assertEqual(self.loads('"\\b\\t\\n\\f\\r"'),
|
||||
'\b\t\n\f\r')
|
||||
s = ''.join(map(chr, range(32)))
|
||||
for c in s:
|
||||
self.assertRaises(self.JSONDecodeError, self.loads, f'"{c}"')
|
||||
self.assertEqual(self.loads(f'"{s}"', strict=False), s)
|
||||
self.assertEqual(self.loads('"\x7f"'), '\x7f')
|
||||
|
||||
def test_escaped_decode(self):
|
||||
self.assertEqual(self.loads('"\\b\\t\\n\\f\\r"'), '\b\t\n\f\r')
|
||||
self.assertEqual(self.loads('"\\"\\\\\\/"'), '"\\/')
|
||||
for c in set(map(chr, range(0x100))) - set('"\\/bfnrt'):
|
||||
self.assertRaises(self.JSONDecodeError, self.loads, f'"\\{c}"')
|
||||
self.assertRaises(self.JSONDecodeError, self.loads, f'"\\{c}"', strict=False)
|
||||
|
||||
def test_big_unicode_encode(self):
|
||||
u = '\U0001d120'
|
||||
self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')
|
||||
|
|
@ -48,6 +71,18 @@ def test_unicode_decode(self):
|
|||
s = f'"\\u{i:04x}"'
|
||||
self.assertEqual(self.loads(s), u)
|
||||
|
||||
def test_single_surrogate_encode(self):
|
||||
self.assertEqual(self.dumps('\uD83D'), '"\\ud83d"')
|
||||
self.assertEqual(self.dumps('\uD83D', ensure_ascii=False), '"\ud83d"')
|
||||
self.assertEqual(self.dumps('\uDC0D'), '"\\udc0d"')
|
||||
self.assertEqual(self.dumps('\uDC0D', ensure_ascii=False), '"\udc0d"')
|
||||
|
||||
def test_single_surrogate_decode(self):
|
||||
self.assertEqual(self.loads('"\uD83D"'), '\ud83d')
|
||||
self.assertEqual(self.loads('"\\uD83D"'), '\ud83d')
|
||||
self.assertEqual(self.loads('"\udc0d"'), '\udc0d')
|
||||
self.assertEqual(self.loads('"\\udc0d"'), '\udc0d')
|
||||
|
||||
def test_unicode_preservation(self):
|
||||
self.assertEqual(type(self.loads('""')), str)
|
||||
self.assertEqual(type(self.loads('"a"')), str)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue