mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
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.
This commit is contained in:
parent
e002bbc6cc
commit
b919d8105c
2 changed files with 27 additions and 0 deletions
|
@ -0,0 +1,5 @@
|
||||||
|
:mod:`faulthandler` can now write ASCII-only strings (like filenames and
|
||||||
|
function names) with a single write() syscall when dumping a traceback. It
|
||||||
|
reduces the risk of getting an unreadable dump when two threads or two
|
||||||
|
processes dump a traceback to the same file (like stderr) at the same time.
|
||||||
|
Patch by Victor Stinner.
|
|
@ -1072,6 +1072,26 @@ _Py_DumpASCII(int fd, PyObject *text)
|
||||||
truncated = 0;
|
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++) {
|
for (i=0; i < size; i++) {
|
||||||
if (kind != PyUnicode_WCHAR_KIND)
|
if (kind != PyUnicode_WCHAR_KIND)
|
||||||
ch = PyUnicode_READ(kind, data, i);
|
ch = PyUnicode_READ(kind, data, i);
|
||||||
|
@ -1095,6 +1115,8 @@ _Py_DumpASCII(int fd, PyObject *text)
|
||||||
_Py_DumpHexadecimal(fd, ch, 8);
|
_Py_DumpHexadecimal(fd, ch, 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
if (truncated) {
|
if (truncated) {
|
||||||
PUTS(fd, "...");
|
PUTS(fd, "...");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue