diff --git a/pkgs/atlas-search/crates/atlas-search/src/pyjson.rs b/pkgs/atlas-search/crates/atlas-search/src/pyjson.rs index 7a7c5a12d5..0821c62628 100644 --- a/pkgs/atlas-search/crates/atlas-search/src/pyjson.rs +++ b/pkgs/atlas-search/crates/atlas-search/src/pyjson.rs @@ -104,7 +104,7 @@ pub fn py_string(s: &str, ensure_ascii: bool) -> String { c if (c as u32) < 0x20 => { let _ = write!(out, "\\u{:04x}", c as u32); } - c if ensure_ascii && (c as u32) > 0x7f => { + c if ensure_ascii && (c as u32) >= 0x7f => { if (c as u32) > 0xffff { let v = (c as u32) - 0x10000; let _ = write!( @@ -257,6 +257,19 @@ mod tests { ); } + #[test] + fn compact_escapes_del_like_cpython() { + // CPython's ensure_ascii encoder escapes everything outside + // 0x20..=0x7e, so DEL (0x7f) becomes \u007f even though it is + // ASCII; with ensure_ascii=False it stays raw (#5404). + assert_eq!(py_string("\u{7f}", true), "\"\\u007f\""); + assert_eq!(py_string("\u{7f}", false), "\"\u{7f}\""); + assert_eq!(py_string("~", true), "\"~\""); + assert_eq!(py_string("\u{80}", true), "\"\\u0080\""); + let v = json!({"k": "x\u{7f}y"}); + assert_eq!(dumps_compact(&v), "{\"k\": \"x\\u007fy\"}"); + } + #[test] fn compact_escapes_non_ascii() { let v = json!({"k": "café"});