From 6de5072f6dec9191b79e9edec917dc73306fb28a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Apr 2025 21:05:54 +0200 Subject: [PATCH 1/2] gh-127604: Replace dprintf() with _Py_write_noraise() --- Python/traceback.c | 58 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/Python/traceback.c b/Python/traceback.c index 56d94d6431befe..836c2fa89efced 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -842,11 +842,11 @@ _Py_DumpDecimal(int fd, size_t value) /* Format an integer as hexadecimal with width digits into fd file descriptor. The function is signal safe. */ -void -_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width) +static void +dump_hexadecimal(int fd, uintptr_t value, Py_ssize_t width, int strip_zeros) { char buffer[sizeof(uintptr_t) * 2 + 1], *ptr, *end; - const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1; + Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1; if (width > size) width = size; @@ -862,7 +862,28 @@ _Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width) value >>= 4; } while ((end - ptr) < width || value); - (void)_Py_write_noraise(fd, ptr, end - ptr); + size = end - ptr; + if (strip_zeros) { + while (*ptr == '0' && size >= 2) { + ptr++; + size--; + } + } + + (void)_Py_write_noraise(fd, ptr, size); +} + +void +_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width) +{ + dump_hexadecimal(fd, value, width, 0); +} + +static void +dump_pointer(int fd, void *ptr) +{ + PUTS(fd, "0x"); + dump_hexadecimal(fd, (uintptr_t)ptr, sizeof(void*), 1); } void @@ -1227,7 +1248,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size) || info[i].dli_fname == NULL || info[i].dli_fname[0] == '\0' ) { - dprintf(fd, " Binary file '' [%p]\n", array[i]); + PUTS(fd, " Binary file '' ["); + dump_pointer(fd, array[i]); + PUTS(fd, "]\n"); continue; } @@ -1237,11 +1260,12 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size) info[i].dli_saddr = info[i].dli_fbase; } - if (info[i].dli_sname == NULL - && info[i].dli_saddr == 0) { - dprintf(fd, " Binary file \"%s\" [%p]\n", - info[i].dli_fname, - array[i]); + if (info[i].dli_sname == NULL && info[i].dli_saddr == 0) { + PUTS(fd, " Binary file \""); + PUTS(fd, info[i].dli_fname); + PUTS(fd, "\" ["); + dump_pointer(fd, array[i]); + PUTS(fd, "]\n"); } else { char sign; @@ -1255,10 +1279,16 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size) offset = info[i].dli_saddr - array[i]; } const char *symbol_name = info[i].dli_sname != NULL ? info[i].dli_sname : ""; - dprintf(fd, " Binary file \"%s\", at %s%c%#tx [%p]\n", - info[i].dli_fname, - symbol_name, - sign, offset, array[i]); + PUTS(fd, " Binary file \""); + PUTS(fd, info[i].dli_fname); + PUTS(fd, "\", at "); + PUTS(fd, symbol_name); + (void)_Py_write_noraise(fd, &sign, 1); + PUTS(fd, "0x"); + dump_hexadecimal(fd, offset, sizeof(offset), 1); + PUTS(fd, " ["); + dump_pointer(fd, array[i]); + PUTS(fd, "]\n"); } } } From 83f4e998e112feec4cdadf7a4a7ad7b3c9c474f5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Apr 2025 21:36:31 +0200 Subject: [PATCH 2/2] Add dump_char() function --- Python/traceback.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Python/traceback.c b/Python/traceback.c index 836c2fa89efced..0a9f571cac7fb0 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -886,6 +886,13 @@ dump_pointer(int fd, void *ptr) dump_hexadecimal(fd, (uintptr_t)ptr, sizeof(void*), 1); } +static void +dump_char(int fd, char ch) +{ + char buf[1] = {ch}; + (void)_Py_write_noraise(fd, buf, 1); +} + void _Py_DumpASCII(int fd, PyObject *text) { @@ -945,8 +952,7 @@ _Py_DumpASCII(int fd, PyObject *text) ch = PyUnicode_READ(kind, data, i); if (' ' <= ch && ch <= 126) { /* printable ASCII character */ - char c = (char)ch; - (void)_Py_write_noraise(fd, &c, 1); + dump_char(fd, (char)ch); } else if (ch <= 0xff) { PUTS(fd, "\\x"); @@ -1283,7 +1289,7 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size) PUTS(fd, info[i].dli_fname); PUTS(fd, "\", at "); PUTS(fd, symbol_name); - (void)_Py_write_noraise(fd, &sign, 1); + dump_char(fd, sign); PUTS(fd, "0x"); dump_hexadecimal(fd, offset, sizeof(offset), 1); PUTS(fd, " [");