diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-11-17 21:59:42 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-17 21:59:42 (GMT) |
| commit | ac89f8cab79800195687dd141de472f90c626ec3 (patch) | |
| tree | 394fd842d019152025ba9497aee10339982ec4b3 /Python/traceback.c | |
| parent | fd206b680705de3e0408fd29c7943317563e61e1 (diff) | |
| download | cpython-ac89f8cab79800195687dd141de472f90c626ec3.zip cpython-ac89f8cab79800195687dd141de472f90c626ec3.tar.gz cpython-ac89f8cab79800195687dd141de472f90c626ec3.tar.bz2 | |
bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596) (GH-29597)
If the string is ASCII only and doesn't need to escape characters,
write the whole string with a single write() syscall.
(cherry picked from commit b919d8105c4d77f00509b6d3ab2073f09db640de)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Python/traceback.c')
| -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 83f3074..23d0e38 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -719,6 +719,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); @@ -742,6 +762,8 @@ _Py_DumpASCII(int fd, PyObject *text) _Py_DumpHexadecimal(fd, ch, 8); } } + +done: if (truncated) { PUTS(fd, "..."); } |
