diff options
author | Victor Stinner <vstinner@python.org> | 2021-11-17 20:12:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-17 20:12:20 (GMT) |
commit | b919d8105c4d77f00509b6d3ab2073f09db640de (patch) | |
tree | 868130697a285fa007cd902d91d66ad9953a410a /Python | |
parent | e002bbc6cce637171fb2b1391ffeca8643a13843 (diff) | |
download | cpython-b919d8105c4d77f00509b6d3ab2073f09db640de.zip cpython-b919d8105c4d77f00509b6d3ab2073f09db640de.tar.gz cpython-b919d8105c4d77f00509b6d3ab2073f09db640de.tar.bz2 |
bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596)
If the string is ASCII only and doesn't need to escape characters,
write the whole string with a single write() syscall.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/traceback.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/traceback.c b/Python/traceback.c index 67f995a..8aef3d8 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -1072,6 +1072,26 @@ _Py_DumpASCII(int fd, PyObject *text) truncated = 0; } + // Is an ASCII string? + if (ascii->state.ascii) { + assert(kind == PyUnicode_1BYTE_KIND); + char *str = data; + + int need_escape = 0; + for (i=0; i < size; i++) { + ch = str[i]; + if (!(' ' <= ch && ch <= 126)) { + need_escape = 1; + break; + } + } + if (!need_escape) { + // The string can be written with a single write() syscall + _Py_write_noraise(fd, str, size); + goto done; + } + } + for (i=0; i < size; i++) { if (kind != PyUnicode_WCHAR_KIND) ch = PyUnicode_READ(kind, data, i); @@ -1095,6 +1115,8 @@ _Py_DumpASCII(int fd, PyObject *text) _Py_DumpHexadecimal(fd, ch, 8); } } + +done: if (truncated) { PUTS(fd, "..."); } |