Skip to content

JSONEncoder with ensure_ascii=True incorrectly escapes DEL character (U+007F) #140793

@T90REAL

Description

@T90REAL

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

Metadata

Metadata

Labels

3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesdocsDocumentation in the Doc dirextension-modulesC modules in the Modules dir

Projects

Status

Done

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions