https://github.com/python/cpython/commit/402dba29281c1e1092ff32875c91f23bacabb677 commit: 402dba29281c1e1092ff32875c91f23bacabb677 branch: main author: Victor Stinner <vstin...@python.org> committer: vstinner <vstin...@python.org> date: 2025-04-23T20:02:25Z summary:
gh-127604: Replace dprintf() with _Py_write_noraise() (#132854) files: M Python/traceback.c diff --git a/Python/traceback.c b/Python/traceback.c index 56d94d6431befe..0a9f571cac7fb0 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,35 @@ _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); +} + +static void +dump_char(int fd, char ch) +{ + char buf[1] = {ch}; + (void)_Py_write_noraise(fd, buf, 1); } void @@ -924,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"); @@ -1227,7 +1254,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 '<unknown>' [%p]\n", array[i]); + PUTS(fd, " Binary file '<unknown>' ["); + dump_pointer(fd, array[i]); + PUTS(fd, "]\n"); continue; } @@ -1237,11 +1266,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 +1285,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); + dump_char(fd, sign); + PUTS(fd, "0x"); + dump_hexadecimal(fd, offset, sizeof(offset), 1); + PUTS(fd, " ["); + dump_pointer(fd, array[i]); + PUTS(fd, "]\n"); } } } _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com