summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-11-17 21:59:19 (GMT)
committerGitHub <noreply@github.com>2021-11-17 21:59:19 (GMT)
commit4ffde90dccd741b04a448f2e44f0b82a41b6fe96 (patch)
tree30aa631cbe2d67a0799fff78b03ca6f04807260c /Python
parent1079b3e3cb3eba7062e174ecc6c0ab20c2d0722e (diff)
downloadcpython-4ffde90dccd741b04a448f2e44f0b82a41b6fe96.zip
cpython-4ffde90dccd741b04a448f2e44f0b82a41b6fe96.tar.gz
cpython-4ffde90dccd741b04a448f2e44f0b82a41b6fe96.tar.bz2
bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596) (GH-29598)
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')
-rw-r--r--Python/traceback.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/traceback.c b/Python/traceback.c
index 284c181..7d6f7f4 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -717,6 +717,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);
@@ -740,6 +760,8 @@ _Py_DumpASCII(int fd, PyObject *text)
_Py_DumpHexadecimal(fd, ch, 8);
}
}
+
+done:
if (truncated) {
PUTS(fd, "...");
}