Skip to content

gh-127604: Replace dprintf() with _Py_write_noraise() #132854

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 2 commits into from
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 52 additions & 16 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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");
}
}
}
Expand Down
Loading