Skip to content

Commit 6de5072

Browse files
committed
pythongh-127604: Replace dprintf() with _Py_write_noraise()
1 parent 4b4b9fb commit 6de5072

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

Python/traceback.c

Lines changed: 44 additions & 14 deletions
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,28 @@ _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);
866887
}
867888

868889
void
@@ -1227,7 +1248,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12271248
|| info[i].dli_fname == NULL
12281249
|| info[i].dli_fname[0] == '\0'
12291250
) {
1230-
dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);
1251+
PUTS(fd, " Binary file '<unknown>' [");
1252+
dump_pointer(fd, array[i]);
1253+
PUTS(fd, "]\n");
12311254
continue;
12321255
}
12331256

@@ -1237,11 +1260,12 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12371260
info[i].dli_saddr = info[i].dli_fbase;
12381261
}
12391262

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]);
1263+
if (info[i].dli_sname == NULL && info[i].dli_saddr == 0) {
1264+
PUTS(fd, " Binary file \"");
1265+
PUTS(fd, info[i].dli_fname);
1266+
PUTS(fd, "\" [");
1267+
dump_pointer(fd, array[i]);
1268+
PUTS(fd, "]\n");
12451269
}
12461270
else {
12471271
char sign;
@@ -1255,10 +1279,16 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12551279
offset = info[i].dli_saddr - array[i];
12561280
}
12571281
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]);
1282+
PUTS(fd, " Binary file \"");
1283+
PUTS(fd, info[i].dli_fname);
1284+
PUTS(fd, "\", at ");
1285+
PUTS(fd, symbol_name);
1286+
(void)_Py_write_noraise(fd, &sign, 1);
1287+
PUTS(fd, "0x");
1288+
dump_hexadecimal(fd, offset, sizeof(offset), 1);
1289+
PUTS(fd, " [");
1290+
dump_pointer(fd, array[i]);
1291+
PUTS(fd, "]\n");
12621292
}
12631293
}
12641294
}

0 commit comments

Comments
 (0)