8000 [3.9] bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596) by miss-islington · Pull Request #29597 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.9] bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596) #29597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump t 8000 o file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -742,6 +762,8 @@ _Py_DumpASCII(int fd, PyObject *text)
_Py_DumpHexadecimal(fd, ch, 8);
}
}

done:
if (truncated) {
PUTS(fd, "...");
}
Expand Down
0