Skip to content

Commit 402dba2

Browse files
authored
gh-127604: Replace dprintf() with _Py_write_noraise() (#132854)
1 parent 99b1377 commit 402dba2

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

Python/traceback.c

+52-16
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,11 @@ _Py_DumpDecimal(int fd, size_t value)
842842

843843
/* Format an integer as hexadecimal with width digits into fd file descriptor.
844844
The function is signal safe. */
845-
void
846-
_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
845+
static void
846+
dump_hexadecimal(int fd, uintptr_t value, Py_ssize_t width, int strip_zeros)
847847
{
848848
char buffer[sizeof(uintptr_t) * 2 + 1], *ptr, *end;
849-
const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
849+
Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
850850

851851
if (width > size)
852852
width = size;
@@ -862,7 +862,35 @@ _Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
862862
value >>= 4;
863863
} while ((end - ptr) < width || value);
864864

865-
(void)_Py_write_noraise(fd, ptr, end - ptr);
865+
size = end - ptr;
866+
if (strip_zeros) {
867+
while (*ptr == '0' && size >= 2) {
868+
ptr++;
869+
size--;
870+
}
871+
}
872+
873+
(void)_Py_write_noraise(fd, ptr, size);
874+
}
875+
876+
void
877+
_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
878+
{
879+
dump_hexadecimal(fd, value, width, 0);
880+
}
881+
882+
static void
883+
dump_pointer(int fd, void *ptr)
884+
{
885+
PUTS(fd, "0x");
886+
dump_hexadecimal(fd, (uintptr_t)ptr, sizeof(void*), 1);
887+
}
888+
889+
static void
890+
dump_char(int fd, char ch)
891+
{
892+
char buf[1] = {ch};
893+
(void)_Py_write_noraise(fd, buf, 1);
866894
}
867895

868896
void
@@ -924,8 +952,7 @@ _Py_DumpASCII(int fd, PyObject *text)
924952
ch = PyUnicode_READ(kind, data, i);
925953
if (' ' <= ch && ch <= 126) {
926954
/* printable ASCII character */
927-
char c = (char)ch;
928-
(void)_Py_write_noraise(fd, &c, 1);
955+
dump_char(fd, (char)ch);
929956
}
930957
else if (ch <= 0xff) {
931958
PUTS(fd, "\\x");
@@ -1227,7 +1254,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12271254
|| info[i].dli_fname == NULL
12281255
|| info[i].dli_fname[0] == '\0'
12291256
) {
1230-
dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);
1257+
PUTS(fd, " Binary file '<unknown>' [");
1258+
dump_pointer(fd, array[i]);
1259+
PUTS(fd, "]\n");
12311260
continue;
12321261
}
12331262

@@ -1237,11 +1266,12 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12371266
info[i].dli_saddr = info[i].dli_fbase;
12381267
}
12391268

1240-
if (info[i].dli_sname == NULL
1241-
&& info[i].dli_saddr == 0) {
1242-
dprintf(fd, " Binary file \"%s\" [%p]\n",
1243-
info[i].dli_fname,
1244-
array[i]);
1269+
if (info[i].dli_sname == NULL && info[i].dli_saddr == 0) {
1270+
PUTS(fd, " Binary file \"");
1271+
PUTS(fd, info[i].dli_fname);
1272+
PUTS(fd, "\" [");
1273+
dump_pointer(fd, array[i]);
1274+
PUTS(fd, "]\n");
12451275
}
12461276
else {
12471277
char sign;
@@ -1255,10 +1285,16 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12551285
offset = info[i].dli_saddr - array[i];
12561286
}
12571287
const char *symbol_name = info[i].dli_sname != NULL ? info[i].dli_sname : "";
1258-
dprintf(fd, " Binary file \"%s\", at %s%c%#tx [%p]\n",
1259-
info[i].dli_fname,
1260-
symbol_name,
1261-
sign, offset, array[i]);
1288+
PUTS(fd, " Binary file \"");
1289+
PUTS(fd, info[i].dli_fname);
1290+
PUTS(fd, "\", at ");
1291+
PUTS(fd, symbol_name);
1292+
dump_char(fd, sign);
1293+
PUTS(fd, "0x");
1294+
dump_hexadecimal(fd, offset, sizeof(offset), 1);
1295+
PUTS(fd, " [");
1296+
dump_pointer(fd, array[i]);
1297+
PUTS(fd, "]\n");
12621298
}
12631299
}
12641300
}

0 commit comments

Comments
 (0)