Skip to content

Commit c2ec66f

Browse files
Merge pull request RustPython#1188 from corona10/hex_format
builtin: Fix hex format of ascii
2 parents c8ce3bd + fafeed1 commit c2ec66f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

tests/snippets/builtin_ascii.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
assert ascii('hello world') == "'hello world'"
22
assert ascii('안녕 세상') == "'\\uc548\\ub155 \\uc138\\uc0c1'"
33
assert ascii('안녕 RustPython') == "'\\uc548\\ub155 RustPython'"
4-
assert ascii(5) == '5'
4+
assert ascii(5) == '5'
5+
assert ascii(chr(0x10001)) == "'\\U00010001'"
6+
assert ascii(chr(0x9999)) == "'\\u9999'"
7+
assert ascii(chr(0x0A)) == "'\\n'"

vm/src/builtins.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
6666
if c.is_ascii() {
6767
ascii.push(c)
6868
} else {
69-
let hex = format!("\\u{:x}", c as i64);
69+
let c = c as i64;
70+
let hex = if c < 0x10000 {
71+
format!("\\u{:04x}", c)
72+
} else {
73+
format!("\\U{:08x}", c)
74+
};
7075
ascii.push_str(&hex)
7176
}
7277
}

0 commit comments

Comments
 (0)