-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Closed
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15new features, bugs and security fixesnew features, bugs and security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirextension-modulesC modules in the Modules dirC modules in the Modules dir
Description
Bug report
Bug description:
There is an inconsistency in Python's json module between py_encode_basestring and JSONEncoder.encode() when handling the DEL character (U+007F, \x7f) with ensure_ascii=True.
The DEL character (ASCII code 127) is being incorrectly escaped as \u007f by JSONEncoder.encode() when ensure_ascii=True, while py_encode_basestring() correctly outputs it as a literal character.
from json.encoder import py_encode_basestring, JSONEncoder
s = '\x7f'
encoder = JSONEncoder(ensure_ascii=True)
expected = encoder.encode(s)
actual = py_encode_basestring(s)
assert actual == expected, f"py_encode_basestring({s!r}) = {actual!r}, but JSONEncoder().encode({s!r}) = {expected!r}"Traceback (most recent call last):
File "/data/test.py", line 10, in <module>
assert actual == expected, f"py_encode_basestring({s!r}) = {actual!r}, but JSONEncoder().encode({s!r}) = {expected!r}"
^^^^^^^^^^^^^^^^^^
AssertionError: py_encode_basestring('\x7f') = '"\x7f"', but JSONEncoder().encode('\x7f') = '"\\u007f"'
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-140793: Make
\x7fhandling consistent across JSON #140794 - gh-140793: Improve documentation for the ensure_ascii option in the json module #140906
- [3.14] gh-140793: Improve documentatation and tests for the ensure_ascii option in the json module (GH-140906) #141227
- [3.13] gh-140793: Improve documentatation and tests for the ensure_ascii option in the json module (GH-140906) #141228
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15new features, bugs and security fixesnew features, bugs and security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirextension-modulesC modules in the Modules dirC modules in the Modules dir
Projects
Status
Done
Status
Todo